symfonyswiftmailer

How to put a name on the email sender?


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

Administrator <no-reply@app.com>

I've tried in the parameters.ini :

mailer_from      = {"no-reply@app.com : 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 ?


Solution

  • // Set a single From: address
    $message->setFrom('your@address.tld');
    
    // Set a From: address including a name
    $message->setFrom(array('your@address.tld' => 'Your Name'));
    
    // Set multiple From: addresses if multiple people wrote the email
    $message->setFrom(array(
      'person1@example.org' => 'Sender One',
      'person2@example.org' => 'Sender Two'
    ));