How to put a name on the email sender?
Asked Answered
S

3

5

I'm trying to, when an email is sent within my Symfony2 application, to name the sender

Administrator <[email protected]>

I've tried in the parameters.ini :

mailer_from      = {"[email protected] : Administrator"}

It seems possible to do so because in SimpleMessage.php we do have :

  /**
   * Set the sender of this message.
   * This does not override the From field, but it has a higher significance.
   * @param string $sender
   * @param string $name optional
   * @return Swift_Mime_SimpleMessage
   */
  public function setSender($address, $name = null)
  {
    if (!is_array($address) && isset($name))
    {
      $address = array($address => $name);
    }

    if (!$this->_setHeaderFieldModel('Sender', (array) $address))
    {
      $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
    }
    return $this;
  }

All the things I tried in the parameters.ini failed. Do you have any idea of what i'm doing wrong please ?

Sabah answered 21/6, 2012 at 8:9 Comment(0)
F
19
// Set a single From: address
$message->setFrom('[email protected]');

// Set a From: address including a name
$message->setFrom(array('[email protected]' => 'Your Name'));

// Set multiple From: addresses if multiple people wrote the email
$message->setFrom(array(
  '[email protected]' => 'Sender One',
  '[email protected]' => 'Sender Two'
));
Factorial answered 4/9, 2013 at 23:8 Comment(0)
P
3

It seems possible to do so because in SimpleMessage.php we do have

Well, it is possible to set a name, but there is nothing indicating you can set it using the configuration.

A quick dump of the available config for the SwitfMailerBundle (php app/console config:dump-reference SwiftmailerBundle) will return:

Default configuration for "SwiftmailerBundle"
swiftmailer:          
    transport:            smtp 
    username:             ~ 
    password:             ~ 
    host:                 localhost 
    port:                 false 
    encryption:           ~ 
    auth_mode:            ~ 
    spool:                
        type:                 file 
        path:                 %kernel.cache_dir%/swiftmailer/spool 
    sender_address:       ~ 
    antiflood:            
        threshold:            99 
        sleep:                0 
    delivery_address:     ~ 
    disable_delivery:     ~ 
    logging:              true 

As you can see, there is sender_address which is passed to the ImpersonatePlugin.php.

I'm not sure, you'll be able to set a name, but the standard way, rawly, is to use a string of that form:

"Administrator "

If it doesn't work, it probably means, that you'll some work, to write a real EmailManager.

SwiftMailerBundle is actually only an integration of the SwiftMailer library, which is, a library, and not a "Manager".

Polka answered 21/6, 2012 at 9:20 Comment(2)
That's why i did, i wrote the EmailManager which gets the config i want in the parameters.ini. Thanks for the tipSabah
Looks like there is an open PR for this config feature: github.com/symfony/SwiftmailerBundle/issues/94 +1Thenar
U
1

Not using the same version of Symfony but since this is one of the top results for this problem, here is what I did for "symfony/mailer": "5.4.*", to get a display name to work.

$email = (new Email())
    ->from(new Address('[email protected]', 'Name To Display'))
    ->to($recipientEmail)
    ...


$mailer->send($email);
Unarm answered 4/4 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.