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?
First of all you need to install and configure Postfix to Use Gmail SMTP on Ubuntu.
Install all necessary packages:
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:
and add following lines to it:
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:
And add following line:
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:
Next, validate certificates to avoid running into error. Just run following command:
Finally, reload postfix config for changes to take effect:
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.
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
andmutt
to send emails, note that if you want to send attachment file viamail
command it's not support or it's better I say I can not send my attached file viamail
command, instead you can usemutt
command line, it's very useful. and inmutt
command you have to type attachment arguments after the email address. I test it and works fine.you can install
mutt
via this command:Using
mail
Using
mutt
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"UPDATED MANY YEARS LATER: Working on Ubuntu 16
Allow "less secure apps" to connect to your Gmail by following this link:
Run the following commands:
Set the following msmtp config (replace [email protected], yourUsername and yourPassword):
Config mail to use msmtp:
Paste this:
Finally send your mail (replace [email protected]):
Alternative you can use "mailx" to even send attachments easily:
Your line could look in shortest way like in this little shell-script: