【问题标题】:Override default mailer URL in Symfony覆盖 Symfony 中的默认邮件程序 URL
【发布时间】:2020-02-27 15:09:46
【问题描述】:

我试图让用户可以在 Symfony 中向我发送电子邮件,我可以向自己发送电子邮件,但我无法从其他电子邮件中获取电子邮件,因为 .env 文件中给出的默认电子邮件地址是不会被覆盖。

  /**
 * @Route("/send_msg", name="send_msg", methods={"GET","POST"})
 */
public function index(Request $request, \Swift_Mailer $mailer)
{

  $name =  $request->query->get('name');
  $mail =  $request->query->get('mail');
  $msg =  $request->query->get('msg');


    $message = (new \Swift_Message('Client'))
        ->setFrom($mail)
        ->setTo('my_gmail@gmail.com')
        ->setBody($msg)

    ;

    $mailer->send($message);
    return new JsonResponse();

}

这是 .env 文件

###> symfony/mailer ###
# MAILER_DSN=smtp://localhost
###< symfony/mailer ###

 # For Gmail as a transport, use: "gmail://username:password@localhost"
 # For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
 # Delivery is disabled by default via "null://localhost"
  MAILER_URL=gmail://my_gmail@gmail.com:my_pass@localhost
 ###< symfony/swiftmailer-bundle ###

    ###> symfony/sendgrid-mailer ###
    #MAILER_DSN=sendgrid://KEY@default
    ###< symfony/sendgrid-mailer ###

【问题讨论】:

    标签: php symfony gmail swiftmailer


    【解决方案1】:

    您将 Gmail 凭据用作 SMTP 服务器。您不能也不应该通过您的 Gmail 凭据从任何其他电子邮件地址发送邮件。即使您使用允许任何发件人地址的另一个 SMTP 服务器,您也不应该使用未在您的 SMTP 服务中进行身份验证的电子邮件地址作为发件人地址,因为您的电子邮件可能永远不会看到收件箱。

    一个选项是使用$message-&gt;setReplyTo($mail); 将回复地址设置为您用户的电子邮件地址。这样,如果您点击邮箱中的回复按钮,邮件应该会发送到用户的电子邮件地址。

    【讨论】:

    • 但是我想接收别人的邮件而不是发送或回复他们..这不是我想要的
    • 使用 setReply 好像是这样,然后当你点击回答按钮时,你会回答谁需要
    • @AsMaBoudjemaa 正如我所说,在技术上可以使用任何电子邮件地址作为发件人地址。但是您必须使用 Gmail 以外的其他 SMTP 服务。 Gmail 不允许您从您自己的 Gmail 地址以外的任何其他电子邮件地址发送邮件。但是,如果您使用其他 SMTP 服务,这些邮件很可能会作为垃圾邮件被丢弃,因为您无权将用户的电子邮件地址用作发件人地址。这就是我建议使用回复的原因,即使它并不完全是你想要的,但在我看来它是最好的选择。
    • @user1026090 这就是我所做的,感谢您的建议和帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多