取“setFrom"名称与使用 Swift Mailer 的 Gmail 配置相同 [英] Take "setFrom" name as in Gmail configuration using Swift Mailer

查看:20
本文介绍了取“setFrom"名称与使用 Swift Mailer 的 Gmail 配置相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony 项目中使用 Swiftmailer 发送电子邮件.

I'm using Swiftmailer to send emails in Symfony project.

$msg = (new \Swift_Message("Subject"))
            ->setFrom('xxx123@gmail.com')
            ->setTo('yyy@gmail.com')
            ->setBody($mailContent);

在收到的电子邮件中,发件人的名称看起来像 xxx123.但此电子邮件 ID 在其配置中的名称是 XXX YYY.

In the received email, the name of Sender looks like xxx123. But the name for this email id in its configuration is XXX YYY.

我可以使用,

->setFrom('xxx123@gmail.com' => 'XXX YYY')

但是,我不想直接在代码中设置名称,因为它可能会更改为其他内容,例如 YYY ZZZ.

but, I don't want to set name directly in the code as it may be changed to something else like YYY ZZZ.

我怎样才能做到这一点?提前致谢!!

How can I achieve this? Thanks in advance!!

推荐答案

发件人名称始终设置在每条消息的标题中.所以标题可以由任何电子邮件客户端设置.在这种情况下,您的 SwiftMailer 也是电子邮件客户端.

Sender name is always set in the header of every message. So header can be set by any email client. Your SwiftMailer is also email client in this case.

您的电子邮件服务器可以重写标题,但仅限于特定情况.像 Gmail 一样,将您未在帐户设置中验证的所有电子邮件地址重写为您的基本电子邮件.

Your email server can rewrite header but only in specific cases. Like Gmail rewrite all email addresses that you didn't validate in the account settings to your base email.

如果您需要从 Gmail 帐户中获取您的发件人姓名,您可以使用 Gmail-API.使用 OAuth 验证您的应用程序并向 url 发送 GET 请求:

If you need to fetch your sender name from Gmail account you can use Gmail-API. Authenticate your application with OAuth and send GET request to url:

https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/YOUR_EMAIL_ADDRESS

您将收到这样的 JSON:

You will receive JSON like this:

{
 "sendAsEmail": "YOUR_EMAIL_ADDRESS",
 "displayName": "SENDER NAME THAT YOU NEED",
 "replyToAddress": "",
 "signature": "Your standard signature for emails created by Gmail in browser",
 "isPrimary": true,
 "isDefault": true
}

有关此请求的 Google API 的更多信息,您可以在此处找到:https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/get

More about Google API for this request you can find here: https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/get

这篇关于取“setFrom"名称与使用 Swift Mailer 的 Gmail 配置相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆