sodcalculator

10 Tech How-To

Send Email in Ubuntu Terminal: Step-by-Step

Guide 10-52436 · Updated · Step by step

This guide shows you how to send an email directly from the Ubuntu command line using built-in tools. You’ll learn to send simple text emails and emails with attachments, all without opening a graphical mail client. This is useful for scripts, system notifications, or when you only have terminal access.

What You’ll Need

  • Ubuntu (or any Debian-based Linux distribution) with sudo access.
  • An SMTP server you can use. This could be a local mail server (like Postfix) or an external service like Gmail, Outlook, or your company’s server. You’ll need the server address, port, and credentials if required.
  • Internet connection if using an external SMTP server.

Step-by-Step

  1. Install the necessary package. The most common tool is mailutils, which provides the mail command. Open a terminal and run:

    sudo apt update
    sudo apt install mailutils

    If you also want to send attachments, install mutt as well:

    sudo apt install mutt
  2. Configure your SMTP server (if needed). If you’re using an external SMTP server, you’ll need to set up the configuration. For mailutils, the configuration file is usually /etc/mail.rc or ~/.mailrc. Add the following lines, replacing with your actual server details:

    set smtp=smtp://smtp.example.com:587
    set smtp-auth=login
    set smtp-auth-user=your-email@example.com
    set smtp-auth-password=your-password
    set from=your-email@example.com

    For Gmail, you might need to use an app-specific password and the server smtp.gmail.com:587 with STARTTLS.

  3. Send a basic email. Use the mail command with the subject and recipient. Type the message body, then press Ctrl+D to send.

    mail -s "Test Subject" recipient@example.com

    After pressing Enter, you’ll be prompted for Cc (carbon copy). Press Enter to skip, then type your message. End with Ctrl+D.

  4. Send an email with a message body from the command line. Use echo to pipe the message directly:

    echo "This is the email body" | mail -s "Subject" recipient@example.com
  5. Send an email with a file as the body. Use input redirection:

    mail -s "Report" recipient@example.com < /path/to/file.txt
  6. Send an email with an attachment using mutt. If you installed mutt, you can attach files easily:

    echo "See attached file." | mutt -s "Subject" -a /path/to/file.pdf -- recipient@example.com

    The -- separates the attachment from the recipient address.

  7. Send an email to multiple recipients. Separate addresses with commas or spaces:

    mail -s "Notice" user1@example.com,user2@example.com
  8. Verify the email was sent. The command usually returns silently on success. Check your recipient’s inbox (and spam folder). If you get an error message, refer to the Troubleshooting section below.

Common Mistakes

  • Forgetting to install mailutils. The mail command won’t exist if the package isn’t installed.
  • Using the wrong SMTP port. Many servers use 587 for submission with STARTTLS, not 465 (which is implicit TLS). Check your provider’s documentation.
  • Not using an app-specific password for Gmail. Regular Gmail passwords often fail due to security policies.
  • Missing the -- in mutt. Without it, mutt might treat the attachment as a recipient.
  • Spacing issues in the subject. If your subject contains spaces, quote it: -s "My Subject".

Tips

  • Use mail -s "Subject" -r sender@example.com to set a custom sender address (if allowed by your SMTP).
  • For HTML emails, use mutt -e "set content_type=text/html".
  • Test with your own email first to confirm the setup works.
  • For scripts, consider using sendmail or ssmtp for more control, but mail and mutt are simpler for most tasks.
  • Keep your SMTP password out of the config file if possible; use environment variables or a secure keyring.

Troubleshooting

  • Command not found: Install mailutils with sudo apt install mailutils.
  • Connection refused or timeout: Check that the SMTP server address and port are correct, and that your firewall allows outbound connections on that port.
  • Authentication failed: Verify your username and password. For Gmail, you may need to generate an app-specific password. Also check that you’re using the correct authentication method (login, plain, etc.).
  • Certificate errors: If you’re using SSL/TLS, you might need to add set smtp-use-starttls or set ssl-verify=ignore (not recommended for security) to your config.
  • Email goes to spam: This is common with new domains or if your IP is not trusted. Use a reputable SMTP provider and consider SPF/DKIM records.
  • Mutt attachment fails: Ensure the file path is correct and that the file is readable. Also, check that mutt is installed.

Frequently Asked Questions

Can I send email without an external SMTP server?

Yes, you can install a local mail server like Postfix and send to local users or relay to the internet, but for most use cases, using an external SMTP is simpler and more reliable.

What is the difference between mail and mutt?

mail is a basic mail client for sending simple text emails. mutt is a more feature-rich client that supports attachments, HTML, and multiple protocols. For basic sends, mail is enough; for attachments, use mutt.

How do I send an email with a subject that contains spaces?

Enclose the subject in double quotes: mail -s "Meeting on Friday" recipient@example.com.

Can I send an email to a distribution list?

Yes, you can list multiple recipients separated by commas or spaces. Alternatively, you can use a group alias in your mail server.

Is it safe to put my SMTP password in the config file?

It’s not ideal. Use environment variables or a more secure method like a keyring. For example, you can set SMTP_PASSWORD and reference it in the config using set smtp-auth-password=$SMTP_PASSWORD (though this may not work in all versions).

Why does my email get rejected by the recipient’s server?

Reasons include your IP being blacklisted, missing SPF/DKIM records, or the content being flagged as spam. Use a trusted SMTP provider and ensure your domain’s email authentication is set up.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →