Sending Emails with OpenWRT

In the applications of the Internet of Things and embedded devices, sending system status, alerts, or log information via email is a common requirement.

OpenWRT, as a widely used embedded Linux distribution, provides flexible email sending configuration options. This article will detail the complete process from OpenWRT’s engineering configuration to the actual email sending.

The email sending function typically relies on msmtp (a lightweight SMTP client). It supports sending simple text emails using the SMTP protocol.

SMTP Protocol

Simple Mail Transfer Protocol — A protocol for sending emails.

SMTP is a network protocol used for sending electronic mail.

Format of Email Content

The format of the email itself follows the RFC 5322 (formerly RFC 822) standard, which mainly includes:

Email Headers

– From: Sender

– To: Recipient

– Subject: Subject

– Date: Date

– MIME-Version: Multimedia support version (e.g., 1.0)

– Content-Type: Content type (e.g., text/plain or multipart/mixed)

Email Body

– Plain text or HTML

– If there are attachments, use MIME encoding (e.g., Base64)

Example Email Content (sent during the DATA phase)

From: [email protected]: [email protected]: Test EmailMIME-Version: 1.0Content-Type: text/plain; charset=utf-8Hello,This is a test email sent via SMTP.

After constructing the above example content, it is piped to the msmtp application for sending. The usage will be explained below.

SMTP Functionality Support

Supported email service providers for msmtp

foxmail, yandex, qq, sina, sohu, 163, 126

It should be noted that the recipient’s email can receive emails sent from the above email providers.

hotmail, outlook, live, office365: Microsoft email services, using OAuth2 authentication, currently not supported; or suitable applications can be ported to OpenWRT to implement email sending functionality.

Compiling the msmtp Component

Execute the OpenWRT project

make menuconfig

Path for the email sending application component msmtp

Mail  --->  [*] msmtp...... Simple sendmail SMTP forwarding (with SSL support)

It is recommended to use a universal UTF-8 encoding format. If the email needs to be re-encoded, enable the component

Utilities  --->  [*] iconv...... Character set conversion utility

iconv is a character encoding conversion tool, for example:

Convert a GBK encoded file to UTF-8

Convert ISO-8859-1 to UTF-8

It can handle encoding formats. For example:

iconv -f GBK -t UTF-8 input.txt > output.txt

This command simply converts the file from GBK encoding to UTF-8.

Configuration File

Default Configuration

The default configuration file for msmtp /etc/msmtprc content

# Example for a system wide configuration file
# A system wide configuration file is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
account default
# The SMTP smarthost
host mail.oursite.example
# Use TLS on port 465
port 465
tls on
tls_starttls off
# Construct envelope-from addresses of the form "[email protected]"
from %[email protected]
# Syslog logging with facility LOG_MAIL instead of the default LOG_USER
syslog LOG_MAIL

Example Configuration

Example QQ email (remember to uncomment when using)

# /etc/msmtprc
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log

account default
host smtp.qq.com # SMTP server
port 587
tls_starttls on
from <email_account> # Sender email account
user <email_account> # User email account
password <email_auth_key> # SMTP authorization code

Configuration Notes

It is recommended that from and user be consistent.

The configuration file permissions must be 600 (-rw——-).

/etc/ssl/certs/ca-certificates.crt is the CA certificate, which exists by default in Linux systems.

SMTP server queries and authorization codes need to be opened by logging into the email website and operating in the email settings. Major domestic email service providers generally support POP3/SMTP/IMAP services.

Example of enabling authorization code for QQ email

Login → 「Settings」→「Account」→「Account and Security」→「Security Settings」→ Find 「POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV services」 → Generate authorization code.

Ports 465 and 587 are quite special.

Ports 465 and 587 use different TLS startup mechanisms.

  1. SMTPS (Implicit TLS)

  • Uses port 465 (historical but widely supported)

  • When the client connects to the server,it immediately starts the TLS encryption handshake, and the entire communication is encrypted from the start.

  • No need for <span><span>STARTTLS</span></span> command.

  • Similar to HTTPS (port 443): connection is encrypted immediately.

  • STARTTLS (Explicit TLS)

    • Uses port 587 (modern standard submission port)

    • The client first connects to the server inplain text and then sends the <span><span>STARTTLS</span></span> command,negotiating to upgrade to an encrypted connection.

    • STARTTLS must be used to enable TLS.

    • Similar to HTTP → upgrade to HTTPS.

    Port 465 configuration tls_starttls off; configuring on will cause an error

    msmtp: the server sent an empty reply
    msmtp: could not send mail (account default from /etc/msmtprc)

    Port 587 configuration tls_starttls onConfiguring off will cause an error

    msmtp: TLS handshake failed: An unexpected TLS packet was received.
    msmtp: could not send mail (account default from /etc/msmtprc)

    Specific support for ports 465 and 587 by major email service providers

    465 587 Server Connection
    QQ and foxmail smtp.qq.com
    NetEase: 163, 126, year.net smtp.163.com
    Sina smtp.sina.cn
    Sohu smtp.sohu.com
    yandex smtp.yandex.com

    Executing Send

    After configuring the /etc/msmtprc file, execute the following command line to send a properly structured plain text email

    {  echo "To: &lt;recipient_email&gt;"  echo "From: &lt;sender_email&gt;"  echo "Subject: &lt;subject&gt;"  echo "Content-Type: text/plain; charset=UTF-8"  echo "MIME-Version: 1.0"  echo ""  echo "&lt;body_line&gt;"} | msmtp &lt;recipient_email&gt;

    Notes

    <recipient_email>: Recipient’s email

    <sender_email>: Sender’s email

    <subject>: Email subject:

    <body_line>: Email body content

    Encoding Conversion Required

    Execute the following command line for encoding conversion

    echo "Original content" | iconv -f GBK -t UTF-8

    Note

    Major email platforms have sending limits, depending on specific restrictions, limiting the number of emails sent per minute or every 15 minutes.

    With the msmtp tool in OpenWRT, we can easily implement email sending functionality, suitable for various monitoring and alert scenarios.

    Ensure to follow security best practices, such as using application-specific passwords and restricting configuration file permissions. This method is not only suitable for personal projects but can also be extended to enterprise-level device management.

    Leave a Comment