How to get sendmail working in Alpine Docker container?
Asked Answered
E

6

18

I just want to send simple email for testing purposes, but when executing sendmail inside the container, I get sendmail: can't connect to remote host (127.0.0.1): Connection refused. What do I need to take into account when using sendmail inside Alpine container?

Empedocles answered 4/8, 2017 at 2:48 Comment(0)
A
5

In a container there will be no running MTA (mail transport agent), simply there is no mail server running. For example, you can use ssmtp (https://pkgs.alpinelinux.org/package/edge/main/x86/ssmtp) and configure it to use an outside, for example your providers, smtp-server (eg. mail.yourprovider.com).

Than you can use ssmtp to send email. Here is a similar case where sending mail from inside the php Docker image is not possible. Here you can read further -> https://github.com/docker-library/php/issues/135

Aruwimi answered 4/8, 2017 at 4:53 Comment(1)
Thanks for the tip, but how do I configure it? I don't really have much expertise on mail servers. In AWS Linux instance everything just worked without having to change any default settings, so I was looking for the simplest possible solution.Empedocles
N
5

If you use the BusyBox sendmail, you can set the configuration in the php.ini file.

BusyBox sendmail relays to an SMTP server. You can specify the server on the command line, with the -S option.

My configuration in php.ini is:

sendmail_path = /usr/sbin/sendmail -S 172.16.239.1 -t -i

I'm using an SMTP server in another container.

https://technote.fyi/code/sysadmin/docker/configuring-alpine-linux-on-docker-to-send-mail-for-wordpress/

Novelist answered 7/12, 2019 at 20:38 Comment(0)
C
2

Set the SMTPHOST environment variable to the hostname or IP of your smtp server. The code to use this variable exist since since 2008-09-27 in the busybox sendmail commit (https://git.busybox.net/busybox/commit/?id=bed22a01fb19de6e4b4c2c7d8c5953bc7aa2580e) but the usage page was only updated on 2016-07-14 with the commit https://git.busybox.net/busybox/commit/?id=9de7509aa013a8634b13a29008cd58ca971c9c7c. Please note that the online documentation page doesn't reflect these changeshttps://www.busybox.net/downloads/BusyBox.html. Use sendmail --help to see the corrected usage.

Cradling answered 20/4, 2020 at 12:16 Comment(0)
D
1

Adding to earlier answer of m4r10k.

  1. Install ssmtp in the docker file of the container.
RUN apk update
RUN apk add ssmtp
  1. Create a configuration file on the host
root=postmaster
mailhub=mail.privateemail.com:587
FromLineOverride=YES
rewriteDomain=<YouDomain.com>
AuthUser=<[email protected]
AuthPass=<YouUserPass>
UseSTARTTLS=YES
hostname=YourDomain.com

Use a volume when running the container to use the ssmtp.conf

See a full example here using docker-compose and SMTP from NameCheap.

Drawtube answered 29/8, 2020 at 18:3 Comment(0)
Y
0

This is my setup using mailhog as smtp:

docker-compose:

version: "3"

services:
  drupal:
    build: .
    container_name: ${PROJECT_NAME}-drupal
    depends_on:
      - mysql
    restart: unless-stopped
    networks:
      - internal
    volumes:
      - ./docroot:/var/www/html
      - ./config:/home/config
  mailhog:
    image: mailhog/mailhog:latest
    container_name: ${PROJECT_NAME}-mailhog
    ports:
      - "1025:1025"
      - "8025:8025"
    restart: unless-stopped
    networks:
      - internal

Don't forget to share the same network between the php and the mailhog containers.

Inside the Dockerfile install mhsendmail and change its binary mode to 0755, making it executable, Finally add the mhsendmail configuration into PHP:

Dockerfile php:7.4-fpm-alpine3.14:

RUN curl -LkSso /usr/bin/mhsendmail 'https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64'&& \
    chmod 0755 /usr/bin/mhsendmail && \
    echo 'sendmail_path = "/usr/bin/mhsendmail --from=nobody@7bd822a2c191 --smtp-addr=mailhog:1025"' >> /usr/local/etc/php/php.ini;

Now send an email from inside the php container to test the configuration:

$ php -r "mail('[email protected]', 'test subject', 'test body');"

Source: https://github.com/mailhog/MailHog/issues/254

Yuzik answered 1/1, 2022 at 8:44 Comment(0)
M
-2

This is a request to user kitanotori: please change the title to something like How to send mail from inside an Alpine Linux container rather than using to word sendmail (and please remove this tag, too) since this will very likely mislead people who are looking how to run the open source MTA Sendmail in an Alpine Linux container, which IMHO definitely makes sense - contrary to the opinion of the user m4r10k.

Note: i ask for pardon if this is not the right way to mention this, but with my current site reputation as a less-than-a-week-as-an-active-member i don't see any other way to address this concerns.

ps: i set this article on my personal ToDo list, and once i've credits enough to comment the question, i'll do it.

edit: promise added

Maricela answered 27/5, 2023 at 23:4 Comment(2)
Hi, as you stated, this is not the right way to mention this. You must use comments for such things. Once you get enough rep you can comment everywhere. We all have started from 1, so do your best to quickly reach that threshold! Remeber, you can also suggest an edit.Bigotry
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewBigotry

© 2022 - 2024 — McMap. All rights reserved.