2

I am trying to send email from a docker container. The simplest one-liner I've been able to see is from an earlier stack overflow answer:

docker run --rm --net="host" php:fpm-alpine sh -c 'echo "Subject: test" | sendmail -v your@mail.com'

I change your@mail.com to my gmail address and run the command. The problem is that I now get this output in my terminal:

sendmail: recv:'220 mail.example.com ESMTP ready'
sendmail: send:'EHLO linuxkit-025000000001'
sendmail: recv:'250 mail.example.com'
sendmail: linuxkit-025000000001: Host not found

What do I need to do differently in order to get a simple sample mail to come through?

I assume I'm close, since I'm getting output from sendmail.

  • because gmail use device allow as system security, you need allow devices in you account gmail – Soleil Mar 08 '19 at 18:20
  • This question appears to be incomplete since running the docker run command you reference as it is results in 'sendmail: can't connect to remote host (127.0.0.1): Connection refused'. Did you start a mail server using a different command? If so, how? – programmerq Mar 08 '19 at 18:49

1 Answers1

3

Sendmail is trying to relay your e-mail through an SMTP server located here: mail.example.com. This is the builtin default config for sendmail on this image.

What you want to do is to pass the google SMTP config for sendmail, like this:

echo "Subject: test" | sendmail -v -f your@mail.com -au<your-gmail-account> -ap<your-gmail-password> -H 'openssl s_client -quiet -tls1 -connect smtp.gmail.com:465' your@mail.com

A practical example for:

  • gmail account: your@gmail.com
  • gmail password: 123456

Would look like this:

echo "Subject: test" | sendmail -v -f your@mail.com -auyour@gmail.com -ap123456 -H 'openssl s_client -quiet -tls1 -connect smtp.gmail.com:465' your@mail.com

sendmail --help

For reference, here is the sendmail --help. You can get it from the image that you are using with:

docker run --rm php:fpm-alpine sendmail --help
BusyBox v1.29.3 (2019-01-24 07:45:07 UTC) multi-call binary.

Usage: sendmail [-tv] [-f SENDER] [-amLOGIN 4<user_pass.txt | -auUSER -apPASS]
                [-w SECS] [-H 'PROG ARGS' | -S HOST] [RECIPIENT_EMAIL]...

Read email from stdin and send it

Standard options:
        -t              Read additional recipients from message body
        -f SENDER       For use in MAIL FROM:<sender>. Can be empty string
                        Default: -auUSER, or username of current UID
        -o OPTIONS      Various options. -oi implied, others are ignored
        -i              -oi synonym, implied and ignored

Busybox specific options:
        -v              Verbose
        -w SECS         Network timeout
        -H 'PROG ARGS'  Run connection helper. Examples:
                openssl s_client -quiet -tls1 -starttls smtp -connect smtp.gmail.com:25
                openssl s_client -quiet -tls1 -connect smtp.gmail.com:465
                        $SMTP_ANTISPAM_DELAY: seconds to wait after helper connect
        -S HOST[:PORT]  Server (default $SMTPHOST or 127.0.0.1)
        -amLOGIN        Log in using AUTH LOGIN (-amCRAM-MD5 not supported)
        -auUSER         Username for AUTH
        -apPASS         Password for AUTH

If no -a options are given, authentication is not done.
If -amLOGIN is given but no -au/-ap, user/password is read from fd #4.
Other options are silently ignored; -oi is implied.
Use makemime to create emails with attachments.
Andreas Lorenzen
  • 3,810
  • 1
  • 24
  • 26