anon
anon

Reputation:

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 then I do:

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

But the emails sent got the original gmail account email.

Can I change it?

Upvotes: 6

Views: 14272

Answers (3)

Jasser Yahyaoui
Jasser Yahyaoui

Reputation: 1

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

Upvotes: -2

Jasser Yahyaoui
Jasser Yahyaoui

Reputation: 1

$email=$entity->getEmail();
->setFrom(array('your fix [email protected]' => $email))

Upvotes: -1

ThiefMaster
ThiefMaster

Reputation: 318488

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

Upvotes: 22

Related Questions