使用Swift Mailer v3.3.2发送邮件时如何更改发件人的名称文本

时间:2014-05-19 10:14:30

标签: php email swiftmailer

我正在使用Swift Mailer v3.3.2从我的应用发送电子邮件,我需要能够更改发件人的文本。

我的代码如下所示:

             //Sending email

            $swift = email::connect();
            $email_message = new View('email/email_template');
            $subject = "Subject here";
            $from = "subdomain@domain.org";
            $email_message->content_email = new View('email/content/signup');
            $email_message->content_email->user = $user;

            $message = $email_message;
            $recipients = new Swift_RecipientList;
            $recipients->addTo($user->email);


             // Build the HTML message

            $message = new Swift_Message($subject, $message, "text/html");
            if ($swift->send($message, $recipients, $from)) {
               ;
            } else {
              ;
            }
            $swift->disconnect();

我希望能够将发件人的姓名文本设置为“Senders_Name Senders_Surname”,即使发件人仍为subdomain@domain.org

有关如何做到这一点的任何线索?

1 个答案:

答案 0 :(得分:1)

行后

$message = new Swift_Message($subject, $message, "text/html");

你应该添加

$message->setFrom(new Swift_Address($from , 'Senders_Name Senders_Surname'));

以下整个工作脚本(我刚刚在3.3.2版本中对其进行了测试):

<?php

    require ('lib/Swift.php');
    require('lib/Swift/Connection/SMTP.php') ;

    $smtp = new Swift_Connection_SMTP();
    $smtp->setServer('server');
    $smtp->setUsername('username');
    $smtp->setPassword('password');
    $smtp->setEncryption($smtp::ENC_SSL);
    $smtp->setPort(465);


    $swift = new Swift($smtp);
    $swift->connect();

    $subject = "Subject here";
    $from = 'test@email.com';

    $message = 'test message';
    $recipients = new Swift_RecipientList;
    $recipients->addTo('mymail');


     // Build the HTML message

    $message = new Swift_Message($subject, $message, "text/html");

    $message->setFrom(new Swift_Address($from , 'Senders_Name Senders_Surname'));



    if ($swift->send($message, $recipients, $from)) {
       ;
    } else {
      ;
    }
    $swift->disconnect();

下图是发件人如何在电子邮件客户端(Thunderbird for me)中显示,以便它可以正常工作。如果您在电子邮件客户端中对其进行测试,请确保您没有将帐户设置为您的某个邮件帐户。在这种情况下,电子邮件客户端显示例如&#34; Me&#34;或者其他。最好的测试只需用你的电子邮件和smtp设置填写addTo,并保持和/和其他部分保持不变

Sender name