Bài 16C: SMTP-CLI

SMTP-CLI

smtp-cli là công cụ dòng lệnh rất mạnh của SMTP, hỗ trợ các tính năng nâng cao như STARTTLS, SMTP-AUTH, hay IPv6. Có thể gởi email với plain-text hay html kết hợp với phần plain-text, file kèm và ảnh nằm trong email.

Tên smtp-cli thay cho:

  1. smtp-client
  2. smtp-command line interface

Script này cần cài đặt các gói phụ thuộc như

apt-get install libio-socket-ssl-perl libdigest-hmac-perl libterm-readkey-perl libmime-lite-perl libfile-libmagic-perl libio-socket-inet6-perl

C1. Cho smtp-cli khả năng thực thi

$ chmod +x smtp-cli

C2. Test với GMail

./smtp-cli --verbose --host=smtp.gmail.com:587 --enable-auth --user mymail@gmail.com --from test@domain.com --to user@another.domain.org --data message-body.txt

C3. Gởi mail với file kèm

./smtp-cli --from test@domain.com --to user@another.domain.org  --subject "Simple test with attachments"  --body-plain "Log files are attached."  --attach /var/log/some.log@text/plain  --attach /var/log/other.log

C4. File kèm nằm trong thân email

./smtp-cli --from test@domain.com --to user@another.domain.org --subject "Image as a mail body" --attach /path/to/tux.png

C5. Tạo email dạng html với ảnh nằm bên trong thân email

body.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body bgcolor="#ffffff" text="#000000">
<div align="center">
Here comes embedded <font color="#006600"><b>Mr Tux</b></font><br>
<img src="cid:tux.png"><br>
<i>Nice, isn't it?</i><br>
<img src="cid:smiley.png"><br>
</div>
</body>
</html>

body.txt

Here comes embedded Mr Tux
... actually it doesn't ... 
Not in a text-only mail reader.
Sorry

Câu lệnh gởi mail

./smtp-cli --from test@domain.com --to user@another.domain.org --subject "HTML with embedded image" --body-html body.html --body-plain body.txt --attach-inline tux.png --attach-inline smiley.png

sau –body-html–body-plain có thể là tên file hay text

 

C6. Cú pháp chung

Usage: smtp-cli [--options]

        --server=<hostname>[:<port>]
                                Host name or IP address of the SMTP server.
                                May include the port after colon, alternatively
                                use --port.
        --port=<number>         Port where the SMTP server is listening.
                                (default: 25)

        -4 or --ipv4            Use standard IP (IPv4) protocol.
        -6 or --ipv6            Use IPv6 protocol. For hosts that have
                                both IPv6 and IPv4 addresses the IPv6
                                connection is tried first.

        --hello-host=<string>   String to use in the EHLO/HELO command.
        --disable-ehlo          Don't use ESMTP EHLO command, only HELO.
        --force-ehlo            Use EHLO even if server doesn't say ESMTP.

        Transport encryption (TLS)
        --disable-starttls      Don't use encryption even if the remote
                                host offers it.
        --ssl                   Start in SMTP/SSL mode (aka SSMTP).
                                Default when --port=465
        --disable-ssl           Don't start SSMTP even if --port=465
        --ssl-ca-file=<filename>
                                Verify the server's SSL certificate against
                                a trusted CA root certificate file.
        --ssl-ca-path=<dirname> Similar to --ssl-ca-file but will look for
                                the appropriate root certificate file in
                                the given directory. The certificates must
                                must be stored one per file with hash-links
                                generated by, for example, c_rehash script
                                from OpenSSL.

        Authentication options (AUTH)
        --user=<username>       Username for SMTP authentication.
        --pass=<password>       Corresponding password.
        --auth-login            Enable only AUTH LOGIN method.
        --auth-plain            Enable only AUTH PLAIN method.
        --auth-cram-md5         Enable only AUTH CRAM-MD5 method.
        --auth                  Enable all supported methods. This is
                                normally not needed, --user enables
                                everything as well.

        Sender / recipient
        --from="Display Name <add@re.ss>"
                                Sender's name address (or address only).
        --to="Display Name <add@re.ss>"
        --cc="Display Name <add@re.ss>"
        --bcc="Display Name <add@re.ss>"
                                Message recipients. Each parameter can be
                                used multiple times.
                                The --bcc addresses won't apprear in
                                the composed message.

        SMTP Envelope sender / recipient
        (rarely needed, use --from, --to, --cc and --bcc instead)
        --mail-from=<address>   Address to use in MAIL FROM command.
                                Use --from instead, unless you want
                                a different address in the envelope and
                                in the headers.
        --rcpt-to=<address>     Address to use in RCPT TO command. Can be
                                used multiple times. Normally not needed,
                                use --to, --cc and --bcc instead.
                                If set the --to, --cc and --bcc will only
                                be used for composing the message body and
                                not for delivering the messages.

        Send a complete RFC822-compliant email message:
        --data=<filename>       Name of file to send after DATA command.
                                With "--data=-" the script will read
                                standard input (useful e.g. for pipes).

        Alternatively build email a message from provided components:
        --subject=<subject>     Subject of the message
        --body-plain=<text|filename>
        --body-html=<text|filename>
                                Plaintext and/or HTML body of the message
                                If both are provided the message is sent
                                as multipart.
        --charset=<charset>     Character set used for Subject and Body,
                                for example UTF-8, ISO-8859-2, KOI8-R, etc.
        --text-encoding=<encoding>
                                Enforce Content-Transfer-Encoding for text
                                parts of the email, including body and
                                attachments. Must be one of:
                                7bit, 8bit, binary, base64, quoted-printable
                                The default is: quoted-printable
        --attach=<filename>[@<MIME/Type>]
                                Attach a given filename.
                                MIME-Type of the attachment is guessed
                                by default guessed but can optionally
                                be specified after '@' delimiter.
                                For instance: --attach mail.log@text/plain
                                Parameter can be used multiple times.
        --attach-inline=<filename>[@<MIME/Type>]
                                Attach a given filename (typically a picture)
                                as a 'related' part to the above 'body-html'.
                                Refer to these pictures as <img src='cid:filename'>
                                in the 'body-html' contents.
                                See --attach for details about MIME-Type.
                                Can be used multiple times.
        --add-header="Header: value"
        --replace-header="Header: value"
        --remove-header="Header"
                                Add, Replace or Remove pretty much any header
                                in the email. For example to set a different
                                Mailer use --replace-header="X-Mailer: Blah",
                                to remove it altogether --remove-header=X-Mailer
                                or to add a completely custom header use
                                --add-header="X-Something: foo bar".
        --print-only            Dump the composed MIME message to standard
                                output. This is useful mainly for debugging
                                or in the case you need to run the message
                                through some filter before sending.

        Other options
        --verbose[=<number>]    Be more verbose, print the SMTP session.
        --missing-modules-ok    Don't complain about missing optional modules.
        --version               Print: smtp-cli version 3.7
        --help                  Guess what is this option for ;-)

Chú thích

Thông thường mail được gởi từ một host cố định
(thí dụ smtp.gmail.com:587)
và từ một user cố định
(thí dụ postmaster@ly-le.info),
vì vậy có thể ghi sẵn vào smtp-cli để không phải ghi trên dòng lệnh

#$host = undef;
#$port = 'smtp(25)';
$host = 'smtp.gmail.com';
$port = 'smtp(587)';
$user = 'postmaster@ly-le.info';
$pass = 'password';
$from = '"RPI\'s Webmaster" <postmaster@ly-le.info>';

Câu lệnh gởi mail trở nên đơn giản hơn

./smtp-cli --verbose --to user1@domain.com --to user2@another.domain.org --data message-body.txt

Leave a Comment

Filed under Software

Leave a Reply