------------------------------------------------------------------------------- Using the SMTP protocol (Client Usage) ------------------------------------------------------------------------------- Notes... * Clients should check the error returns of commands before continuing, but for simple tests they often don't go that. (see "info/co-processing/general_hints.txt") * SMTP like most internet protocols requires a return-newline sequence for the end-of-line. However "sendmail", "exim", and "postfix" will accept just newline, but the Symantic mailsecure appliance, and "emailrelay" mail proxy require it. * Some SMTP servers insist on angled brackets for the envelope addresses. Anything outside the angle brackets is ignored as comment (as it should). ------------------------------------------------------------------------------- Using mailx (Redhat/Fedora machines) Generally (using the systems mail daemon)... echo "body of mail" | \ mailx -s test A.Thyssen@example.com Stop "mailx" reading RC files (which can modify behaviour) Use MAILRC envvar to stop ~/.mailrc and use -n to stop /etc/mail.rc Note: postfix mail server will send its verbose output by mail! echo "body of mail" | \ env MAILRC=/dev/null \ mailx -v -n -s test A.Thyssen@example.com Send directory to a SMTP server (from can also be set) This uses newer 'mail account' features of mailx... echo "body of mail" | \ env MAILRC=/dev/null \ smtp=smtp.example.com \ from=A.Thyssen@example.com \ mailx -v -n -s "mailx_smtp" A.Thyssen@example.com Other things you can do... Send encrypted (SMTPS) smtp=smtps://smtp.example.com \ smtp-use-starttls= # alturnative to previous nss-config-dir=/etc/pki/nssdb # issuer is not recognized.??? Send With Authentication smtp-auth-user=login \ smtp-auth-password=secret \ smtp-auth=login \ ------------------------------------------------------------------------------- swaks - Swiss Army Knife SMTP Transaction Tester Older Protocols, but still valid SMTP HELO on port 25 (unencrypted) The original protocol SMTPS HELO with tls on port 465 SSL with original protocol SSMTP EHLO with tls on port 465 SSL with extended protocol Normal Protocols (preferred) ESMTP EHLO on port 25 (unencrypted) Extended Protocol ESMTPS EHLO with tls on port 25 StartTLS extended protocol and ditto on port 587 (mail submition port) Normal Internal Mail Send (unencrypted ESMTP on port 25) swaks --to user@example.com \ --from user@example.com \ --add-header 'Subject: plain' \ --server smtp.example.com Encrypted Internal Mail Send (Start TLS on port 25) Certificates is needed if "tls_verify_hosts" set in EXIM config swaks --to user@example.com \ --from user@example.com \ --add-header 'Subject: encrypted' \ --server smtp.example.com --protocol ESMTPS # --tls-cert client_keys/client.crt --tls-key client_keys/client.key Ditto on port 587 swaks --to user@example.com \ --from user@example.com \ --add-header 'Subject: encrypted port 587' \ --server smtp.example.com:587 --protocol ESMTPS # --tls-cert client_keys/client.crt --tls-key client_keys/client.key Direct TLS on port 465 swaks --to user@example.com \ --from user@example.com \ --add-header 'Subject: Port 465' \ --server smtp.example.com --protocol SSMTP # --tls-cert client_keys/client.crt --tls-key client_keys/client.key ------------------------------------------------------------------------------- Using curl to send mail! curl -v "smtps://smtp.example.com:465" \ --mail-from "user@example.com" \ --mail-rcpt "user@example.com" \ --upload-file <( printf 'Subject: curl test\n\ntesting curl mail\n';) # --user "$USER:$PASS" ------------------------------------------------------------------------------- Check the Server Certificates for SMTPS (port 465) openssl s_client -connect smtp.example.com:465 | RCPT TO: | DATA | From: SMTP_TEST@nowhere.important | To: no-one-really@nowhere.important | Date: '"`date +'%a, %d %b %Y %T %z'`"' | Subject: Test from '"`hostname -s`"' | | This is a test mail from '"`whoami`"' | | If you see this mail report it to {your email address} | | . | QUIT ' | perl -ne '$|=1; s/^\s*\| ?// || next; print;' } Now we can send that to some SMTP service (typically localhost:25)... WARNING: Some mail server are strict about the CR-LF end-of-line handling this can be fixed by using "socat", or "netcat" (or "nc") With some timing delay after sending each line (prefered) generate_mail | perl -ne '$|=1; print stderr "> $_"; print; select(undef,undef,undef,0.2);' | socat - tcp:smtp.example.com:smtp,crlf # "netcat" alternative to using "socat" above #netcat --crlf smtp.example.com smtp Add the return-linefeed to output, for use with "tcp_client" or "telnet" perl -ne '$|=1; s/^\s*\| ?// || next; print stderr "> $_"; s/\n$/\r\n/; print; select(undef,undef,undef,0.5); # sleep 1/2 sec' | tcp_client smtp.example.com:smtp #telnet smtp.example.com smtp Use sed to add the return-newline (newer bash "\r" syntax) perl -ne '....' | sed "s/\$/\r/" | tcp_client smtp.example.com:smtp #telnet smtp.example.com smtp Or all together... echo ' | EHLO '"`hostname -s`"' | MAIL FROM: | RCPT TO: A.Thyssen@example.com | DATA | From: nobody@localhost | To: A.Thyssen@example.com | Subject: Testing 1 2 3 | | Testing 1 2 3 | . | QUIT ' | perl -ne '$|=1; s/^\s*\| ?// || next; print stderr "> $_"; print; select(undef,undef,undef,0.2);' | socat - tcp:smtp.example.com:smtp,crlf Symantec Messaing Gateway The autoflush and extra pauses is vital on these servers, otherwise it gives a "421 esmtp: protocol deviation" error due to commands being given before it responds. NOTE: This will not work as our mailsecure only accept mail from the internal SMTP servers, or my workstation (fix settings). generate_mail | perl -ne 'BEGIN{select((select(STDOUT),$|=1)[0]);sleep 1;}; print stderr "> $_"; print; select(undef,undef,undef,0.2); END{sleep 1;}; ' | socat - tcp:smtp-gateway.example.com:smtp,crlf ------------------------------------------------------------------------------- SMTP Proxy "EmailRelay" EMailRelay - Mail Store, Forward, POP, Proxy, Simple filter emailrelay --as-proxy smtp.example.com:25 --port 4000 --immediate Now you can sendmail to port 4000 and it will relay to the given SMTP server. Any errors by the SMTP server is passed back to the original sender. But you can also specify a filter to modify the mail header or body during the proxy forwarding operation, as long as it does nto take too long. If it takes some time, a store-then-forward mail proxy may be needed. -------------------------------------------------------------------------------