1

I am using PHPMailer to send emails. Works great, except the email's From Address shows up as Me! [[email protected]] instead of Me! [[email protected]]. How do I set the email's From email address?

<?php
    require_once ('class.phpmailer.php');
    $email='[email protected]';


    $mail = new PHPMailer;

    $mail->isSMTP();                                      // Set mailer to use SMTP

    $mail->Host       = "smtp.gmail.com";
    $mail->Port       = 587;    //Maybe 465 instead? SSL only?
    $mail->Username   = "myusername";
    $mail->Password   = "mypassword";


    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

    $mail->From = $email;
    $mail->FromName = 'Me!';
    $mail->addAddress($email, 'Josh Adams');  // Add a recipient
    $mail->addReplyTo($email, 'Information');
    $mail->addCC($email);
    $mail->addBCC($email);

    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        exit;
    }

    echo 'Message has been sent';
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • Can you show us received email source header code? And describe what part troubles you there. – Glavić Oct 02 '13 at 17:02
  • 3
    Unless something has changed fairly recently, the unchangeable 'from' address is intentional - it prevents spoofing: [http://stackoverflow.com/questions/3871577/change-sender-address-when-sending-mail-through-gmail-in-c-sharp](http://stackoverflow.com/questions/3871577/change-sender-address-when-sending-mail-through-gmail-in-c-sharp) – Kristen Waite Oct 02 '13 at 17:07
  • @KristenJukowski. I am sure you are correct. – user1032531 Oct 02 '13 at 17:22
  • 2
    @user1032531 you're not completely out of luck if the 'from' address doesn't need to change and you have control over the associated email account. You can still set that email ([email protected]) up as a sender from within your gmail account; once you've taken care of that, your code above should work as intended! – Kristen Waite Oct 02 '13 at 17:25
  • @KristenJukowski. I will try your solution when I get a chance. Thank you. – user1032531 Oct 02 '13 at 17:35

1 Answers1

2

It is likely that Kristen is right in the comments, however you could try editing the class.phpmailer.php file and changing the settings there.



// The From email address for the message.
// @type string

   public $From = 'root@localhost';

// The From name of the message.
// @type string

   public $FromName = 'Root User';

// The Sender email (Return-Path) of the message.
// If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
// @type string

    public $Sender = '';

tremor
  • 3,068
  • 19
  • 37