Sending Email With Gmail and Custom From Address Using GNU Mail

In this guide, we will walk you through the process of sending an email using Gmail as the SMTP server and customizing the “From” address using the GNU Mail command-line utility on a Ubuntu-based system. This can be useful if you want to send emails from a specific address other than your Gmail account.

Prerequisites

Before you begin, make sure you have the following prerequisites:

  1. A Ubuntu-based Linux system.
  2. A Gmail account from which you want to send emails.
  3. Internet connectivity.

Step 1: Install Required Packages

Open a terminal and install the necessary packages:

1
2
```bash
sudo apt install ssmtp mailutils

Step 2: Configure ssmtp

Edit the ssmtp configuration file to use your Gmail account for sending emails. You can use the text editor of your choice. In this example, we’ll use vim:

1
sudo vim /etc/ssmtp/ssmtp.conf

Uncomment the following line to enable overriding the “From” address:

1
2
```plaintext
FromLineOverride=YES

Add the following lines to configure Gmail as the SMTP server:

1
2
3
4
5
6
```plaintext
[email protected]
AuthPass=YOURPASSWORD
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

Make sure to replace [email protected] and YOURPASSWORD with your actual Gmail email address and password.

Save and exit the configuration file.

Step 3: Configure .mailrc

Install the bsd-mailx package to enable GNU Mail to read the .mailrc configuration file:

1
sudo apt install bsd-mailx

Create or edit the .mailrc file in your home directory to set the default “From” address for outgoing emails. In this example, we’re setting it to “No Reply [email protected]”:

1
2
```bash
echo 'set from="No Reply <no.reply@example.com>"' >> ~/.mailrc

Step 4: Test Sending an Email

You can now test sending an email using the mail command. Here are a couple of examples:

1
2
```bash
echo "This is a test" | mail -s "Test" [email protected]

In this example, replace [email protected] with the recipient’s email address.

1
2
```bash
echo "Tester" | mail -r "No Reply <no.reply@example.com>" -s "Test" [email protected]

This command sends an email with the custom “From” address specified in the .mailrc file.

That’s it! You’ve successfully configured and tested sending emails using Gmail as the SMTP server and customizing the “From” address using GNU Mail on your Ubuntu-based system. You can now use these steps to send emails from your desired address using the mail command.

0%