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

3

6

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?

Cardio answered 25/3, 2011 at 11:9 Comment(0)
B
22

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
Belden answered 25/3, 2011 at 11:13 Comment(1)
thanks. do you know if i can send many emails thru gmail without problems? (they are all legit sent to registered users from my website)Follansbee
S
-1
$email=$entity->getEmail();
->setFrom(array('your fix [email protected]' => $email))
Strainer answered 15/4, 2016 at 22:52 Comment(1)
Please also add an explanation to your answer, since code dumps are often hard to understandCoquina
S
-2

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 [email protected]
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 [email protected]' => $email))
        ->setTo('your adress [email protected]')
        ->setBody($body);
    $this->get('mailer')->send($message);
    $em->persist($entity);
    $em->flush();
    return $this->redirect($this->generateUrl('email_sended'));
}
Strainer answered 15/4, 2016 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.