mailhog / MailHog

Web and API based SMTP testing
MIT License
13.93k stars 1.06k forks source link

Unable to pre-load OutgoingSMTP values for use in Release #248

Open jlellis opened 5 years ago

jlellis commented 5 years ago

I'm trying to provide my developers with a pre-defined 'Release' smtp target by providing MailHog with a -outgoing-smtp JSON file. I've fiddled with this for several hours. I can see the entry appearing in the Release selection box in the web gui, but using it always fails.

When running MailHog from the command line in the foreground (or with a systemd service defintion that routes stdout to the syslog) I always get log output like this:

Jun 12 18:42:43 mhog-cmi-001 env[94281]: {Name:mhog-cmi-001.pavlovmedia.corp Save:false Email:jlellis@pavlovmedia.com Host: Port: Username: Password: Mechanism:}
Jun 12 18:42:43 mhog-cmi-001 env[94281]: Got message: oTaHcdh5LHT0BvCJf-dzI4uOT02EwTm75wONgrd09_E=@mhog-cmi-001.pavlovmedia.corp
Jun 12 18:42:43 mhog-cmi-001 env[94281]: Server not found: mhog-cmi-001.pavlovmedia.corp

So it seems to be correctly saving the NAME of the outbound server, but not the host/port content, it just fails.

If I manually create a Custom setting in the web gui with the same values, it works, and logs thusly:

Jun 12 18:44:04 mhog-cmi-001 env[94281]: {Name:grrr Save:true Email:jlellis@pavlovmedia.com Host:localhost Port:25 Username: Password: Mechanism:NONE}
Jun 12 18:44:04 mhog-cmi-001 env[94281]: Got message: oTaHcdh5LHT0BvCJf-dzI4uOT02EwTm75wONgrd09_E=@mhog-cmi-001.pavlovmedia.corp
Jun 12 18:44:04 mhog-cmi-001 env[94281]: Saved server with name grrr
Jun 12 18:44:04 mhog-cmi-001 env[94281]: Using server with name: grrr
Jun 12 18:44:04 mhog-cmi-001 env[94281]: Releasing to jlellis@pavlovmedia.com (via localhost:25)

I've passed the json file through jq as a syntax check and it worked fine.

The current content of that file is:

{
    "server": {
        "name": "mhog-cmi-001.pavlovmedia.corp",
        "host": "mhog-cmi-001.pavlovmedia.corp",
        "port": "25",
        "mechanism": "NONE"
    }
}

I've spent several hours trying various things in this file and I've reached my limit of patience with it. I have googled multiple times looking for anyone posting a working example of this file with no luck at all.

I just want to have the Release page provide a known-working default config for the developers to use. From what I can tell, the go code is simply ignoring the Host and Port items in this file no matter how I provide it.

ns3777k commented 5 years ago

@jlellis

due to the reference your config is a little ambiguous and the configuration format itself is a little redundant.

try this:

{
    "mhog-cmi-001.pavlovmedia.corp": {
        "name": "mhog-cmi-001.pavlovmedia.corp",
        "host": "mhog-cmi-001.pavlovmedia.corp",
        "port": "25",
        "mechanism": "NONE"
    }
}

or just change the json object key and name property to the same value.

adriankirchner commented 5 years ago

That fixed it for me. Thank you @ns3777k

simonschaufi commented 4 years ago

Could you explain how you set the environment variable? I put this in my ~/.profile file:

export MH_OUTGOING_SMTP="$HOME/.mailhog/outgoing-smtp.json"

but mailhog is not picking up this value (the dropdown list is empty)

My second try was adding the path in the file /etc/systemd/system/mailhog.service:

ExecStart=/usr/bin/env /usr/local/bin/mailhog -outgoing-smtp="/home/user/.mailhog/outgoing-smtp.json" > /dev/null 2>&1 &

and running the following commands:

systemctl daemon-reload
systemctl enable mailhog

but it's still not working.

It only works when I run mailhog manually with the file path attached like so:

mailhog -outgoing-smtp="/home/user/.mailhog/outgoing-smtp.json"

I'm totally confused...

ns3777k commented 4 years ago

@simonschaufi Well, I assume you're using linux. First off, let's make sure the basic example works correct:

$ MH_OUTGOING_SMTP="/home/user/.mailhog/outgoing-smtp.json" mailhog

mailhog should pick this up.

simonschaufi commented 4 years ago

@ns3777k yes, that works

ns3777k commented 4 years ago

@simonschaufi good. what's the final goal? you wanna start it as a systemd service?

simonschaufi commented 4 years ago

Exactly. Do I need to place the environment in .bashrc or is .profile correct? I'm using Ubuntu 18.

When i execute this command: echo $MH_OUTGOING_SMTP I get: /home/user/.mailhog/outgoing-smtp.json so the environment variable is available in Terminal

ns3777k commented 4 years ago

@simonschaufi Should be working fine specifying the systemd service without env variables.

Service /etc/systemd/system/mailhog.service:

[Unit]
Description=MailHog Service
[Service]
Type=simple
ExecStart=/usr/bin/env /tmp/MailHog_linux_amd64 -outgoing-smtp=/tmp/servers.json > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target

Then:

$ sudo systemctl daemon-reload
$ sudo systemctl start mailhog
$ sudo systemctl status mailhog

● mailhog.service - MailHog Service
   Loaded: loaded (/etc/systemd/system/mailhog.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-02-18 00:24:24 MSK; 4s ago
 Main PID: 16823 (MailHog_linux_a)
    Tasks: 6 (limit: 4915)
   Memory: 2.1M
   CGroup: /system.slice/mailhog.service
           └─16823 /tmp/MailHog_linux_amd64 -outgoing-smtp=/tmp/servers.json > /dev/null 2>&1 &

With /tmp/servers.json:

{
    "mhog-cmi-001.pavlovmedia.corp": {
        "name": "mhog-cmi-001.pavlovmedia.corp",
        "host": "mhog-cmi-001.pavlovmedia.corp",
        "port": "25",
        "mechanism": "NONE"
    }
}
simonschaufi commented 4 years ago

@ns3777k can you verify that it works on your machine also with the environment variable? I don't want to touch "internal" files to have a clean system that I can easily setup again.

ns3777k commented 4 years ago

@simonschaufi yes, it does. if you wanna use env variables with systemd, you should put the env inside the service definition:

[Unit]
Description=MailHog Service
[Service]
Environment="MH_OUTGOING_SMTP=/tmp/servers.json"
Type=simple
ExecStart=/usr/bin/env /tmp/MailHog_linux_amd64 > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target

then daemon reload and restart

simonschaufi commented 4 years ago

So the daemon doesn't have access to the variables I declared in my ~/.profile?

ns3777k commented 4 years ago

Well, I would not recommend setting systemd service and pointing it to a file inside a user home directory. Technically it's possible with setting EnvironmentFile in the service definition (https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=).

Instead I'd recommend setting the environment variables in a separate file outside the home directory or specifying arguments in ExecStart.

campbellmc commented 4 years ago

For the original issue here - perhaps the documentation needs a little clue for this? A line below the example JSON would be useful, viz:

Ensure the name parameter and the key for the entire server block match exactly (both shown as server name in the above example.)

Some other things to note which may be useful for people who get stuck:

The email field is ambiguous. Is it the From Email or the To Email. It seems to be neither.

In the password field - if you are using with an SMTP API such as Sengrid - the password (aka API secret) provided by Sendgrid may contain backslashes. This will create a valid JSON file, however, this won't work with Mailhog. Regenerate the API key/secret on Sendgrid until a secret without backslashes in it is obtained.