First of all you need to install and configure Postfix to Use Gmail SMTP on Ubuntu.
Install all necessary packages:
$ sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com
Postfix: Configuring Gmail as Relay
I recently had to re-provision a VM which I use to run a bunch of cron jobs and email me reports. I use Ubuntu as my OS, Postfix as my mail server, and Gmail for my email account.. The details to make this happen can be found in quite a few places online, but the information was a bit scattered and didn’t match my situation exactly.
Steps to Success
1. Edit /etc/postfix/main.cf
# sets gmail as relay
relayhost = [smtp.gmail.com]:587
# use tls
smtp_use_tls=yes
# use sasl when authenticating to foreign SMTP servers
smtp_sasl_auth_enable = yes
# path to password map file
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# list of CAs to trust when verifying server certificate
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# eliminates default security options which are imcompatible with gmail
smtp_sasl_security_options =
If you need further debugging you can add these:
debug_peer_list=smtp.gmail.com
debug_peer_level=3
2. Add Gmail creds to /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 username:password
3. Run sudo postmap /etc/postfix/sasl_passwd
Make sure the /etc/sasl_passwd* files are owned by the postfix user:
sudo chown postfix sasl_passwd*
4. Run /etc/init.d/postfix reload
Troubleshooting
1. No TLS
status=bounced (host smtp.gmail.com[74.125.65.109] said: 530 5.7.0 Must issue a STARTTLS command first. s9sm727762ybm.8 (in reply to MAIL FROM command))
You’re missing
smtp_use_tls=yes
in main.cf2. Unknown CA
certificate verification failed for smtp.gmail.com[74.125.47.109]:587: untrusted issuer /C=US/O=Equifax/OU=Equifax Secure Certificate Authority
You’re missing
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
in main.cfesting
Check if mails are sent via Gmail SMTP server
If you have configured everything correctly, following command should generate a test mail from your server to your mailbox.
echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com
You need to use following syntax of
mail
and mutt
to send emails, note that if you want to send attachment file via mail
command it's not support or it's better I say I can not send my attached file via mail
command, instead you can use mutt
command line, it's very useful. and in mutt
command you have to type attachment arguments after the email address. I test it and works fine.
you can install
mutt
via this command:$ sudo apt-get install mutt
Using
mail
mail -s "TestSubject" nospam@gmail.com -a "UserReport.txt" < MessageBody.txt
Using
mutt
mutt -s "TestSubject" nospam@gmail.com -a "UserReport.txt" < MessageBody.txt
While
UserReport.txt
is your attachment file, MessageBody
is text/file of your body of email, TestSubject
is your email subject.-s
flag is used for "Subject" and -a
flag is used for "Attachment file"
ubuntu server mail response"
cat /var/mail/root
Example with attachment in Jenkins ( * start with / infront of var means absolute path meaning starting from root directory. ./ or var/ means relative path starting at current dir)
sources:
https://askubuntu.com/questions/522431/how-to-send-an-email-using-command-line
http://mhawthorne.net/posts/postfix-configuring-gmail-as-relay.html
No comments:
Post a Comment