How to change from-address when using gmail smtp server
Asked Answered
J

6

29

I want to send an email from A to B, with HEADER and CONTENT through gmail.

How to do that by PHP?

I've specified the FROM ([email protected]), but when I receive the email, it's still from my gmail account ([email protected]).

$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 FROM part?

Jaala answered 26/8, 2009 at 5:27 Comment(1)
if you has access the "[email protected]" email, you can set up your gmail account to "add new address" and you can send email with this address. but it send an activation code to that address and if you has not access to that account, you cannot set it.Scanty
M
62

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.

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.

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.

If you are intent on using gmail then you could:

====

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.

====

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.

Microwave answered 26/8, 2009 at 6:55 Comment(4)
Other third-party Mail Transfer Agent like gmail are all acceptable.Since gmail has such limitations,could you recommend another one?Jaala
I am really late to the party, but I am trying something like this, could you help me with my question? #31173578Lighterage
Yes you can! Look at @casaram answer.Adis
You CAN, if you add the other from-address to your account.Koser
A
10

This question and correct answer may be relevant:

When using Gmail for SMTP, can you set a different "from" address?

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.

Doing so will allow you to authenticate with [email protected] and send email from [email protected]

Alhambra answered 29/3, 2013 at 14:58 Comment(1)
This just asked me to insert new SMTP details for my other email address :(Obit
N
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?

The easiest way to send an email with PHP is with the mail function. This example comes straight from their documentation:

$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.

Negligee answered 26/8, 2009 at 5:47 Comment(23)
It seems that "From: [email protected]" is not working now.Jaala
Did you replace [email protected] with the email address that you are actually wanting to send from?Negligee
No,I think when I receive the email,should be from "[email protected]",but in fact it shows it's from "[email protected]" which is my gmail account.Jaala
I just sent an email to you with PHP where I made the FROM as your email, did it work?Negligee
and another one where I made the FROM as [email protected].Negligee
Could you send me your library and demo code? Seems the version I'm using doesn't work:(Jaala
BTW,are you using gmail?As iAn said below,this can't be achieved with gmail.Jaala
I am just using the native PHP mail function and the code in my answer. I copied it and changed "[email protected]" to your email address. Which version of PHP are you using?Negligee
So you aren't sending from gmail. I don't want to install postfix or equivalent,and the default "send" of linux is not scalable,so I decided to use a third party Mail Transfer Agent,like gmail.Jaala
I am not using gmail as the SMTP, no. I'm using a PHP script which is hosted on my school server account. If you just want the email to have the FROM line as being from "[email protected]", you don't have to use gmail. Gmail doesn't change the FROM line of incoming mails, only outgoing. Do you need it to really come from gmail or just have it appear that way in the FROM line?Negligee
Other third-party Mail Transfer Agent like gmail are all acceptable.Do you know which public MTA doesn't have such restrictions?Jaala
If you click on the show orignal message link on the email I sent you, you'll see the full Headers, which reflect the SMTP server (ie my server) and any other server that handled the message before it got to you. But this should only be a concern if you are spamming (which I hope you aren't) or if you are afraid that the recipient will look at the full headers and see something you don't want them to.Negligee
So are you trying to send emails using PHP locally on your own computer? Not on hosted server? I don't understand why things like MTAs and postfix and MTA restrictions are part of the equation. Sign up for a good hosting plan, put the example script I used in my original answer in the publishing directory, and go to the script in your browser. You'll get an email with what you want, I'm sure.Negligee
And not to sound sleazy, but a vote up on my answer would sure make this hour long conversation seem worth it.Negligee
OK,I've given you the vote:) But you didn't understand me. I don't want to send from my hosting environment...Jaala
What I am seeking of is a public and free MTAJaala
postfix, sendmail, and exim are all public and free and control (according to wikipedia) over half of all emails sent in the world. It still sounds like you want an SMTP sever to me, but I'm way over my head.Negligee
Yes,I want a PUBLIC and FREE SMTP server,I don't want to setup it by myself.Jaala
Nor has it preinstalled in my hosting environmentJaala
one really cool mta I know of is zimbra. but if you don't want a hosting environment, I'm not sure you'd want an exchange-like solution either.Negligee
Well, if I'm not mistaken, what you want then is an SMTP Proxy Server. Based on my hunting around on google, you get the same issues with a free mail proxy that you do with free proxies in general, which is they are crowded and thus slow, unreliable, and possibly unsecure. If it was me, and I absolutely had to use an SMTP server that wasn't part of my host, I'd pay the 10-20 bucks per month to know it was reliable. Even if you could do it over Google, your account would be flagged most likey if it was sending 1000s of emails but never recieving.Negligee
Thanks. Your comment was useful for me. The only trouble is I can't use Laravel mail queue now. Using php mail() for mailing means duplicate this basic Laravel functionality manually, but it isn't a rational solution.Bibliopole
This is an ooold thread. Most developers implement a more robust SMTP mailer library for PHP rather than use the native mail function. if Laravel has some mailer bundled with it, you should (as you suggested) use that mailer to keep things consistent. But how to achieve whatever it is you are trying to do with their mailer is really going to depend on what it actually is. laravel.com/docs/5.2/mailNegligee
F
1

The answer above are not quite correct.

You are definitely able to specify any senders as long as you own the other email address.

As the help page explains:

  1. On your computer, open Gmail.
  2. In the top right, click Settings.
  3. Click the Accounts and import or Accounts tab.
  4. In the "Send mail as" section, click Add another email address.
  5. Enter your name and the address you want to send from.
  6. Click Next Step and then Send verification.
  7. 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.
  8. Click Add Account.

Once that email is added successfully, you can send email on the behalf of the new email address in gmail.

Google will not rewrite your from email in this way while you're sending email via Google SMTP.

Fakir answered 21/7, 2018 at 23:47 Comment(0)
G
1

You need to go to GMAIL settings and add new alias.

You will be asked SMTP information, which is basically useless, since you are using SMTP to send email, BUT the catch is that if your alias is on Google Suite domain it will be added just with simple email confirmation!

Once you have the alias there, you can change "From" header in your SMTP email.


NOTE: You cannot change the "From" address to [email protected], that's just how Gmail works and is the reason it's trusted.

Graves answered 13/12, 2018 at 4:15 Comment(0)
V
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:

  1. If the web server is at your home/work place; use your ISP's smtp-server
  2. If the web server is at a dedicated hosting center, ask them what smtp-server to use.
Vesture answered 26/8, 2009 at 7:11 Comment(6)
No,a public and free MTA is better:(Jaala
Better in what sense? Why do you not want to use the ISP's SMTP-server?Vesture
I don't want to pay extra bucks:)Jaala
What ISP do you use? I have never heard of one that charges you to use their SMTP server?Vesture
The point is in the freedom of roaming - often ISPs don't allow SMTP access from other networks while client is roaming. Besides, many ISPs don't allow setting custom "From:" address. Also, many cheap ISPs don't provide SMTP service at all, or charge extra for it.Donella
@AlexanderShcheblikin Most probably he had to switch to some corporate contract or similar, to get access to the ISP SMTP.Aught

© 2022 - 2024 — McMap. All rights reserved.