简体   繁体   English

我如何使用 sendgrid API 和 symfony 4

[英]How do i use the sendgrid API with symfony 4

I need help to embed sendgrid in my symfony 4 project.我需要帮助将 sendgrid 嵌入到我的 symfony 4 项目中。 I installed the https://github.com/expertcoder/SwiftmailerSendGridBundle to use the api, correctly followed the instructions, got an API key from sendgrid我安装了https://github.com/expertcoder/SwiftmailerSendGridBundle以使用 api,正确按照说明操作,从 sendgrid 获得了 API 密钥

Here's the part in my.env file这是 my.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 ###

Is it correct?这是正确的吗? Thanks谢谢

This answer is not using the Swiftmailer but Symfony's new Mailer.这个答案不是使用 Swiftmailer,而是使用 Symfony 的新邮件程序。 Following the Symfony Docs you have to install按照Symfony Docs你必须安装

composer require symfony/mailer and composer require symfony/sendgrid-mailer composer require symfony/mailercomposer require symfony/sendgrid-mailer

then uncomment the new lines in the .env file and add your Sendgrid API key然后取消注释 .env 文件中的新行并添加您的 Sendgrid API 密钥

###> symfony/sendgrid-mailer ###
SENDGRID_KEY=YOUR-API-KEY
MAILER_DSN=smtp://$SENDGRID_KEY@sendgrid
###< symfony/sendgrid-mailer ###

(the MAILER_DSN uses the SENDGRID_KEY variable so do not set it twice) (MAILER_DSN 使用 SENDGRID_KEY 变量,所以不要设置两次)

Ideally you would add these lines to a .env.local file as well and remove the API-key from your .env file.理想情况下,您还将这些行添加到.env.local文件中,并从 .env 文件中删除 API 密钥。

finally:最后:

       $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);

Your string of connection has a problem, in user you must put apikey您的连接字符串有问题,您必须在用户中输入 apikey

Example:例子:

MAILER_URL=smtp://smtp.sendgrid.net:587?encryption=tls&auth_mode=login&username=apikey&password=SG.7RafsniRSAa-almQ.DV---------------------fN-9hMkExWhM9RHTVUMj9DE

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

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