简体   繁体   English

如何正确配置MAIL_URL?

[英]How to properly configure MAIL_URL?

The follwoing smtp url giving me an error 以下smtp网址给我一个错误

process.env.MAIL_URL="smtp://mail_name@outlook.com:Password@smtp.outlook.com:457";

What am I doing wrong? 我究竟做错了什么?

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. 对于初学者,您的问题是您的用户名(可能还有您的密码)包含无法按原样放置在URL中的字符,因此需要进行编码。

I want to take this opportunity to provide a little more in-depth answer to the issue of configuring the MAIL_URL environment variable. 我想借此机会对配置MAIL_URL环境变量的问题提供更深入的解答。

If you simply need a quick string that will work, do: 如果您只需要一个可行的快速字符串,请执行以下操作:

process.env.MAIL_URL="smtp://"+encodeURIComponent("mail_name@outlook.com")+":"+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. 还要考虑到您可能需要使用smtps进行安全连接,如果它使用TLS,您的连接可能会失败。

I recommend to read the rest if you need anything more robust. 如果您需要更强大的功能,我建议您阅读其余内容。

URL 网址

A URL has the following structure: URL具有以下结构:

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. 该方案可以是smtpsmtps (用于安全连接),在这种情况下,您还将设置用户,密码,主机和(最可能)端口。

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. 每个部分都需要以适合在URL中使用的方式进行编码,但由于主机(域)通常已经合适,因此您只需确保对您的用户名/密码进行编码。

In EcmaScript, encodeURIComponent can be used for this. 在EcmaScript中, encodeURIComponent可用于此目的。

MAIL_URL and node environment variables MAIL_URL和节点环境变量

Meteor checks the value of process.env.MAIL_URL when sending an email. Meteor在发送电子邮件时会检查process.env.MAIL_URL的值。

process.env is populated by node.js with the environment variables available to it on startup. process.env由node.js填充,启动时可用的环境变量。

It is possible to add properties to it on runtime, so setting process.env.MAIL_URL before sending an email will work. 可以在运行时向其添加属性,因此在发送电子邮件之前设置process.env.MAIL_URL将起作用。 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. 我建议使用settings.json或使用环境变量本身来settings.json 2种方法。

using settings.json 使用settings.json

Create a json file in your project. 在项目中创建一个json文件。 It is highly recommended not to commit it into source control with the rest of your code. 强烈建议不要使用其余代码将其提交到源代码管理中。

for example: config/development/settings.json 例如: 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. 然后你可以使用--settings开关运行Meteor。

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 ): 如果你想要一个实用程序脚本(对于* nix上的zsh)将它转换它(取决于node ):

mail_url.sh 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
$ MAIL_USER=foo@bar.baz
$ 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%4015766w0rd@smtp.gmail.com:465

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM