Sending email on Ubuntu using Gmail credentials is a straightforward process that requires some initial setup. With the right configuration, you can use the command line to send emails from your Ubuntu machine using your Gmail account. Here's how to do it.
Step 1: Install the required packages
The first step is to install the packages needed for sending email on Ubuntu. Use 'apt-get' command directly to install as below.
sudo apt-get install msmtp msmtp-mta
This command will install the "msmtp" package, which is a simple command-line SMTP client, and the "msmtp-mta" package, which sets up MTA (Mail Transfer Agent) support for msmtp.
Step 2: Configure msmtp for Gmail
The next step is to configure msmtp to use your Gmail account for sending email. Open the msmtp configuration file by using any editor as you prefered, here we use 'vim':
sudo vim /etc/msmtprc
Add the below values into the opened file:
Replace "your_email" and "your_password" with your Gmail email address and password. Save and Exit from the file..
Step 3: Test the configuration
To test the configuration, run the following command in the terminal:
echo "Hello, World!" | msmtp email@example.com
Replace "recipient_email" with the email address of the person you want to send the email to.
This command will send a test email to the recipient using your Gmail account. If everything is working correctly, you should see a "queued for delivery" message.
Step 4: Set up an alias
To make sending emails even easier, you can set up an alias in your ".bashrc" file. You can use any editor as you are comfortable to edit like below. For this example, we use 'vim' editor.
Add the following line to the file:
alias gmail='echo "Type your message. Press Ctrl-D when finished." && cat | msmtp firstname.lastname@example.org'
Replace "recipient_email" with the email address of the person you want to send the email to. Save and Exit from the file..
Step 5: Send an email using the alias
To send an email using the alias, run the following command in the terminal:
Above command will openup a file in your default editor, Type your message, and then press Ctrl-D when finished. The message will be sent to the recipient using your Gmail account.