php – How to change from-address when using gmail smtp server-ThrowExceptions

Exception or error:

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(from@example.com),but when I receive the email,it’s still from my gmail account(abc@gmail.com).

screenshot of from address
(source: maishudi.com)

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

How to solve:

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:

  • Setup a dedicated “myapp@gmail.com” account
  • If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a “myapp@mydomain.com” account.

====

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.

Answer:

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 youremail@gmail.com and send email from from@example.com

Answer:

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      = '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.

Answer:

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 whatever@dude.com, that’s just how Gmail works and is the reason it’s trusted.

Answer:

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.

Answer:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *