0

I'm using Swift Mailer v3.3.2 to send emails from my app and I need to be able to change the text of the sender.

My code looks like this:

             //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();

I want to be able to set the name text of the sender as 'Senders_Name Senders_Surname', even though the sender is still subdomain@domain.org

Any clue on how to do that?

Alberto
  • 908
  • 2
  • 10
  • 25

1 Answers1

1

After line

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

you should add

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

The whole working script below (I've just tested it in 3.3.2 version):

<?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();

Below image how the sender is displayed in email client (Thunderbird for me) so it works fine. If you test it in your email client make sure you don't have account from set as one of your mail accounts. In that case email client shows for example "Me" or sth else. The best for test just fill in addTo with your email and smtp settings and leave from and other parts unchanged

Sender name

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • But if I do this, what I will add is the name of the person who is going to receive the email, right? What I want is set the name of the person who is sending the email – Alberto May 19 '14 at 12:00
  • Sorry, I've looked at addTo method in your code. I've changed the answer – Marcin Nabiałek May 19 '14 at 12:05
  • Thanks for the answer. Could you maybe indicate where do I need to add the line? Thanks! – Alberto May 19 '14 at 12:26
  • It should be added in the same place where you set subject of the message. Probably you can set it just after email::connect(). I use syntax as in this example: http://swiftmailer.org/docs/messages.html – Marcin Nabiałek May 19 '14 at 12:31
  • I get the error I thought. I think is because I use Swift Mailer v3.3.2, and the solution you propose is for the last version (5 I think). This is the error I get: Fatal error: Call to undefined method Swift::setFrom() – Alberto May 19 '14 at 12:46
  • I've changed the answer. Hope it will work. I was browsing for 3.3.2 files for PHP5 – Marcin Nabiałek May 19 '14 at 13:20
  • Thanks Marcin. I still receive the emails with the default name even if I add the line you said – Alberto May 19 '14 at 15:16
  • Thanks so much, Marcin. You rock! :) – Alberto May 20 '14 at 10:18