forhadmethun
forhadmethun

Reputation: 599

How to properly configure MAIL_URL?

The follwoing smtp url giving me an error

process.env.MAIL_URL="smtp://[email protected]:[email protected]:457";

What am I doing wrong?

Upvotes: 1

Views: 1772

Answers (1)

MasterAM
MasterAM

Reputation: 16488

For starters, your issue is that your user name (and perhaps your password) contain a character that cannot be placed in a URL as-is, and therefore needs to be encoded.

I want to take this opportunity to provide a little more in-depth answer to the issue of configuring the MAIL_URL environment variable.

If you simply need a quick string that will work, do:

process.env.MAIL_URL="smtp://"+encodeURIComponent("[email protected]")+":"+encodeURIComponent("Password")+"@smtp.outlook.com:457";

Also take into account that you may need to use smtps for secure connection, and if it uses TLS, your connection may fail.

I recommend to read the rest if you need anything more robust.

URL

A URL has the following structure:

scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]

The scheme would be either smtp or smtps (for secure connection), and in this scenario you will also set the user, password, host and (most likely) port.

Each of the parts needs to be encoded in a way that is suitable for use in a URL, but since hosts (domains) are normally already appropriate, you only need to make sure that your user name/password are encoded.

In EcmaScript, encodeURIComponent can be used for this.

MAIL_URL and node environment variables

Meteor checks the value of process.env.MAIL_URL when sending an email.

process.env is populated by node.js with the environment variables available to it on startup.

It is possible to add properties to it on runtime, so setting process.env.MAIL_URL before sending an email will work. However, you should do so wisely to prevent your secrets from leaking.

I would suggest 2 methods for setting it up, either using settings.json or using the environment variable itself.

using settings.json

Create a json file in your project. It is highly recommended not to commit it into source control with the rest of your code.

for example: config/development/settings.json

{
  "smtp": {
    "isSecure": true,
    "userName": "your_username",
    "password": "your_password",
    "host": "smtp.gmail.com",
    "port": 465
  }
}

And somewhere in your server code:

Meteor.startup(function() {
  if (Meteor.settings && Meteor.settings.smtp) {
    const { userName, password, host, port, isSecure } = Meteor.settings.smtp;
    const scheme = isSecure ? 'smtps' : 'smtp';
    process.env.MAIL_URL = `${scheme}://${encodeURIComponent(userName)}:${encodeURIComponent(password)}@${host}:${port}`;
  }
});

Then you can run Meteor with the --settings switch.

meteor run --settings config/development/settings.json

using an environment variable

You can set the environment variable to the encoded string. If you want a utility script (for zsh on *nix) that will convert it (depends on node):

mail_url.sh

#!/bin/zsh

alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'

ENC_USER=`urlencode $2`
ENC_PASS=`urlencode $3`

MAIL_URL="$1://$ENC_USER:$ENC_PASS@$4"

echo $MAIL_URL

which can be used as follows:

$ chmod +x mail_url.sh
$ MAIL_SCHEME=smtps
$ [email protected]
$ MAIL_PASSWORD=p@$$w0rd
$ MAIL_HOST=smtp.gmail.com:465
$ export MAIL_URL=$(./mail_url.sh $MAIL_SCHEME $MAIL_USER $MAIL_PASSWORD $MAIL_HOST)
$ echo $MAIL_URL
smtps://foo%40bar.baz:p%[email protected]:465

Upvotes: 4

Related Questions