I want to send an email from A to B,with HEADER and CONTENT through gmail.
我想通过Gmail向HEADER和CONTENT发送一封从A到B的电子邮件。
How to do that by PHP?
如何通过PHP做到这一点?
I've specified the FROM(from@example.com),but when I receive the email,it's still from my gmail account(abc@gmail.com).
我已经指定了FROM(from@example.com),但是当我收到电子邮件时,它仍然来自我的Gmail帐户(abc@gmail.com)。
screenshot of from address http://maishudi.com/mail.jpg
地址http://maishudi.com/mail.jpg的屏幕截图
$mail->From = "from@example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("abc12@163.com", "Josh Adams");// name is optional
$mail->AddReplyTo("abc12@qq.com", "Information");
How do I change the FROM part?
如何更改FROM部分?
5 个解决方案
#1
42
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服务发送的邮件中的“发件人”和“回复”标题重写为与您的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 "myapp@gmail.com" account
- 设置专用的“myapp@gmail.com”帐户
- If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a "myapp@mydomain.com" account.
- 如果您拥有您应该发送的域名,请使用域名的免费Gmail,并设置“myapp@mydomain.com”帐户。
====
====
Edit June 2015
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
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运行,MailChimp非常了解有关发送电子邮件的所有信息。他们还给你12k /月免费,这是相当不错的。
#2
8
This question and correct answer may be relevant:
这个问题和正确答案可能是相关的:
When using Gmail for SMTP, can you set a different "from" address?
使用Gmail for 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 youremail@gmail.com and send email from from@example.com
这样做将允许您使用youremail@gmail.com进行身份验证并从from@example.com发送电子邮件
#3
1
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 A to 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 mail function. This example comes straight from their documentation:
使用PHP发送电子邮件的最简单方法是使用邮件功能。这个例子直接来自他们的文档:
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\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 part,则必须使用gmail作为SMTP服务器。我不知道你是否可以在脚本级别设置它。
#4
0
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 this answer), you have 2 options:
如果你想使用Gmail的原因是因为你不想设置MTA(你在这个答案的评论中说明的原因),你有2个选择:
- If the web server is at your home/work place; use your ISP's smtp-server
- 如果Web服务器在您的家/工作地点;使用您的ISP的smtp服务器
- If the web server is at a dedicated hosting center, ask them what smtp-server to use.
- 如果Web服务器位于专用托管中心,请询问他们使用什么smtp-server。
#5
0
The answer above are not quite correct. You are definitely able to specify any senders as long as you own the domain. Let me tell you how.
上面的答案并不完全正确。只要您拥有域,您肯定能够指定任何发件人。我告诉你怎么样。
1. Go to gmail settings gear, click account tab.
2. Under send as section, click add another email.
3. Fill your email server information.
Once that email is added successfully, you can send email on the behalf of the new email address in gmail.
成功添加该电子邮件后,您可以代表gmail中的新电子邮件地址发送电子邮件。
Google will not rewrite your from email in this way while you're sending email from your PHP code.
当您从PHP代码发送电子邮件时,Google不会以这种方式重写您的电子邮件。
#1
42
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服务发送的邮件中的“发件人”和“回复”标题重写为与您的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 "myapp@gmail.com" account
- 设置专用的“myapp@gmail.com”帐户
- If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a "myapp@mydomain.com" account.
- 如果您拥有您应该发送的域名,请使用域名的免费Gmail,并设置“myapp@mydomain.com”帐户。
====
====
Edit June 2015
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
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运行,MailChimp非常了解有关发送电子邮件的所有信息。他们还给你12k /月免费,这是相当不错的。
#2
8
This question and correct answer may be relevant:
这个问题和正确答案可能是相关的:
When using Gmail for SMTP, can you set a different "from" address?
使用Gmail for 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 youremail@gmail.com and send email from from@example.com
这样做将允许您使用youremail@gmail.com进行身份验证并从from@example.com发送电子邮件
#3
1
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 A to 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 mail function. This example comes straight from their documentation:
使用PHP发送电子邮件的最简单方法是使用邮件功能。这个例子直接来自他们的文档:
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\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 part,则必须使用gmail作为SMTP服务器。我不知道你是否可以在脚本级别设置它。
#4
0
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 this answer), you have 2 options:
如果你想使用Gmail的原因是因为你不想设置MTA(你在这个答案的评论中说明的原因),你有2个选择:
- If the web server is at your home/work place; use your ISP's smtp-server
- 如果Web服务器在您的家/工作地点;使用您的ISP的smtp服务器
- If the web server is at a dedicated hosting center, ask them what smtp-server to use.
- 如果Web服务器位于专用托管中心,请询问他们使用什么smtp-server。
#5
0
The answer above are not quite correct. You are definitely able to specify any senders as long as you own the domain. Let me tell you how.
上面的答案并不完全正确。只要您拥有域,您肯定能够指定任何发件人。我告诉你怎么样。
1. Go to gmail settings gear, click account tab.
2. Under send as section, click add another email.
3. Fill your email server information.
Once that email is added successfully, you can send email on the behalf of the new email address in gmail.
成功添加该电子邮件后,您可以代表gmail中的新电子邮件地址发送电子邮件。
Google will not rewrite your from email in this way while you're sending email from your PHP code.
当您从PHP代码发送电子邮件时,Google不会以这种方式重写您的电子邮件。