Monday, September 12, 2016

Raspberry Pi & Arch Linux - Day 3

Raspberry Pi & Arch Linux - Day 3

In this brief post, we will describe how to send email in Arch. 

There are several choices to send email in linux. To get things done quickly, I choose the one that looks the simplest: SSMTP.

Problem 3: How to send an email with SSMTP

If the ssmtp tool is not installed on the system, type pacman -S package-name to install it. On my Arch Linux, the ssmtp is not pre-installed.

After the installation, we need to edit the configuration file in /etc/ssmtp/ssmtp.conf. The configuration is straightforward and the options are self-explanatory.

Here is the sample configuration file copied from the Arch Wiki:

/etc/ssmtp/ssmtp.conf
# The user that gets all the mails (UID < 1000, usually the admin)
root=username@gmail.com

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also https://support.google.com/mail/answer/78799
mailhub=smtp.gmail.com:587

# The address where the mail appears to come from for user authentication.
rewriteDomain=gmail.com

# The full hostname
hostname=localhost

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
AuthUser=username
AuthPass=password

# Email 'From header's can override the default domain?
FromLineOverride=yes

To send an email, use the following command:

$ echo test | mail -v -s "testing ssmtp setup" tousername@somedomain.com

There is one critical issue with this approach: the security.  Note that the configuration contains the email username and password. It is not safe to keep your personal profile in this way. The Arch Wiki page suggests creating a new ssmtp group and set the ownership. The security problem would be my next topic but for today at least we can send the email.

Trick

As a beginner to the linux system, I am not familiar with all the commands. I often store some commands in a text file. Some commands are really long and it is not convenient to type the code every time. Here is a command that select a specific line in the file and execute it.

$ cat my-command-file | awk "NR==line-of-interest{print}" | bash

The first part of the command is just to print out the whole file; the second part is to select the line of interest and then finally pass it to bash. The bash command will execute the string command coming out of awk tool.


No comments:

Post a Comment