php 使用 gmail smtp 服务器时如何更改发件人地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1332510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change from-address when using gmail smtp server
提问by omg
I want to send an email from Ato B,with HEADERand CONTENTthrough gmail.
我想通过 gmail从A向B发送一封带有HEADER和CONTENT的电子邮件。
How to do that by PHP?
如何通过 PHP 做到这一点?
I've specified the FROM([email protected]),but when I receive the email,it's still from my gmail account([email protected]).
我已经指定了发件人([email protected]),但是当我收到电子邮件时,它仍然来自我的 gmail 帐户([email protected])。

(source: maishudi.com)

(来源:麦舒地网)
$mail->From = "[email protected]";
$mail->FromName = "Mailer";
$mail->AddAddress("[email protected]", "Josh Adams");// name is optional
$mail->AddReplyTo("[email protected]", "Information");
How do I change the FROMpart?
如何更改FROM部分?
回答by Ian
The short answer - you can't.
简短的回答 - 你不能。
Google rewrites the From and Reply-To headers in messages you send via it's SMTP service to values which relate to your gmail account.
Google 将您通过其 SMTP 服务发送的邮件中的 From 和 Reply-To 标头重写为与您的 gmail 帐户相关的值。
The SMTP feature of gmail isn't intended to be an open or relay service. If it allowed any values for the From header, it would significantly dilute Google's standing with spam services, as there would be no way to verify the credentials of the sender.
gmail 的 SMTP 功能并非旨在成为开放或中继服务。如果它允许 From 标头的任何值,它将显着削弱 Google 在垃圾邮件服务方面的地位,因为将无法验证发件人的凭据。
You need to consider alternatives. How are you planning to host your script/application/website when it's finished: virtually every hosting solutions (shared/vps/dedicated server) will come pre-configured with an email transfer solution: be it sendmail or postfix on *nix, or IIS on Windows.
您需要考虑替代方案。您打算如何在完成后托管您的脚本/应用程序/网站:几乎所有托管解决方案(共享/vps/专用服务器)都将预先配置电子邮件传输解决方案:无论是 *nix 上的 sendmail 或 postfix,还是 IIS在 Windows 上。
If you are intent on using gmail then you could:
如果您打算使用 gmail,那么您可以:
- Setup a dedicated "[email protected]" account
- If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a "[email protected]" account.
- 设置专用的“[email protected]”帐户
- 如果您拥有应该发送的域,请使用域的免费 gmail,并设置一个“[email protected]”帐户。
====
====
Edit June 2015
2015 年 6 月编辑
It was suggested that GMail does allow sending via different addresses. As far as I can tell, this is for sending via the GMail wep app, and utilises your existing external SMTP server, which is not relevant to the original question.
有人建议 GMail 确实允许通过不同的地址发送。据我所知,这是通过 GMail wep 应用程序发送的,并利用您现有的外部 SMTP 服务器,这与原始问题无关。
====
====
Edit Nov 2013
2013 年 11 月编辑
Seeing as this is still getting a trickle of votes. A quick update.
看来这仍然是一点点的选票。快速更新。
Google have withdrawn their free GMail for domains. There are plenty of other free services around. One of note is Mandrill- a one-to-one email service intended for transactional emails (e.g. ecommerce orders etc.). It's ran by MailChimp, who pretty much know all there is to know about sending email at volume. They also give you 12k/month free, which is rather nice.
谷歌已经撤回了免费的域名 GMail。周围还有很多其他免费服务。其中一个值得注意的是Mandrill- 一种用于交易电子邮件(例如电子商务订单等)的一对一电子邮件服务。它由 MailChimp 运营,他几乎知道关于批量发送电子邮件的所有知识。他们还免费为您提供 12k/月,这是相当不错的。
回答by casaram
This question and correct answer may be relevant:
这个问题和正确答案可能是相关的:
When using Gmail for SMTP, can you set a different "from" address?
将 Gmail 用于 SMTP 时,您可以设置不同的“发件人”地址吗?
Gmail requires you to validate From addresses before sending mail as that email address. So you need to add a new sender in your personal gmail account and validate it.
Gmail 要求您在以该电子邮件地址发送邮件之前验证发件人地址。因此,您需要在您的个人 Gmail 帐户中添加一个新发件人并对其进行验证。
Doing so will allow you to authenticate with [email protected] and send email from [email protected]
这样做将允许您使用 [email protected] 进行身份验证并从 [email protected] 发送电子邮件
回答by Martin Zvarík
You need to go to GMAIL settings and add new alias.
您需要转到 GMAIL 设置并添加新别名。
You will be asked SMTP information, which is basically useless, since you are using SMTP to send email, BUTthe catch is that if your alias is on Google Suite domainit will be added just with simple email confirmation!
您将被询问 SMTP 信息,这基本上是无用的,因为您使用 SMTP 发送电子邮件,但问题是,如果您的别名在 Google Suite 域中,则只需通过简单的电子邮件确认即可添加它!
Once you have the alias there, you can change "From" header in your SMTP email.
在那里有了别名后,您可以更改 SMTP 电子邮件中的“发件人”标题。
NOTE:You cannot change the "From" address to [email protected], that's just how Gmail works and is the reason it's trusted.
注意:您不能将“发件人”地址更改为[email protected],这就是 Gmail 的工作方式,也是它受到信任的原因。
回答by Anthony
Unlike everyone else, I'll take the plunge and make the assumption that by letters you mean emails...
与其他人不同,我将冒险尝试并假设您所说的字母是指电子邮件......
But I'm not sure what you are getting at when you mention that it should include "Headers and Content". Do you want to forward emails? Do you want the emails from Ato appear as though they came from B's gmail account in the headers? Are you building some sort of gmail client?
但是当您提到它应该包括“标题和内容”时,我不确定您会得到什么。您要转发电子邮件吗?您是否希望来自A的电子邮件在标题中显示为来自B的 gmail 帐户?您正在构建某种 Gmail 客户端吗?
The easiest way to send an email with PHP is with the mailfunction. This example comes straight from their documentation:
使用 PHP 发送电子邮件的最简单方法是使用邮件功能。这个例子直接来自他们的文档:
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
If you want the headers to appear from A's gmail and not to simply change the from/reply to part, you'd have to use gmail as the SMTP server. I don't know if you can set that at the script level.
如果您希望标题出现在A的 gmail 中,而不是简单地更改 from/reply to 部分,则必须使用 gmail 作为 SMTP 服务器。我不知道您是否可以在脚本级别设置它。
回答by Espo
If the reason you want to use gmail is because you don't want to set up an MTA (the reason you stated in a comment to thisanswer), you have 2 options:
如果你想使用Gmail的原因是因为你不希望建立一个MTA(你的意见陈述的理由这个答案),你有两个选择:
- If the web server is at your home/work place; use your ISP's smtp-server
- If the web server is at a dedicated hosting center, ask them what smtp-server to use.
- 如果网络服务器在您的家/工作场所;使用您的 ISP 的 smtp 服务器
- 如果 Web 服务器位于专用托管中心,请询问他们使用什么 smtp-server。
回答by Yulin
The answer above are not quite correct.
上面的答案并不完全正确。
You are definitely able to specify any senders as long asyou own the other email address.
只要您拥有其他电子邮件地址,您绝对可以指定任何发件人。
As the help pageexplains:
正如帮助页面解释的那样:
- On your computer, open Gmail.
- In the top right, click Settings.
- Click the Accounts and importor Accountstab.
- In the "Send mail as" section, click Addanother email address.
- Enter your name and the address you want to send from.
- Click Next Stepand then Send verification.
- For school or work accounts, enter the SMTP server (for example, smtp.gmail.com or smtp.yourschool.edu) and the username and password on that account.
- Click Add Account.
- 在您的计算机上,打开Gmail。
- 在右上角,点击设置。
- 单击帐户和导入或帐户选项卡。
- 在“发送邮件为”部分中,单击添加另一个电子邮件地址。
- 输入您的姓名和要发送的地址。
- 单击下一步,然后单击发送验证。
- 对于学校或工作帐户,输入 SMTP 服务器(例如,smtp.gmail.com 或 smtp.yourschool.edu)以及该帐户的用户名和密码。
- 单击添加帐户。
Once that email is added successfully, you can send email on the behalf of the new email address in gmail.
成功添加该电子邮件后,您可以在 gmail 中代表新电子邮件地址发送电子邮件。
Google will notrewrite your from email in this way while you're sending email via Google SMTP.
谷歌不会从电子邮件这样,当你通过谷歌SMTP发送电子邮件重写。

