19

Here's what I have:

cat UserReport.txt | mail -s "TestSubject" "[email protected]"

It's a small file, much smaller than a picture so it should send almost instantly. Though I receive nothing. I installed mailutils but might have installed it incorrectly.

How can I send an email from a bash script?

5
  • 1
    That shoud work if the system runs an MTA. Does your system use sendmail, ssmtp, postfix, ...?
    – Jan
    Sep 10, 2014 at 15:51
  • It's a 12.04 VM, if that tells you anything, and I have no clue what my company uses Sep 10, 2014 at 16:01
  • Having mailutils installed is not sufficient. You need a MTA, as @Jan noticed. What do ls -l $(which sendmail) tells you? And dpkg -S $(which sendmail)? what are you using to read and send email normally, and with which configuration? Please add this info to your question.
    – Rmano
    Sep 10, 2014 at 17:39
  • I am curious: what was the issue in stackoverflow.com/questions/25849276/… ?
    – fedorqui
    Sep 16, 2014 at 8:14
  • Useless use of cat Apr 1, 2017 at 14:04

3 Answers 3

31

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

Then open your postfix config file:

$ sudo -H gedit /etc/postfix/main.cf

and add following lines to it:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

You might have noticed that we haven’t specified our Gmail username and password in above lines. They will go into a different file. Open/Create:

$ sudo -H gedit /etc/postfix/sasl_passwd

And add following line:

[smtp.gmail.com]:587    [email protected]:PASSWORD

If you want to use your Google App’s domain, please replace @gmail.com with your @domain.com.

Fix permission and update postfix config to use sasl_passwd file:

$ sudo chmod 400 /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd

Next, validate certificates to avoid running into error. Just run following command:

$ cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Finally, reload postfix config for changes to take effect:

$ sudo /etc/init.d/postfix reload

Testing

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" [email protected]

To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account [email protected] with PASSWORD and check "Sent Mail" folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. This logging is one strong reason that we often use Gmail when mail delivery is critical.

Troubleshooting

Error: "SASL authentication failed; server smtp.gmail.com"

You need to unlock the captcha by visiting this page https://www.google.com/accounts/DisplayUnlockCaptcha

You can run test again after unlocking captcha.
source


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" [email protected] -a "UserReport.txt"  < MessageBody.txt

Using mutt

mutt -s "TestSubject" [email protected] -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"

7
  • I get an invalid head error Sep 10, 2014 at 15:53
  • While this answers how to send email from the commandline, the (obvious) problem of the user is: Why doesn't the recipient get the email?
    – Jan
    Sep 10, 2014 at 16:14
  • I appreciate the effort you put in this answer, but: How can you be sure that @Roboman1723 wants to use GMail as an MTA? Perhaps he / she is behind a firewall in a corporate environment and would need to use $company's MTA? IMHO we really need Roboman1723 to give detailed feedback before recommending something,
    – Jan
    Sep 11, 2014 at 6:42
  • sudo: /etc/postfix/sasl_passwd: command not found ! Feb 9, 2016 at 22:23
  • +1 for the note about CAPTCHA, this is the first tutorial I've seen that listed, and was exactly my problem for the past 2 hours Oct 5, 2016 at 2:03
3

UPDATED MANY YEARS LATER: Working on Ubuntu 16

  1. Allow "less secure apps" to connect to your Gmail by following this link:

    https://myaccount.google.com/lesssecureapps?pli=1
    
  2. Run the following commands:

    sudo update-ca-certificates
    sudo apt-get install msmtp-mta
    nano ~/.msmtprc
    
  3. Set the following msmtp config (replace [email protected], yourUsername and yourPassword):

    account gmail
    auth on
    host smtp.gmail.com
    port 587
    auth on
    tls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
    from [email protected]
    user yourUsername
    password yourPassword
    
    account default : gmail
    
  4. Config mail to use msmtp:

    nano ~/.mailrc
    
  5. Paste this:

    set sendmail="/usr/bin/msmtp"
    
  6. Finally send your mail (replace [email protected]):

    echo hello | mail -s test [email protected]
    
  7. Alternative you can use "mailx" to even send attachments easily:

    echo "mail body" | mailx -a /path/to/your/file.doc -s "mail subject" [email protected]
    
3
  • 2
    It gave error after sendimg mail msmtp: no recipients found Sending data to /usr/bin/msmtp failed: Process exited with a non-zero status cannot send message: Process exited with a non-zero status
    – user13107
    May 19, 2016 at 9:22
  • 1
    "Equifax_Secure_CA"... Yeah, that aged well. Jan 15, 2018 at 16:27
  • Why do you paste the above two lines into ~/.mailrc? msmtp-mta already creates a symlink from /usr/sbin/sendmail to /usr/bin/msmtp and "-a gmail" is not necessary since you already delared it as the default ("account default : gmail")? Or am I missing something?
    – mkurz
    Jun 5, 2018 at 22:44
2

Your line could look in shortest way like in this little shell-script:

#!/bin/bash
cat email.txt && sendmail [email protected] < /tmp/email.txt
0

Not the answer you're looking for? Browse other questions tagged .