使用Docker容器中的GNU Mailutils发送邮件

我创build了一个docker镜像,在这里我使用以下命令安装mailutils包:

RUN apt-get update && apt-get install -y mailutils 

作为示例命令,我正在运行:

 mail -s 'Hello World' {email-address} <<< 'Message body' 

当我在本地机器上执行相同的命令时,它发送邮件。 但是,在docker容器中,它没有显示任何错误,但是在指定的电子邮件ID上没有收到邮件。

我尝试使用--net=host参数在产卵我的docker集装箱。

以下是我的docker命令:

 docker run --net=host -p 0.0.0.0:8000:8000 {imageName}:{tagName} {arguments} 

有什么我失踪? 有人能解释这个问题背后的networking概念吗?

安装ssmtp并configuration发送所有邮件到你的relayhost。

https://wiki.debian.org/sSMTP

感谢您的回复@pilasguru。 ssmtp适用于从docker集装箱内发送邮件。

为了使反应更加冗长,以下是需要做的事情。

  • 在容器中安装ssmtp。 你可以通过下面的命令来做到这一点。

    RUN apt-get update && apt-get -y install ssmtp

  • 您可以在/etc/ssmtp/ssmtp.confconfigurationssmtp的configuration

    理想的configuration。

`

 # # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root={root-name} # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub={smtp-server} # Where will the mail seem to come from? rewriteDomain={domain-name} # The full hostname hostname=c67fcdc6361d # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES 

`

  • 您可以直接从您的Docker镜像的根目录复制该目录。 例如。 您将您的configuration保存在名为my.conf文件中。

    您可以使用以下命令将它们复制到docker容器中:

    COPY ./my.conf /etc/ssmtp/ssmtp.conf

  • 使用简单的命令发送邮件,例如:

    ssmtp recipient_name@gmail.com < filename.txt

  • 您甚至可以发送附件,指定使用以下命令:

    echo -e "to: {to-addr}\nFrom: {from-addr}\nsubject: {subject}\n"| (cat - && uuencode /path/to/file/inside/container {attachment-name-in mail}) | ssmtp recipient_name@gmail.com

  • uuencode可以通过apt-get install sharutils命令apt-get install sharutils