Freebsd Send Emails Gmail with msmtp Command

There are many reasons where sending email from the server or by command line is very useful. Because you are too lazy to log in to a Gmail account that is connected to 2FA or for security reasons. Whatever your reason, sending emails using the command line is worth trying, whether it's as a lesson or as a routine job for a system administrator.

Sending email with a private server is very fun, you will face challenges that are difficult for you to feel. For most people, sending emails is usually done using a web browser such as Google Chrome with attractive image displays. Now we will try to send an email via the command line with the private server that we have configured.

However, configuring an email server with exim, sendmail or postfix, exim requires many steps and also you will be faced with difficult and confusing configurations. However, you can simply use an existing email account, and send via MSMTP email as a traditional email client would.

In this tutorial, we will guide you and explain how to send email to a Google account with the help of MSMTP. This article will help you with the process of installing and configuring MSMTP on a FreeBSD server.





1. Installing MSMTP

Before you install MSMTP, update the FreeBSD pkg package or system ports. This process ensures that your system has the latest repository updates. Run the command below to update the PKG package or ports.

Update PKG or Ports
root@ns3:~ # pkg update -f
root@ns3:~ # pkg upgrade -f
root@ns3:~ # portmaster -a
root@ns3:~ # portupgrde -a
Continue by installing the msmtp dependencies.

Dependencies
root@ns3:~ # pkg install ca_root_nss bash indexinfo gnutls gettext-runtime
If the steps above have been completed, you can continue with installing msmtp. We will use the port system to install msmtp on FreeBSD.

/usr/ports/mail/msmtp
root@ns3:~ # cd /usr/ports/mail/msmtp
root@ns3:/usr/ports/mail/msmtp # make install clean
Type the script below in the mailer.conf file.

/etc/mail
root@ns3:~ # ee /etc/mail/mailer.conf
sendmail        /usr/local/bin/msmtp
send-mail       /usr/local/bin/msmtp
mailq           /usr/local/bin/msmtp
newaliases      /usr/local/bin/msmtp
hoststat        /usr/bin/true
purgestat       /usr/bin/true


2. Create app password Gmail

To send an email with the msmtp command, you have to do a little configuration on your Gmail account. In msmtp your Gmail password doesn't work, msmtp only requires "Gmail app password". Follow the steps below to create a “Gmail app password”.

1. Open your Gmail account
2. Click the Security menu on the left
3. Click "2-Step Verification"



4. Click "App passwords" at the very bottom



5. Click "Your app passwords", type the name of the app you want to create, for example "Test_FreeBSD_MSMTP"



6. After that, "password app" will appear. You will later use this password to configure MSMTP.




3. MSMTP Configuration

In this section we will configure MSMTP to connect to the Gmail server. In /root you create a .msmtprc file.

/root/.msmtprc
root@ns3:~ # touch .msmtprc
After you have finished creating the /root/.msmtprc file, type the following script in the file.

/root/.msmtprc
root@ns3:~ # ee /root/.msmtprc
defaults
auth           on
tls            on
tls_trust_file /usr/local/share/certs/ca-root-nss.crt
logfile /root/.msmtp.log

# Gmail
account         gmail
host            smtp.gmail.com
port            587
from datainchi@gmail.com
user datainchi
password thll cbrj ahvd pkrw

# Set a default account
account default : gmail
The next step is to create a .msmtprc symlink file.

/root/.msmtprc   /usr/local/etc/msmtprc
root@ns3:~ # ln -s /root/.msmtprc /usr/local/etc/msmtprc
Create a log file to store all activities performed by MSMTP.

/root/.msmtp.log
root@ns3:~ # touch /root/.msmtp.log
Pay attention to the .msmtprc script, the file says "tls_trust_file". We use FreeBSD's default TLS script which comes from the ca_root_nss application. The file is located in the /usr/local/share/certs directory. And we take the password from the Gmail account that we created above, namely "thll cbrj ahvd pkrw".

The next step is Change permissions.

Change permissions
root@ns3:~ # chmod 0777 /root/.msmtp.log
root@ns3:~ # chmod 0644 /root/.msmtprc


4. Test Send Email To Gmail

Now we will run the MSMTP server, by sending an Email via the command line. Run the following command to send an email to your Gmail account, friends or family.

Change permissions
root@ns3:~ # echo "Good Morning FreeBSD. I use the FreeBSD MSMTP server to send email to a Gmail account" | mail -s "Test Email" inchimediatama@gmail.com
The above command is used to send an email, from an MSMTP server.

From: datainchi@gmail.com
To: inchimediatama@gmail.com

The -s option in the script above is the subject of the email you will send

You can see the results in the "sent" menu of your email account, in this example "datainchi@gmail.com"



By following all the guidelines in this article, you have successfully sent an email via the command line. So very easy and simple to use. If you are interested in this guide, you can expand it with PHP commands or others.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post