When using Gmail for SMTP, can you set a different

2019-01-04 14:07发布

I am using Swift Mailer 406 for sending emails. I connect to my smtp.gmail.com account and then I do:

->setFrom(array($from => $fromname))

But the emails sent got the original gmail account email.

Can I change it?

3条回答
再贱就再见
2楼-- · 2019-01-04 14:50

In your Parameters.yml you should make this configuration:

parameters:
database_host: 127.0.0.1
database_port: null
database_name: your db name
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: your fix adress@gmail.com
mailer_password: your password of your fix adress
mailer_port: 465
mailer_encryption: ssl
auth_mode:         login
secret: 3556f3fb752a82ce0ee9c419ef793b7a707f324a

And in your contact controller you should add this to fix setfrom() function of swiftmailer:

if ($form->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $subject = $entity->getSubject();
    $name=$entity->getName();
    $email=$entity->getEmail();
    $body=$entity->getBody();
    $message = \Swift_Message::newInstance('here')
        ->setSubject("Shoppify email from ".$name." Subject ".$subject)
        ->setFrom(array('your fix adress@gmail.com' => $email))
        ->setTo('your adress destionation@example.com')
        ->setBody($body);
    $this->get('mailer')->send($message);
    $em->persist($entity);
    $em->flush();
    return $this->redirect($this->generateUrl('email_sended'));
}
查看更多
叛逆
3楼-- · 2019-01-04 14:51
$email=$entity->getEmail();
->setFrom(array('your fix adress@gmail.com' => $email))
查看更多
老娘就宠你
4楼-- · 2019-01-04 15:01

gmail doesn't allow you to use random From addresses. You have to add and validate the address you'd like to use in the gmail settings:

Settings -> Accounts -> Send mail as -> Add another email address you own
查看更多
登录 后发表回答