Email not Sent Using Shell cmd/script on Centos

2019-08-26 06:49发布

问题:

I'm trying to send an email from my centos using shell cmd to my gmail address.

Here is configuration that I have done:

/etc/postfix/main.cf

myhostname = relay.example.com
relayhost = [smtp.gmail.com]:587
mailbox_size_limit = 0
recipient_delimiter = +
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous 

/etc/postfix/sasl_passwd

[smtp.gmail.com]:587 My_Address@gmail.com:My_Password

I created hash db file for Postfix:

postmap /etc/postfix/sasl_passwd
chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

I run the cmd after restarting Postfix:

echo "Body" | mail -s "Subject" My_Address@gmail.com

I set gmail so that it can accept less secure apps.

Note that I don't have networking problems issues (as you see the connectivity below):

1st Connectivity Test 2nd Connectivity Test

After running the cmd no error appears, but the message is not sent.

Veiw of logs: tail -f /var/log/maillog

Nov  2 00:55:43 quickstart sendmail[5228]: wA1JwGn3031205: to=<My_Address@gmail.com>, ctladdr=<cloudera@quickstart.cloudera> (501/501), delay=11:56:47, xdelay=00:00:00, mailer=esmtp, pri=390482, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
Nov  2 00:55:43 quickstart sendmail[5228]: wA1JchlC026214: to=<My_Address@gmail.com>, ctladdr=<cloudera@quickstart.cloudera> (501/501), delay=12:16:44, xdelay=00:00:00, mailer=esmtp, pri=480482, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
Nov  2 00:55:43 quickstart sendmail[5228]: wA1JCSq5019689: to=<My_Address@gmail.com>, ctladdr=<cloudera@quickstart.cloudera> (501/501), delay=12:42:59, xdelay=00:00:00, mailer=esmtp, pri=480482, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
Nov  2 01:17:17 quickstart postfix/postfix-script[8412]: fatal: the Postfix mail system is not running
Nov  2 01:17:17 quickstart postfix/postfix-script[8469]: warning: group or other writable: /etc/postfix/./main.cf
Nov  2 01:17:17 quickstart postfix/postfix-script[8483]: starting the Postfix mail system
Nov  2 01:17:17 quickstart postfix/master[8484]: fatal: bind 127.0.0.1 port 25: Address already in use
Nov  2 01:18:14 quickstart sendmail[8554]: wA28IEGk008554: from=cloudera, size=228, class=0, nrcpts=1, msgid=<201811020818.wA28IEGk008554@quickstart.cloudera>, relay=root@localhost
Nov  2 01:18:14 quickstart sendmail[8558]: wA28IEsc008558: from=<cloudera@quickstart.cloudera>, size=483, class=0, nrcpts=1, msgid=<201811020818.wA28IEGk008554@quickstart.cloudera>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1]
Nov  2 01:18:14 quickstart sendmail[8554]: wA28IEGk008554: to=My_Addressq@gmail.com, ctladdr=cloudera (501/501), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30228, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (wA28IEsc008558 Message accepted for delivery)

Could you please help, thanks!

回答1:

I solved it! The problem is that I installed Sendmail also, which is another mail server. So automatically Postfix was disabled because there can't be two of them running at the same time.

Here is the fatal error from logs: tail -f /var/log/maillog

Nov  2 01:17:17 quickstart postfix/postfix-script[8412]: fatal: the Postfix mail system is not running

I resoleved it by removing sendmail completely

[root@quickstart ~]# yum remove sendmail

Then restarting Postfix

[root@quickstart ~]# service postfix restart
Shutting down postfix:                                     [  OK  ]
Starting postfix:                                          [  OK  ]


回答2:

I understood that you don't have problem with network. There is small change needed in the mail sending script. Please follow the below format and let me if it works

echo "Body" | mail -s "Subject" -m My_Address@gmail.com