【问题标题】:What do we need to configure, to send emails with Laravel?我们需要配置什么,才能使用 Laravel 发送电子邮件?
【发布时间】:2021-09-13 07:19:43
【问题描述】:

在 Node.js 中使用 Nodemailer 时,我们需要设置 SMTP 服务器,以便传递消息。例如,最常用的 SMTP 服务器被指定为here

我想知道的是,当使用 Laravel 发送电子邮件时,我们是否需要设置与 Node.js 相同的 SMPT 服务器?这是让我感到困惑的区别:

  • 使用 Node.js 时,我可以将包安装为库,然后我通常需要提供一个 api-key(我可以从 SMTP 服务器获取)。例如(对于SendInBlue)我们安装库,我们导入相应的包,我们指定api密钥,我们可以根据规范准备使用它
// Include the Sendinblue library\
var SibApiV3Sdk = require('sib-api-v3-sdk');
var defaultClient = SibApiV3Sdk.ApiClient.instance;
// Instantiate the client
var apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = 'YOUR_API_V3_KEY';
  • 在使用 Laravel 时,here 我发现它可以与 SwiftMailer 一起使用,并且整个设置看起来与 Node.js 方法有点不同。在这里,我看到定义了域、端口和协议,但我没有从上面看到 API 的密钥(另外我不确定域、端口和协议的值是从哪里获得的)
// Create the Transport
$transport = new Swift_SmtpTransport('smtp.example.org', 25);
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// or even some more advanced approaches

// Option #1: SMTPS = SMTP over TLS (always encrypted):
$transport = new Swift_SmtpTransport('smtp.example.org', 587, 'ssl');
// Option #2: SMTP with STARTTLS (best effort encryption):
$transport = new Swift_SmtpTransport('smtp.example.org', 587, 'tls');

那么当我们想通过 Laravel 使用邮件时,我们实际上需要什么?我们需要相同的 SMTP 服务器(例如,从我上面提供的列表中)还是 Laravel 以不同的方式工作并且我们需要其他东西来设置和获取配置值(如域、端口和协议)?

【问题讨论】:

  • 这里你可以看到在 Laravel 中使用SendGrid SMTP 发送邮件的例子

标签: php node.js laravel smtp swiftmailer


【解决方案1】:

您也可以在项目的 .env 文件中进行配置

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 2011-12-14
    • 2017-01-01
    • 2011-10-27
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多