【问题标题】:When using Gmail for SMTP, can you set a different "from" address?将 Gmail 用于 SMTP 时,您可以设置不同的“发件人”地址吗?
【发布时间】:2011-07-22 20:15:26
【问题描述】:

我正在使用 Swift Mailer 406 发送电子邮件。我连接到我的 smtp.gmail.com 帐户,然后我这样做:

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

但是发送的邮件得到了原来的gmail账号邮件。

我可以换吗?

【问题讨论】:

    标签: php email smtp gmail swiftmailer


    【解决方案1】:

    gmail 不允许您使用随机发件人地址。您必须在 gmail 设置中添加并验证您要使用的地址:

    Settings -> Accounts -> Send mail as -> Add another email address you own
    

    【讨论】:

    • 谢谢。你知道我是否可以毫无问题地通过 gmail 发送许多电子邮件? (它们都是从我的网站合法发送给注册用户的)
    【解决方案2】:
    $email=$entity->getEmail();
    ->setFrom(array('your fix adress@gmail.com' => $email))
    

    【讨论】:

    • 请在您的答案中添加解释,因为代码转储通常很难理解
    【解决方案3】:

    在您的 Parameters.yml 中,您应该进行以下配置:

    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
    

    在你的联系人控制器中,你应该添加这个来修复 swiftmailer 的setfrom() 功能:

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

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 2015-03-02
      • 2011-05-13
      • 1970-01-01
      • 2015-06-16
      • 2010-09-11
      • 1970-01-01
      • 2013-06-19
      相关资源
      最近更新 更多