Differences
This shows you the differences between two versions of the page.
linux_wiki:send_email_via_gmail_smtp_cmd_line [2014/12/08 20:28] billdozor |
linux_wiki:send_email_via_gmail_smtp_cmd_line [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Send Email via Gmail SMTP Cmd Line ====== | ||
- | Goal: To send an e-mail message from a bash script or command line program. | ||
- | Prerequisites | ||
- | * Install msmtp | ||
- | * Install mailx | ||
- | |||
- | ===== Install msmtp ===== | ||
- | This package will allow you to configure what gmail account to send from and will connect securely via TLS to the gmail SMTP servers. | ||
- | |||
- | <code bash> | ||
- | sudo apt-get install msmtp-mta | ||
- | </ | ||
- | |||
- | ===== Install Mailx ===== | ||
- | Install a simple mail transfer agent to actually prepare the e-mail message. | ||
- | |||
- | <code bash> | ||
- | sudo apt-get install heirloom-mailx | ||
- | </ | ||
- | |||
- | ===== Configure Gmail Account on MSMTP ===== | ||
- | |||
- | 1) Create a file in your home directory called " | ||
- | <code bash> | ||
- | vim ~/.msmtprc | ||
- | </ | ||
- | |||
- | 2) Paste the following into the config file, change the " | ||
- | |||
- | <code bash> | ||
- | #Gmail account | ||
- | defaults | ||
- | logfile ~/msmtp.log | ||
- | |||
- | account gmail | ||
- | auth on | ||
- | host smtp.gmail.com | ||
- | from your_address@gmail.com | ||
- | auth on | ||
- | tls on | ||
- | tls_trust_file / | ||
- | user your_address@gmail.com | ||
- | password your_gmail_password | ||
- | port 587 | ||
- | |||
- | account default : gmail | ||
- | </ | ||
- | |||
- | **Note:** If you use two factor authentication for gmail, you will need to create an application specific password for this config file. To do that: | ||
- | * Login to Gmail | ||
- | * Click your Account Icon (top right), then Account | ||
- | * Click on the Security tab on the top | ||
- | * In the " | ||
- | * Select a custom name and type something to describe msmtp so you won't forget what it is for. | ||
- | * Click Generate | ||
- | * Copy and paste the password from the pop up box into the config file above as " | ||
- | |||
- | 3) Secure the config file | ||
- | Since it contains credentials, | ||
- | <code bash> | ||
- | chmod 600 .msmtprc | ||
- | </ | ||
- | |||
- | ===== Configure Mailx ===== | ||
- | Setup mailx to use msmtp to send e-mail. | ||
- | |||
- | Create a .mailrc in your home folder: | ||
- | <code bash> | ||
- | vim ~/.mailrc | ||
- | </ | ||
- | |||
- | Paste the following in: | ||
- | <code bash> | ||
- | set sendmail="/ | ||
- | set message-sendmail-extra-arguments=" | ||
- | </ | ||
- | |||
- | ===== Send mail from the command line ===== | ||
- | |||
- | ==== Interactively ==== | ||
- | |||
- | <code bash> | ||
- | mail -s " | ||
- | </ | ||
- | |||
- | You will now be able to type in your message. When you want to send it, type ENTER to go to a blank line and then Ctrl+D to end/send. | ||
- | |||
- | ==== Auto Messages ==== | ||
- | To send an e-mail from a script, you will need to pre-write your message. | ||
- | |||
- | Populate a text file, such as " | ||
- | Then, send it like this: | ||
- | <code bash> | ||
- | mail -s "My Awesome Message Subject" | ||
- | </ |