0

I'm using PHPMailer linked to my gmail account.

I've required the autoloader, created a function to handle sending mail, then fire the function to test. It all looks like this:

require "PHPMailer/PHPMailerAutoload.php";

function sendMail($to, $subject, $body, $from){

  //init PHPMailer
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = "tls";
  $mail->Port = 587;
  $mail->isHTML(true);

  //connection settings
  $mail->Host = "smtp.gmail.com";
  $mail->Username = "xxx@xxx.com";
  $mail->Password = "xxxxxxxx";

  //addresses
  $mail->addAddress($to);
  $mail->setFrom($from);

  //create email
  $mail->Subject = $subject;
  $mail->Body = $body;

  //send email
  $mail->send();
}

//sendMail(to, subject, body, from)
sendMail("xxxx@xxxxx.com", "Test Subject", "Test body", "xxxxx@xxxxx.com");

This sends the email with the correct subject and body, to the right place, but it ignores the 'from' address and the email always says it comes from the gmail account through which the email is passed. Is there anyway to configure this to work?

Note I have obviously omitted the correct account details and real to or from addresses from the code above.

CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • I swear I'm not being a dick BUT, do you *really* need phpMailer? I'm asking this because PHP has a built-in [mail()](http://pt2.php.net/manual/en/function.mail.php) function which works wonders *if* you don't need attachments and whatnot. It has limitations, as stated in phpmailers docus, but if you don't need a jackhammer to kill a fly why use it? (I'm obviously assuming it's a tiny fly) – MoshMage Sep 24 '14 at 08:53
  • @MoshMage I'm using Microsoft Azure server. PHP mail() doesn't work. That's the whole point of using SMTP here. – CaribouCode Sep 24 '14 at 08:59
  • shucks. Let me try to read them docs and check if I can help – MoshMage Sep 24 '14 at 09:08
  • Yeh it's a pain, wish I could just use PHP mail(), life would be simpler. – CaribouCode Sep 24 '14 at 09:11
  • [Are you missing a argument in your `setFrom()`?](http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_setFrom) – MoshMage Sep 24 '14 at 09:19
  • No. I've come to the conclusion I think my code is correct and it's to do with gmail settings not allowing the sender to be a proper alias address. – CaribouCode Sep 24 '14 at 09:27
  • Oh well. I'm out of Ideas, then. Merry Luck on your searches – MoshMage Sep 24 '14 at 09:29
  • 1
    @MoshMage it's a *really* bad idea to call `mail()` directly - the vast majority of example code is just wrong, incomplete or vulnerable to attack. For example *all* the code examples on the PHP docs page are vulnerable to header injection, and will not encode headers and bodies correctly. – Synchro Sep 24 '14 at 09:34
  • 1
    @Synchro Never really thought you could inject `mail()` (mostly because I never thought about doing it myself) but oddly enough I always make sure the "sender" is *just* an email (as in, regex the input and match to a email form); Meaning that while it can be injected it isn't that hard to prevent. BUT! It's nice to know of this atrocity to prevent in the future. Thank you. – MoshMage Sep 24 '14 at 09:41
  • Validation is always good. Header injection is quite simple - for example if you pass through a subject line untouched, it can have extra headers appended by an attacker, like `Your password reminder\r\nBCC: drevil@example.com` – Synchro Sep 24 '14 at 09:49

2 Answers2

5

It's very simple: By default, Gmail doesn't allow you to set the from address to a non-gmail address unless they are handling your domain; they will rewrite the from address to be your gmail address, exactly as you are seeing. This is nothing to do with PHPMailer nor the PHP mail function.

There is provision to set up specific from addresses (rather than whole domains), but you have to set them up beforehand; you can't just send from random addresses. See this answer.

Community
  • 1
  • 1
Synchro
  • 35,538
  • 15
  • 81
  • 104
-1

Change mentioned portion below:

$mail->addAddress($to);
$mail->setFrom($from);

TO

$mail->addAddress($to); 
$mail->From = $from;
$mail->FromName = $from_name;

Hope it will work.

Regards.

  • Thanks but this doesn't work. It still comes from the same email address and the 'from name' now shows up as 'root user'. – CaribouCode Sep 24 '14 at 08:40
  • Brother, can you please follow the convention of PHPMailer from the link: https://github.com/PHPMailer/PHPMailer . Its clearly written here. Let me know if you need any help regarding this. :) – William Francis Gomes Sep 24 '14 at 09:13
  • I think if you use below it will help you. $mail->From = $sender_email; $mail->FromName = $sender_name; $mail->addAddress($receiver_email, $receiver_name); – William Francis Gomes Sep 24 '14 at 09:16
  • I'm aware that's the way it says to do it on PHPMailer but your method doesn't work for me. I think this may have something to do with my gmail account being unable to send from alias addresses. When it gets passed through that account, it automatically gets stamped with that email address as the sender. – CaribouCode Sep 24 '14 at 09:26
  • I hope this link will help u. http://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer – William Francis Gomes Sep 24 '14 at 09:30
  • You can do this either way (though `setFrom()` is preferred), but it won't solve the OP's problem. – Synchro Sep 24 '14 at 09:39