
How to linux send mail from command line using smtp server? Sending emails directly from the Linux command line is a useful skill for developers, administrators, and anyone looking to automate tasks or system notifications. By using an SMTP (Simple Mail Transfer Protocol) server, you can configure Linux to send emails easily, whether for error logging, system reports, or notifications. In this guide, we’ll explore step-by-step instructions on how to use the Linux command line to send emails via an SMTP server, covering various popular tools and best practices.
Prerequisites
Before getting started, ensure that:
- You have root or sudo access to the Linux machine.
- An SMTP server is available and configured for your domain or email provider.
- Necessary packages like mailx or sendmail are installed on your system.
Method 1: Using mail or mailx Command
The mail or mailx command is one of the simplest ways to send emails from the command line in Linux. Many Linux distributions have this package pre-installed, but if it’s missing, you can install it by running:
bash Copy code sudo-apt update sudo-apt install mailutils -y # For Debian/Ubuntu sudo-yum installing mailx -y # For CentOS/RHEL Sending an Email with mail
To send an email, use the following syntax:
bash Copy code echo "Email body content" | mail -s "Subject of the Email" [email protected]
- -s specifies the subject of the email.
- The recipient’s email address is the final argument.
For example:
bash Copy code echo "This is a test email from Linux!" | mail -s "Test Email" [email protected]
Configuring mailx with an SMTP Server
To set up mailx to use an SMTP server, modify its configuration file. Open or create the file .mailrc in your home directory:
bash Copy code nano ~/.mailrc
Add the following lines, replacing placeholders with your SMTP server details:
bash Copy code set-smtp=smtp://smtp.example.com:587 [email protected] set-smtp-auth-password=yourpassword set-ssl-verify=ignore
Save and close the file. Now, you can send emails through the specified SMTP server using mailx.
Method 2: Using sendmail Command
The sendmail command is a highly flexible mail transfer agent (MTA) that can be used to send emails. It is widely used in scripts and automated systems.
Install sendmail
To install sendmail, run the following commands:
bash Copy code sudo-apt update sudo-apt installing sendmail -y # For Debian/Ubuntu sudo-yum installing sendmail -y # For CentOS/RHEL
Sending an Email with sendmail
Create a text file with the email content. For instance, let’s call it email.txt:
plaintext Copy code To: [email protected] Subject: Test Email from Sendmail This is the email body. Sent via sendmail command from Linux!
Now, send the email with the following command:
bash Copy code sendmail [email protected] < email.txt
Method 3: Using ssmtp for Simplicity
If you prefer a lightweight approach, the ssmtp command is an easy-to-configure tool for sending emails via an SMTP server.
Install ssmtp
To install ssmtp, run:
bash Copy code sudo apt install ssmtp -y # Debian/Ubuntu sudo yum install ssmtp -y # CentOS/RHEL
Configure ssmtp for SMTP
Edit the configuration file /etc/ssmtp/ssmtp.conf:
bash Copy code sudo nano /etc/ssmtp/ssmtp.conf
Add the following details:
bash Copy code [email protected] mailhub=smtp.example.com:587 [email protected] AuthPass=yourpassword UseSTARTTLS=YES
Save and close the file. You can now send emails with ssmtp like so:
bash Copy code echo "This is the body" | ssmtp [email protected]
Method 4: Using Python’s smtplib for Customization
For users who need greater control or customization, Python’s built-in smtplib library is an excellent tool. This method can handle advanced setups and multiple recipients, making it a powerful option.
Example Script in Python
- Create a new Python script file, send_email.py:
python Copy code import smtplib from email.mime.text import MIMEText sender = "[email protected]" recipient = "[email protected]" subject = "Test Email from Python" body = "This is a test email sent from a Python's smtplib library." msg = MIMEText(body) msg['Subject'] = subject msg['From'] = sender msg['To'] = recipient with smtplib.SMTP("smtp.example.com", 587) as server: server.starttls() server.login(sender, "yourpassword") server.sendmail(sender, recipient, msg.as_string()) print("Email sent successfully!")
- Run the script using Python:
bash Copy code python3 send_email.py
This method provides more control over email formatting and allows for attachments, making it a versatile choice for custom requirements.
Troubleshooting Common Issues
- SMTP Authentication Failed
If you encounter authentication errors, double-check your SMTP credentials and ensure they are correct in the configuration files. - SSL/TLS Errors
Some SMTP servers require SSL/TLS. Enable these options in the configuration file of your chosen tool. For example, in ssmtp, set UseSTARTTLS=YES. - Port Blocked by Firewall
Make sure the port (usually 587 or 465 for SMTP) is open in your firewall settings.
Conclusion
Linux send mail from command line using smtp server is a powerful skill, enabling automation, system alerts, and regular reports to be sent without manual intervention. By using tools like mailx, sendmail, ssmtp, or even Python’s smtplib, you can tailor your approach to suit your needs. Make sure to secure sensitive information like SMTP passwords, and always verify SMTP configuration before implementation.