Linux how to use postfix SMTP sent a mail

By December 9, 2016Tutorials

1、Install mailx/postfix

The test environment is selected Ubuntu 14.04.X and CemtOS6.X.
<code> Ubuntu open shell and run those commands:
sudo apt-get install heirloom-mailx
sudo apt-get install postfix

CentOS open shell and run those commands:
yum install mailx.x86_64
yum install postfix.x86_64
</code>

2、For example GMAIL make SSL authentication certificate and key

Open Linux shelland run those commands:
<code>mkdir .certs
certutil -N -d .certs
echo -n | openssl s_client -connect smtp.gmail.com:465 | sed -ne ‘/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p’ > /$PATH$/.certs/gmail.crt
certutil -A -n “Google Internet Authority” -t “C,,” -d /$PATH$/.certs -i set /$PATH$/.certs/gmail.crt (Replace $ PATH $ with the actual path)</code>

Tips:  The certutil may return a command not found error, according to the tips to install.

3、Modify the mailx configuration file

Ubuntu modify the /etc/nail.rc file , CentOS modify the /etc/mail.rc file, the file to increase the following content:
<code>set smtp-use-starttls(Tips:  This condition is increased when using TLS / STARTTLS port: 587)
set ssl-verify=ignore
set nss-config-dir=/$PATH$/.certs (Replace $ PATH $ with the actual path)
set from=username@astercc.com
set smtp=smtps://smtp.gmail.com:465(Tips: Gmail use TLS / STARTTLS port 587 can be changed to, set “smtp=smtp: //smtp.gmail.com: 587″)
set smtp-auth-user=username@astercc.com
set smtp-auth-password=password
set smtp-auth=login</code>

If you need more than one mailbox account and the presence of the configuration file, when send a email to provide choice in the there is another way is as follows:
<code>account gmail {
set smtp-use-starttls(Tips:  This condition is increased when using TLS / STARTTLS port: 587)
set ssl-verify=ignore
set nss-config-dir=/$PATH$/.certs (Replace $ PATH $ with the actual path)
set from=username@astercc.com
set smtp=smtps://smtp.gmail.com:465(Tips: Gmail use TLS / STARTTLS port 587 can be changed to, set “smtp=smtp: //smtp.gmail.com: 587″)
set smtp-auth-user=username@astercc.com
set smtp-auth-password=password
set smtp-auth=login
}</code>
This requires the mail / mailx command to add “-A” parameter reference.

4、Description of parameters added by the configuration file

set smtp-use-starttls:  Gmail send a mail use TLS/STARTTLS.
ssl-verify: Use SSL.
nss-config-dir: The local path to SSL authentication certificate and key.
from: Send a email for this address.
smtp: The address of the SMTP server.
smtp-auth-user:  The SMTP server authentication user name.
smtp-auth-password: The SMTP server authentication user password.
smtp-auth: Mail authentication method.
Determine the configuration parameters, save the file, you can use the mail / mailx command to send the email.

5、Send a test email

Command line Use the following command to send mail <code> mail / mailx (-vcabSA) -s “Test mail” address </ code>

Enter message in the shell, enter the content after the completion of carriage return “ctrl + d” send mail content, Multiple recipients are separated by commas, and you can also get the content of the message to be sent via the pipe (|) and redirection (<).
<code>-s Specifies the message subject.
-v Lists messages such as the location, status, and so on when send a email.
-c The list of users to cc.
-a The file specifies the file to attach.
-b Send a whisper List, multiple lists should be use “,” to separate.
-S Set the variables of the internal options (such as the above configuration file can be added by -S smtp-auth-user=username@astercc.com specified).
-A When have multiple SMTP account name to use. </ Code>

Tips:

Resolving host smtp.gmail.com . . . done.
Connecting to 173.194.72.108:587 . . . connected.
Error initializing NSS: Unknown error -8015.
“/root/dead.letter” 11/300
. . . message not sent.
Email sent with an exception return error similar to the above, such errors can be understood as no certificate or create a certificate does not take effect.

Tips : SMTP send mail encounter “535” error account or password error. Some mailboxes need to change the password to SMTP authorization code.

Leave a Reply