Raspberry Pi_Eng_22.2.1 SSMTP Program – Mail Transfer Agent


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


22.2   Using E-mail

 

22.2.1  <SSMTP> Program - Mail Transfer Agent

 

We can send E-mails to others via our own E-mail server, such as gmail.com or hanmail.net usually with outlook or other tools.

 

We can send E-mail via E-mail server that we use through Raspberry Pi also. To do this, we need a MTA(Mail Transfer Agent) program that sends E-mail by connecting to E-mail server.

 

We will work with <SSMTP> program among available MTA programs in Raspberry Pi. This program is very simple and easy to set up and easy to use.

 

To install this program, run the following command.

 

sudo  apt-get  install  ssmtp

 

Once the program installation is complete, you must configure the specific MTA program. If <SSMTP> program installation is completed, the configuration file for <SSMTP> program is created in "/etc/ssmtp/ssmtp.conf" file. Use a text file editor to modify the contents of the file.

 

 

The following describes the main configuration items. The required items may differ depending on the mail server.

    Sending system

    Hostname=       Hostname of Raspberry Pi to connect to mail server

 

    Mail server and port information

    mailhub=          The DNS name and port of the mail server.

The standard uses ports "25", but it can vary depending on the mail server. Typically, ports are different for SSL and TLS.

    UseTLS=           This is the setting for using TLS (Transport Layer Security).

YES       -- When using TLS

NO       -- When using SSL

    UseSTARTTLS=   This is the setting for using TLS(Transport Layer Security)

YES       -- When using TLS

NO       -- When using SSL

    rewriteDomain=  Used domain.

It specifies the domain to use when you want it appears that the E-mail appear to be sent from the specified E-mail server.

 

    Sender's mail account and authentication method

    root=               Sender's E-mail address              

    AuthUser=         User account of E-mail server. Account @MailServer  

    AuthPass=         Password for user account of E-mail server.

    AuthMethod=    Authentication method to use when connecting to E-mail server.

    FromLineOverride= It specifies whether to allow sender of mail to be override.

YES - Allow the user to specify their own From: address

NO - Use the system generated From: address

 

 

The following is an example of configuration for sending E-mail using gmail.com.

 

root=username@gmail.com
mailhub=smtp.gmail.com:587               
rewriteDomain=gmail.com

Hostname=raspberrypi
AuthUser=username@gmail.com
AuthPass=password

AuthMethod=LOGIN

FromLineOverride=YES
UseTLS=YES

UseSTARTTLS=YES

 

To restrict access to "/etc/ssmtp/ssmtp.conf" file, run the following command.

 

chmod  774    /etc/ssmtp/ssmtp.conf

 

The meanings of the privileges granted here are as follows. This allows file owner and owner group to do everything but allow others to read only.

    7    -- Owner user can read, write, execute.

    7    -- Owner group can read, write, execute

    4    -- Other users can read only.

 

In this way, a user with the right to send E-mail can be given the necessary privileges.

 

<SSMTP> Once the setup of the program is completed, E-mail can be sent directly from the Terminal screen. Use the following command to send an E-mail using <SSMTP> program.

 

[Command Format]

ssmtp   <receiver-mail-address> 

 

[Command Overview]

    This start <SSMTP> program.

    User privilege           -- Normal user.

                                                        

[Detail Description]

    Creating an E-mail using standard input

When you execute the command as follows, it lets you input <mail-content> through <standard input>. After inputting the contents of mail in <standard input>, press [Ctrl + D] to means input is completed, and the operation that sends mail starts.

 

ssmtp   <receiver-mail-address> 

>  <mail-content>                    ßstandard input

 

    Creating an E-mail using input redirection function

If using Input redirection function "<<" as follows, it is easier to input mail contents. Enter the contents from the next line of "EOF", enter "EOF" when input is complete, and press [Enter] to execute the command.

 

ssmtp   <receiver-mail-address>  <<  EOF

<mail-content>   

EOF

 

    Mail contents are composed by using the following delimiters.

    From:    -- Sender's mail address in mail

    To:        -- Recipient's mail address in mail

    Cc:       -- Referrer's mail address in mail

    Bcc:      -- Hidden recipient's mail address in mail

    Subject: -- Subject of mail

    Body     -- Add a blank line after the preceding item and describes body.

 

Below is an example of composing a mail contents.

To: receiver-email-address@gmail.com

From: sender-email-address@gmail.com

Subject: this is test subject

 

This is the mail body!

 

[Main option]

-auusername

Specifies username for SMTP authentication.

-appassword

Specifies password for SMTP authentication.

-t

Read message, searching for recipients. ``To:'', `Cc:'', and ``Bcc:'' lines will be scanned for people to send to. Any addresses in the argument list will be suppressed (not supported).

-v

Go into verbose mode.

 

[Used Example]

Next is sending a simple E-mail to another E-mail account via "gmail" from Raspberry Pi.

 

pi@raspberrypi ~ $ ssmtp omegakim@realomega.com  

From: kim.dueggyu@gmail.com

To: omegakim@realomega.com

subject: test e-mail

 

This is test e-mail from Raspberry Pi.

 

The following is an example of using input redirection function to make processing more convenient.

 

pi@raspberrypi ~ $ ssmtp omegakim@realomega.com << EOF

From: kim.dueggyu@gmail.com

To: omegakim@realomega.com

subject: test e-mail

This is test e-mail from Raspberry Pi.

EOF

 

In the following example, the recipient's e-mail address is not specified in "To:" delimiter, but "-t" option is used to find and use the address specified in "To:" in the mail body contents.

 

pi@raspberrypi ~ $ ssmtp   -t  << EOF

From: kim.dueggyu@gmail.com

To: omegakim@realomega.com

subject: test e-mail

This is test e-mail from Raspberry Pi.

EOF

 

The following is confirming receipt of e-mail on outlook. The e-mail sent from "gmail" is confirmed.