开发者

When using Gmail for SMTP, can you set a different "from" address?

I am using Swift Mailer 406 for sending emails. I connect to my smtp.gmail.com account and t开发者_StackOverflow中文版hen I do:

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

But the emails sent got the original gmail account email.

Can I change it?


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


$email=$entity->getEmail();
->setFrom(array('your fix adress@gmail.com' => $email))


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'));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜