Messages Getting Sent with incorrect To:

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
mikekarl
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 3:20 pm

Messages Getting Sent with incorrect To:

Post by mikekarl »

I'm sending a bunch of mailers out that are customized for each recipient. At some point after an error has occurred mail starts getting sent out with and incorrect "To:" field. The mail is still going to the intended recipient, but the To: part has the information from a former recipient.

The Swift_Message doesn't get reset between messages.

I'm not sure if this is a bug, or just something that I need to watch out for, but if you have an idea of what I should do to prevent it please let me know.

Using: Swift Mailer Version 3.3.2 for PHP5.

Code:
I just included the relative stuff. Basically I'm instantiating this class once and then for each recipient setting the variables and calling send();

Code: Select all

global $swift_connections;
$swift_connections = array();
$swift_connections[] = new Swift_Connection_SMTP("server1");
$swift_connections[] = new Swift_Connection_SMTP("server2");
 
class SwiftMailer extends Mailer{
    
    protected $swift = false;
    protected $swift_connections = false;
    protected $email_message = false;
    protected $from_address = false;
    private $html_id = false;
    private $text_id = false;
    
    function __construct(){
        parent::__construct();
        global $swift_connections;
        if(!is_array($swift_connections)){
            die('Create $swift_connects array');
        }else{
            $this->swift_connections = $swift_connections;
            $this->start_connection();
        }
        $this->email_message = new Swift_Message("Subject");
        $this->email_message->setCharset("utf-8");
    }
    function start_connection(){
        
        $this->swift = new Swift(new Swift_Connection_Rotator($this->swift_connections)); 
        
        //anti flood plugin
        $this->swift->attachPlugin(new Swift_Plugin_AntiFlood(2000, 5), "anti-flood");
        
    }
    
    function reset(){
        try{
            $this->swift->disconnect();
        }catch (Swift_ConnectionException $e){
            echo "Disconnect Failed - SwiftMailer->reset() \n\n ".$e->getMessage()."\n\n";
        }
        unset($this->swift);
        try{
            $this->start_connection();
        }catch (Swift_BadResponseException $e){
            echo "Restart Failed 1 - SwiftMailer->reset() \n\n ".$e->getMessage()."\n\n";
            return false;
        }
        return true;
        
    }
 
function send(){
        if ($this->to_email && $this->from_address){
            $this->email_message->setSubject($this->subject);
                    
            //remove old messages
            if ($this->text_id){
                try{
                    $this->email_message->detach($this->text_id);
                    $this->text_id = false;
                }catch (Exception $e){
                    //this will probably happen the first time
                    echo $e->getMessage().'<br /><br />';
                }
            }
            if ($this->html_id){
                try{
                    $this->email_message->detach($this->html_id);
                    $this->html_id = false;
                }catch (Exception $e){
                    //this will probably happen the first time
                    echo $e->getMessage().'<br /><br />';
                }
            }
            
            
            if($this->text_email){
                $this->text_id = $this->email_message->attach(new Swift_Message_Part($this->text_email));
            }
            if ($this->html_email){
                $this->html_id = $this->email_message->attach(new Swift_Message_Part($this->html_email, "text/html"));
            }
            
            $to = false;
            if ($this->to_name){
                $to = new Swift_Address($this->to_email, $this->to_name);
            }else{
                $to = new Swift_Address($this->to_email);
            }
            
            try{
                if($this->swift->send($this->email_message, $to, $this->from_address)){
                    return true;
                }else{
                    return false;
                }
            }catch (Swift_ConnectionException $e){
                     echo "Connection Error On Send - Trying Reset \n\n ".$e->getMessage()."\n\n";
                    if ($this->reset()){
                        try{
                            if($this->swift->send($this->email_message, $to, $this->from_address)){
                                return true;
                            }else{
                                return false;
                            }
                        }catch (Swift_ConnectionException $e){
                            die ("Could Not Reset Connection \n\n".$e->getMessage());
                        }
                    }else{
                        return false;
                    }
            
            }
            
        }else{
            $this->message = 'Email Addresses Not Set';
            return false;
        }
    }
    
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Messages Getting Sent with incorrect To:

Post by Chris Corbyn »

This is a known bug which was recently identified. It will be fixed in the next major release.

For now, you may want to try to suggestion listed here:

viewtopic.php?f=52&t=77319&p=435411
mikekarl
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 3:20 pm

Re: Messages Getting Sent with incorrect To:

Post by mikekarl »

Thanks Chris!

For now, I'm just ending the sending process and letting cron restart the scripts. As I found in some other posts I'm probably trying to send way to many emails at one time.

thanks!
mike
Post Reply