简体   繁体   中英

Mail getting sent from a different email address

I am facing this problem where the mail is getting sent as a different email rather than the one specified.

                    $from_name = 'send';
                    $from = 'send@test.com';
                    $to = 'receive@test.com';
                    $to_name = 'receive';
                    $also_to = 'cc@test.com';
                    $also_to_name = 'cc';
                    $message = 'Dear receive Thank you for the booking';
                    $message .= '<br><br>'.$homepage;
                    $mail = new PHPMailer();
                    $mail->SMTPDebug  = 0;
                    $mail->IsSMTP();    
                    $mail->Port = 465;
                    $mail->Host = "ssl://smtp.gmail.com";
                    $mail->SMTPAuth = true;
                    $mail->Username = 'send1@test.com';
                    $mail->Password = ******;

                    $mail->SetFrom($from,$from_name );
                    $mail->AddReplyTo($from,$from_name );
                    $mail->Subject  = 'Booking Details';
                    $mail->MsgHTML($message);
                    $mail->AddAddress($to,$to_name);
                    $mail->AddCC($also_to,$also_to_name);

When I sent it using this code the mail goes with name as 'send' but not the 'send@test.com' email address it gets sent from "send -> send1@test.com" rather than send -> send@test.com. Above I have mentioned the smtp details that i have used. Also when i change:

                    $mail->Username = 'send@test.com';
                    $mail->Password = ******;

The mail does not go through to any email address. I hope this is clear enough for you guys and I would appreciate any help. Thank you

$mail->Username = send1@test.com;

this need to be in quotes like :

$mail->Username = "send1@test.com" ;

Check whether this resolves you issue ?

If not try setting debug to true $mail->SMTPDebug = 1;

and see what's being actually sent to gmail server

By default gmail will use the account's email address

Ref : How to change from-address when using gmail smtp server

Look at this: http://phpmailer.worxware.com/index.php?pg=properties

$From public root@localhost Sets the From email address for the message

and

$FromName public Root User Sets the From name of the message

So you want specifically add the following:

$mail->From = $from;
$mail->FromName= $from_name;

您必须调用setFrom方法来设置发件人的邮件地址和名称:

$mail->SetFrom('send@test.com', 'Your Name');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM