简体   繁体   English

带有swiftmailer和Symfony 2的电子邮件标题

[英]Emails headers with swiftmailer and Symfony 2

I asked a similar question earlier, but the context has changed a bit. 之前我问过类似的问题,但是上下文有所变化。 I want to use swiftmailer to send an email in Symfony 2. The problem is I'm using the gmail SMTP server, and so when my message arrives, my email client shows it from gmail.com, rather than mydomain.com. 我想使用swiftmailer在Symfony 2中发送电子邮件。问题是我正在使用gmail SMTP服务器,因此,当我收到消息时,我的电子邮件客户端会从gmail.com而不是mydomain.com显示它。 How can I fix this? 我怎样才能解决这个问题?

$message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom(array('digest@mydomain.com' => "Digest"))
        ->setSender(array('digest@mydomain.com' => "Digest"))
        ->setCharset('iso-8859-1')
        ->setContentType('text/html')
        ->setTo('myemail@gmail.com')
        ->setBody($this->renderView('email.html.twig', array()));

$this->get('mailer')->send($message);

The Gmail SMTP server only allows you to send mail using the email address you're aithenticating with. Gmail SMTP服务器仅允许您使用要使用的电子邮件地址发送邮件。 So if you configured Symfony with 因此,如果您将Symfony配置为

mailer_transport: gmail
mailer_user: myemail@gmail.com
mailer_password: ******

All your messages we'll look like they're from myemail@gmail.com myemail@gmail.com所有邮件看起来都来自myemail@gmail.com

What you can do maybe is use another smtp server that is more flexible in the types of From addresses they will allow. 您可能要做的是使用另一台smtp服务器,该服务器在允许的“发件人”地址类型上更为灵活。 If you are on a local development server, you could use your ISP's SMTP server, or use one of the various email as a service provider, such as Sendgrid , Mailjet or postmarkapp 如果你是一个本地开发服务器上,你可以使用你的ISP的SMTP服务器,或使用各种电子邮件中的一个作为服务提供商,如SendgridMailjetpostmarkapp

They all allow you to send messages from any address you can prove ownership of, after some configuration steps. 通过一些配置步骤,它们都允许您从可以证明拥有所有权的任何地址发送消息。

One thing that may be worth trying also if you really want to use the gmail smtp server, is to define your from address in your "Send mail as" Gmail configuration (in Settings -> Accounts). 如果您确实要使用gmail smtp服务器,那么可能还需要尝试的一件事是在“发送邮件为” Gmail配置中(在“设置”->“帐户”中)定义发件人地址。 This will let your app send email using the configured adresses in the From: field. 这将使您的应用使用“ From:字段中配置的地址发送电子邮件。

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

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