简体   繁体   中英

How to set 'FROM' property using Swiftmailer with Gmail?

<?php
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('username@gmail.com')
  ->setPassword('password')
  ;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('HomeWork')
  ->setFrom(array('exampleFROM@gmail.com' => 'NAME'))
  ->setTo(array('exampleTO@gmail.com'=> 'NAME'))
  ->setBody('Test Message Body')
  ;
$mailer->send($message);
?>

It works but sender is 'username@gmail.com'. How can I specify any other email address in order to send fake email?

Gmail disallows overriding the FROM name except from verfied email addresses that you prove to gmail you own. Either choose a different email server or go to your gmail settings and change it to another valid email address that you can receive email from.

You need to set the outgoing emailaddress in gmail. Gmail changes the emailaddress to the address in your gmail if it is not in the list of emailaddresses where you can sent from.

Gmail -> settings -> Send mail as -> Add another email address you own

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