How to Install Sendmail On Ubuntu 22.04 And Configure Gmail SMTP
How to setup and configure Sendmail on Ubuntu 22.04, 20.04 or Debian 10, 11 with Gmail SMTP and install mail server on Linux for Localhost or Server (VPS) without DKIM or DMARC authentication. Thereby you can send emails from applications or from websites quickly and ensure that they do not fall into the spam mailbox.
This method install Sendmail on Ubuntu 22.04 and configure a mail server on Linux or Debian. Sendmail, an SMTP (Simple Mail Transfer Protocol) program, is utilized to send outgoing mail from the server to the recipient via php-mail or php pear. Let’s follow the steps below provided by ItsmeIT to carry out the installation and configuration.
Step 1. Update the system and install Sendmail on Ubuntu
sudo apt-get update sudo apt-get install sendmail mailutils sendmail-cf sasl2-bin
In which ” mailutils ” allows running the mail command via PHP.
Step 2. Configure mail server on Ubuntu 22.04 and how to fix crashes
To configure sendmail on Ubuntu 22.04 with email management parameters, you will be prompted to confirm some configuration settings and enter ‘Y’ to proceed.
sudo sendmailconfig
Configure sendmail with the existing /etc/mail/sendmail.conf? [Y] Configure sendmail with the existing /etc/mail/sendmail.mc? [Y] Reload the running sendmail now with the new configuration? [Y]
If you run the command “ sendmailconfig ” to install email sending on Ubuntu or Debian and get a hang, press “Ctrl + X” to exit the suspended process and run the following commands one by one:
cd /etc/mail/tls sudo openssl dsaparam -out sendmail-common.prm 2048 sudo chown root:smmsp sendmail-common.prm sudo chmod 0640 sendmail-common.prm sudo dpkg --configure -a sudo sendmailconfig
Step 3. Configure Sendmail Ubuntu Gmail SMTP
1. Add SMTP Configuration
To add Google SMTP configuration, navigate to the mail folder, open the file “sendmail.mc”, and add the following configurations. After configuring Sendmail on your Ubuntu server, proceed to run the following command to build Gmail SMTP configuration.
Note: These configurations should be placed before the “MAILER_DEFINITIONS” when you configure Sendmail on Ubuntu 22.04.
sudo nano /etc/mail/sendmail.mc
# SMTP For Linux Ubuntu or Debian (itsmeit) define(`SMART_HOST',`[smtp.gmail.com]')dnl define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl define(`confAUTH_OPTIONS', `A p')dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-auth.db')dnl define(`confDOMAIN_NAME', `mail.itsmeit.co')dnl
Note:confDOMAIN_NAME
should be replaced with your real domain name. If you set up email on Ubuntu with localhost, you can configure it as mail.domain.com and then also add mail.domain.com to the /etc/hosts file. (Example: 127.0.1.1 localhost mail.itsmeit.co).
2. Build Gmail SMTP configuration on Ubuntu 22.04
As seen in the “Add SMTP Configuration” step for setting up and configure sendmail on Ubuntu 22.04, ItsmeIT has created a file named “gmail-auth.db” in the “authinfo” folder. To continue, open a terminal and run the following commands to build the necessary settings
:
sudo -s mkdir -m 700 /etc/mail/authinfo touch /etc/mail/authinfo/gmail-auth.db chmod 600 /etc/mail/authinfo/gmail-auth.db
3. Create a Gmail App Password to Use SMTP
To create an app and password on Google you can follow these steps:
- Sign in to your Google account
- Turn on 2-step verification under “ Security ”
- Type in the search box the word “ App password ” and click on it to open it.
- Select “Other (Custom name)” and give your app a name.
- Click “ Generate ” to generate the application.
- Copy and save the password and proceed to step 4 to continue to configure sendmail on Ubuntu 22.04.
4. Set up the application associated with Mail Server
Create a file named “gmail-auth” and add the content below and make sure you use the email and password of the created “app” to configure sendmail Ubuntu Gmail SMTP.
sudo nano /etc/mail/authinfo/gmail-auth
AuthInfo:smtp.gmail.com "U:buivanloi.2010@gmail.com" "P:16-digit-app-password"
As root, run the following command to create a Gmail SMTP configuration on Ubuntu. This command will create a hash map from the file “gmail-auth” located in the directory “/etc/mail/authinfo”, and save the result in a new file named “gmail-auth.db” in the same directory.
makemap hash /etc/mail/authinfo/gmail-auth.db < /etc/mail/authinfo/gmail-auth
After saving the gmail-auth file, restart sendmail to apply the changes to the settings and configurations for sending emails on Ubuntu 22.04.
sudo systemctl restart sendmail
5. Configuring Domain on Localhost
This step is for operating on localhost. If you are installing a mail server on Ubuntu Linux that is not on the local machine, skip this step.
Open the hosts file using the command below and add the email domain. In step 1, when adding the SMTP configuration, we specified the domain as “mail. itsmeit.co.” Now, let’s add this domain to the hosts file as follows:
sudo nano /etc/hosts
As you can see in the screenshot, I have specified that the email domain “mail. itsmeit.co” is mapped to the localhost IP addresses 127.0.0.1 and 127.0.1.1. This ensures that it will function on localhost.
127.0.0.1 localhost mail. itsmeit.co 127.0.1.1 itsmeit mail. itsmeit.co
Step 4. How to test sending email on Ubuntu or Debian with Terminal
After completing the complex process of configure sendmail on Ubuntu 22.04 in the previous sections, you can test whether the email setup is working by attempting to send an email to any email address. Below is an example command for sending mail in Linux. I have installed and tested the ability to send emails from the Ubuntu command line version 22.04:
echo \ "Content: Test gmail" | mail -s \ "Subject: Test sendemail" -a \ "From: buivanloi.2010@gmail.com" -a \ "To: To <buivanloi.2010@gmail.com>"buivanloi.2010@gmail.com
These are the steps to install and configure mail server on Ubuntu or Linux operating systems such as Ubuntu or Debian without having to authenticate DKIM or DMARC. This setup uses the Gmail account you have configured and allows you to send emails directly via SMTP. If you have any questions, please feel free to leave a comment below.
– Advertising –
This was exceedingly helpful, thank you so much
How can I ensure this runs and monitors the array on startup?