Setting up `ssmtp` involves configuring it to use a mail server for sending emails. Here's a step-by-step guide to help you set up `ssmtp` on a Linux system. In this example, I'll use Gmail as the mail server, but you can adapt the settings for other mail servers as needed.
Step 1: Install `ssmtp`
First, ensure that `ssmtp` is installed on your system. Use the following command:
sudo apt install ssmtp
sudo apt update
Step 2: Configure `ssmtp`
Edit the `ssmtp` configuration file using a text editor. In this example, I'll use `nano`:
sudo nano /etc/ssmtp/ssmtp.conf
Add the following configuration to the file:
# Config file for sSMTP sendmail
root=username@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=username@gmail.com
AuthPass=your_gmail_app_password
UseTLS=YES
UseSTARTTLS=YES
Replace `username@gmail.com` with your Gmail address and `your_gmail_app_password` with the [App Password](https://support.google.com/accounts/answer/185833?hl=en) generated for your Gmail account. Save the file (press `Ctrl + O`, then press `Enter`, and exit with `Ctrl + X`).
Step 3: Test `ssmtp` Configuration
Now, you can test whether `ssmtp` is working correctly. Use the following command to send a test email:
echo "Test message" | sudo ssmtp recipient@example.com
Replace `recipient@example.com` with the email address where you want to send the test message. If the configuration is correct, you should receive the test email.
Additional Tips:
- Ensure that the `ssmtp` configuration file has the correct permissions. Run the following command to restrict access to the file:
sudo chmod 640 /etc/ssmtp/ssmtp.conf
- You might need to enable "Less secure app access" for your Gmail account if you face authentication issues. Visit your [Google Account Security Settings](https://myaccount.google.com/security-checkup) and turn on "Less secure app access" temporarily.
Example:
Bash script that runs daily using cron, fetches the RAM and disk usage information and sends an email with the collected data.
Here's a sample Bash script named `daily_email_report.sh`:
#!/bin/bash
# Set the recipient email address
recipient="your_email@example.com"
# Function to get RAM usage
get_ram_usage() {
echo "RAM Usage:"
free -h
echo "------------------------"
}
# Function to get disk usage
get_disk_usage() {
echo "Disk Usage:"
df -h
echo "------------------------"
}
# Function to send email using ssmtp
send_email() {
subject="Daily System Report"
message=$(get_ram_usage; get_disk_usage)
echo "$message" | sudo ssmtp "$recipient" -s "$subject"
}
# Run the script to get system information and send an email
send_email
Save this script, make it executable with:
chmod +x daily_email_report.sh
Now, you can set up a cron job to run this script daily at 9 AM. Open your crontab configuration by running:
crontab -e
Add the following line to schedule the script:
0 9 * * * /path/to/daily_email_report.sh
Replace `/path/to/daily_email_report.sh` with the actual path where you saved the script. Save and exit the editor.
This cron schedule (`0 9 * * *`) means the script will run at 9 AM every day. Adjust the timing as needed.
Now, your system will automatically send an email with RAM and disk usage information every day at 9 AM. Make sure your system time is set correctly for cron to work as expected.
We can also use mailutils or mutt for sending an email.
Comments
Post a Comment