【问题标题】:How do i use the sendgrid API with symfony 4我如何在 symfony 4 中使用 sendgrid API
【发布时间】:2019-05-20 14:04:28
【问题描述】:
我需要帮助将 sendgrid 嵌入到我的 symfony 4 项目中。
我安装了https://github.com/expertcoder/SwiftmailerSendGridBundle 来使用api,正确地按照说明进行操作,从sendgrid 获得了一个API 密钥
这是我的 .env 文件中的部分
MAILER_URL=smtp://smtp.sendgrid.net:587?encryption=tls&auth_mode=login&username=%SENDGRID_API_KEY%&password=%SENDGRID_API_KEY%
###> expertcoder/swiftmailer-send-grid-bundle ###
SENDGRID_API_KEY= MyAPiKey
###< expertcoder/swiftmailer-send-grid-bundle ###
正确吗?谢谢
【问题讨论】:
标签:
symfony4
sendgrid
sendgrid-api-v3
【解决方案1】:
这个答案不是使用 Swiftmailer,而是 Symfony 的新 Mailer。跟随Symfony Docs你必须安装
composer require symfony/mailer
和composer require symfony/sendgrid-mailer
然后取消注释 .env 文件中的新行并添加您的 Sendgrid API 密钥
###> symfony/sendgrid-mailer ###
SENDGRID_KEY=YOUR-API-KEY
MAILER_DSN=smtp://$SENDGRID_KEY@sendgrid
###< symfony/sendgrid-mailer ###
(MAILER_DSN 使用 SENDGRID_KEY 变量,所以不要设置两次)
理想情况下,您也可以将这些行添加到 .env.local 文件中,并从 .env 文件中删除 API 密钥。
终于:
$email = (new Email())
->from('hello@example.com')
->to('you@example.com')
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH)
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
$mailer->send($email);