How to change from-address when using gmail smtp server

53,559

Solution 1

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.

Solution 2

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]

Solution 3

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.

Solution 4

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.

Share:
53,559

Related videos on Youtube

omg
Author by

omg

Updated on July 09, 2022

Comments

  • omg
    omg almost 2 years

    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?

    • sarkiroka
      sarkiroka about 6 years
      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.
  • omg
    omg over 14 years
    It seems that "From: [email protected]" is not working now.
  • Ashith
    Ashith over 14 years
    Did you replace [email protected] with the email address that you are actually wanting to send from?
  • omg
    omg over 14 years
    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.
  • Ashith
    Ashith over 14 years
    I just sent an email to you with PHP where I made the FROM as your email, did it work?
  • Ashith
    Ashith over 14 years
    and another one where I made the FROM as [email protected].
  • omg
    omg over 14 years
    BTW,are you using gmail?As iAn said below,this can't be achieved with gmail.
  • Ashith
    Ashith over 14 years
    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?
  • omg
    omg over 14 years
    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.
  • omg
    omg over 14 years
    Other third-party Mail Transfer Agent like gmail are all acceptable.Since gmail has such limitations,could you recommend another one?
  • Ashith
    Ashith over 14 years
    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?
  • omg
    omg over 14 years
    Other third-party Mail Transfer Agent like gmail are all acceptable.Do you know which public MTA doesn't have such restrictions?
  • Ashith
    Ashith over 14 years
    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.
  • Ashith
    Ashith over 14 years
    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.
  • Ashith
    Ashith over 14 years
    And not to sound sleazy, but a vote up on my answer would sure make this hour long conversation seem worth it.
  • omg
    omg over 14 years
    OK,I've given you the vote:) But you didn't understand me. I don't want to send from my hosting environment...
  • omg
    omg over 14 years
    No,a public and free MTA is better:(
  • omg
    omg over 14 years
    What I am seeking of is a public and free MTA
  • Ashith
    Ashith over 14 years
    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.
  • omg
    omg over 14 years
    Yes,I want a PUBLIC and FREE SMTP server,I don't want to setup it by myself.
  • omg
    omg over 14 years
    Nor has it preinstalled in my hosting environment
  • Ashith
    Ashith over 14 years
    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.
  • Ashith
    Ashith over 14 years
    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.
  • Espo
    Espo over 14 years
    Better in what sense? Why do you not want to use the ISP's SMTP-server?
  • omg
    omg over 14 years
    I don't want to pay extra bucks:)
  • Espo
    Espo over 14 years
    What ISP do you use? I have never heard of one that charges you to use their SMTP server?
  • Alexander Shcheblikin
    Alexander Shcheblikin almost 11 years
    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.
  • Gocht
    Gocht almost 9 years
    I am really late to the party, but I am trying something like this, could you help me with my question? stackoverflow.com/questions/31173078/…
  • peterh
    peterh over 7 years
    @AlexanderShcheblikin Most probably he had to switch to some corporate contract or similar, to get access to the ISP SMTP.
  • Paul Basenko
    Paul Basenko about 7 years
    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.
  • Ashith
    Ashith about 7 years
    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/mail
  • evilReiko
    evilReiko over 6 years
    Yes you can! Look at @casaram answer.
  • Phil
    Phil over 5 years
    This just asked me to insert new SMTP details for my other email address :(
  • rustyx
    rustyx over 5 years