How to Have Swift Mailer Use Sender Display Name

Good day!

I was able to send emails using Yii2 swift mailer fine. I’m just having trouble with one feature.

============

BACKGROUND

============

(1) I’m trying to have gmail and ymail display sender as a name instead of an email address.

(2) In order to do so, I think I need to set email header ‘From’ field to something like the line below:

‘From: DisplayName <YourName@domain.com>’

============

PROBLEM

============

(1) According to the two URL below, it is easy to do:

<—Urls Ommitted. I was not allowed to post urls.—>

(2) I think I just need to use a keyed array for setFrom() input below where they key is the Email and the value is the DisplayName:




$status['sent'] = \Yii::$app->mailer->compose(

    $modelNewsletter['page'],

    [

        'intro'     => $modelNewsletter['intro'],

        'emailData' => $emailData

    ]

)->setFrom(

    [

        $ctrl['basic.adminEmail'] => $ctrl['sender.name']

    ]

)->setTo(

    $obj['email']

)->setSubject(

    $modelNewsletter['subject']

)->send();



(3) But I could not get it to work. I also tried searching this forum. But could not find relevant information.

============

DEBUGGING

============

(1) I first tried to send out real emails. But it would not work. The email sends but gmail and ymail won’t show a name for the sender.

(2) Then I try to set config/web.php to create dummy file emails instead by setting mailer.useFileTransport to TRUE.




'mailer' => [

    'useFileTransport' => true,

],



This is so that I can use Yii2 debugger to view the email headers.

(3) Yii2 Debugger shows me the ff headers. Please see "From". It has no name, just an email.:

Message-ID: <c92cb0cfe23645d1463c4769f54b6dd0@www.mystartups.ph>

Date: Tue, 14 Jun 2016 03:21:47 +0000

Subject: Personalized Test

From: admin@mystartups.ph

To: zentlim@gmail.com MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=swift_v4_1465874507_0bba28970e4d1af435a481e131158cfb="

============

VERSION

============

According to my composer.lock file, I’m using the ff versions:

(1) yiisoft/yii2 2.0.5

(2) swiftmailer/swiftmailer v5.4.1

============

QUESTION

============

How do I get setFrom() to update email header so that it will use a display name with an email?

There might be the same case as with Gmail with your SMTP server. Check this solution that you might already see while googling for an answer. You did look for an answer, right?

Thank you for sharing the link.

Yes. I did search for answers.

In the link you sent, the solutions proposed are not for Yii2.

(1) It has a solution for stand alone php and swift mailer.

(2) It has a solution for some other tool (doctrine) coupled with swift mailer.

Ultimately, the link you shared also tries to implement the solution that I posted in my original post which is the code below:




->setFrom(array('email@domain.com' => 'DisplayName'))



My argument is that this solution is not working for me despite Yii2 pages saying that it will work:

http://www.yiiframework.com/doc-2.0/yii-mail-messageinterface.html#setFrom()-detail

==========

QUESTION

==========

(1) Has anyone else encountered this problem?

(2) Or does anyone have tips on an alternative way to achieve the same results?

If even with the file transport you can’t see full From header double check all the arrays you use there. I’ve checked my system’s sample of file-saved mail and there is “From: Name <email@example.com>” header for ->setFrom([‘email@example.com’ => ‘Name’]) settings.

If everything seems to be ok try to upgrade Yii 2 to newest version.

Thank you running my test case. Thank you also for this advise: "double check all the arrays you use there".

I took it literally and added debug lines. They wouldn’t show. I added more. I soon found that what I was editing was not what I was running.

Sorry for using your time with this careless mistake. I’ll avoid this next time. Have a good day ahead!

P.S.

I would have posted this sooner but the forum would only allow me two posts per day.

No worries. You have not forced me to help you, it was my choice so no need for apologies. Errors like this one happen all the time.

I’m glad everything is fine now.

Thanks again!