How to configure email delivery through Gmail SMTP in shell script

I’m trying to set up automated email sending using my Gmail account through a shell script. I’ve installed msmtp and configured the settings file located at /etc/msmtp/msmtprc with the following configuration:

defaults
auth on
tls on
logfile ~/.msmtp.log

account gmail
host smtp.gmail.com
port 587
from [email protected]
user [email protected]
password app_password
tls_starttls on

account default : gmail

Here’s my shell script for sending messages:

#!/bin/bash

RECIPIENT="[email protected]"
TITLE="Email Title"
MESSAGE="This is the email content."

echo "$MESSAGE" | mail -s "$TITLE" "$RECIPIENT"

When I execute the script, I get this error message:

mail: cannot send message: Process exited with a non-zero status

I tested with a simple command to verify basic functionality:

echo "Testing message content" | mail -s "Testing Title" [email protected]

But I’m getting the same error. The mail logs show these issues:

mSMTP[892]: cannot locate smtp server
mSMTP[892]: failed to connect to mailhub:25
mSMTP[903]: Host configuration error for "smtp.gmail.com"
mSMTP[903]: Port setting invalid "587"
mSMTP[903]: cannot locate smtp server
mSMTP[903]: failed to connect to mailhub:25

What could be causing this configuration problem?

Your mail command isn’t linking to msmtp properly. Had this exact issue last year with monitoring scripts. Add sendmail_path configuration to point directly to msmtp. Create a wrapper script or modify your system’s mail config to use msmtp as the MTA. Gmail SMTP gets finicky with certain network setups. I switched to using curl directly with Gmail’s SMTP instead of the mail command: bash curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd --mail-from '[email protected]' --mail-rcpt '[email protected]' --user '[email protected]:app_password' -T message.txt This skips all the msmtp config headaches and works every time. Just format your message with proper headers in a text file first.

msmtp isn’t loading your config. Check if mail is actually using msmtp as the backend - most systems default to postfix or sendmail. Run which msmtp and alternatives --display mta to see what’s configured. Also, that config path /etc/msmtp/ looks off - it’s usually ~/.msmtprc or /etc/msmtprc

The Problem:

You’re experiencing issues sending emails using msmtp and the mail command, resulting in the error “mail: cannot send message: Process exited with a non-zero status”. Your logs indicate that msmtp cannot locate the SMTP server and is encountering port configuration errors. This suggests a problem with either your msmtp configuration, the mail command’s configuration, or your system’s mail handling setup.

:thinking: Understanding the “Why” (The Root Cause):

The mail command relies on a Mail Transfer Agent (MTA) to send emails. Your system likely defaults to an MTA like Postfix or Sendmail. The error indicates that the mail command is not correctly using msmtp as its backend MTA, instead falling back to the system default, which is failing to connect to Gmail’s SMTP server. Additionally, incorrect permissions or a misconfigured msmtprc file might prevent msmtp from accessing the necessary settings.

:gear: Step-by-Step Guide:

  1. Verify and Correct msmtp Configuration:

    First, ensure your msmtp configuration file is correctly configured and accessible. The most common location is ~/.msmtprc, but it can vary. If you are using /etc/msmtp/msmtprc, ensure the file permissions allow your user to read it. Otherwise, move the configuration file to ~/.msmtprc. It should look like this:

    defaults
    auth on
    tls on
    logfile ~/.msmtp.log
    
    account gmail
    host smtp.gmail.com
    port 587
    from [email protected]
    user [email protected]
    password app_password
    tls_starttls on
    
    account default : gmail
    

    Replace [email protected] and app_password with your actual Gmail address and the app password generated for your Gmail account. Crucially, ensure that two-factor authentication is enabled on your Gmail account. App passwords are essential for secure SMTP access when 2FA is active.

  2. Configure mail to Use msmtp:

    The mail command needs to be explicitly configured to use msmtp. This is system-dependent. On many systems, you can use update-alternatives (if available) to set msmtp as the default MTA for mail. For example on Debian-based systems:

    sudo update-alternatives --config mailx
    

    Select msmtp from the list of available mail transports. Alternatively, you can create a symbolic link from /usr/bin/mail to your msmtp binary, but this is system-dependent and requires caution as it can overwrite the default MTA settings. Consult your system’s documentation for the correct approach.

  3. Test Email Delivery:

    After making these changes, test your email sending script again:

    #!/bin/bash
    
    RECIPIENT="[email protected]"
    TITLE="Email Title"
    MESSAGE="This is the email content."
    
    echo "$MESSAGE" | mail -s "$TITLE" "$RECIPIENT"
    

    Check your ~/.msmtp.log file for any errors.

:mag: Common Pitfalls & What to Check Next:

  • Firewall Issues: Ensure your firewall allows outbound connections on ports 587 (SMTP) and 465 (SMTPS).
  • Gmail Account Settings: Double-check that your Gmail account allows less secure apps (though this is generally discouraged for security reasons and app passwords are preferred).
  • Incorrect App Password: Verify that the app password you’re using is correct. Regenerate a new app password if you suspect it’s been compromised.
  • DNS Resolution: If using a custom DNS server, temporarily switch to your ISP’s DNS to eliminate potential DNS resolution problems.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Your error logs show msmtp isn’t reading your config file. It’s trying to connect to “mailhub:25” instead of “smtp.gmail.com:587” - that’s the default fallback.

First, check if /etc/msmtp/msmtprc has the right permissions for your user. I had the same issue and just moved the config to ~/.msmtprc in my home directory - fixed it instantly.

Also make sure your mail command actually uses msmtp. You might need to set up alternatives or create a symlink. On Ubuntu, I had to run sudo update-alternatives --config mailx to get the right mail transport.

Double-check your Gmail app password is right and you’ve got two-factor auth enabled. Regular passwords don’t work with SMTP.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.