Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder seanmarkham's Avatar
    Join Date
    Dec 2011
    Location
    Lincolnshire, England
    Posts
    50
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Swiftmailer looping problem

    Hi All, I am using swiftmailer for sending emails, it sends two emails to each recipient and I cannot find the loop error.

    Code:
    $activity = $_REQUEST['activity'];
    $template = $_REQUEST['template'];
    
    /* Database config */
    		
    		$db_host		= "localhost";
    		$db_user		= "user";
    		$db_pass		= "pass";
    		$db_database	= "database"; 
    		$db_table       = "testing";
    		/* End config */
    		
      $connectmail = "info@mail.co.uk";
      $connectsubject = "Contact form database";
      $connecterror = "There was a problem with the online database connection <br /><br />The data is:<br /><br />Name: " . $fname . "<br />Email: " . $from . "Telephone: " . $phone . "Activiy: " . $activity;
      $connectheader = "From: online database";
    		
    		
    		$con = mysql_connect($db_host,$db_user,$db_pass) or mail($connectmail, $connectsubject, $connecterror . "<br/><br />Error: Establishing a connection", $connectheader);
    		
    		
    		mysql_select_db($db_database)  or mail($connectmail, $connectsubject, $connecterror . "<br/><br />Error: Connecting to a database", $connectheader);
    
    
    	
    $sql = mysql_query("SELECT email FROM testing WHERE activity = '$activity' AND campaignsent = 'no' AND email REGEXP '^[^@]+@[^@]+\.[^@]{2,}$'");
    
    $recipients = array();
    while($row = mysql_fetch_array($sql)) {
        $recipients[] = $row['email'];
    
    }
    
    require_once '../Swift-5.0.1/lib/swift_required.php';
     
     	// Create the mail transport configuration
    $transport = Swift_MailTransport::newInstance();
    
    // Create the message
    $message = Swift_Message::newInstance();
    
    foreach($recipients as $recipient){
    	
    	
    if ($template == "halloween"){
    
    $subject = "Halloween";
    $message->setBody(
    '<html>' .
    ' <head></head>' .
    ' <body>' .
    ' <div align="center">' .
    '  <img src="' . // Embed the file
         $message->embed(Swift_Image::fromPath('../images/halloween/images/halloween-advert.gif')) .
       '" alt="Ancaster Leisure Halloween Laser Tag" />' .
       '<br />Click <a href="http://www.mysite.co.uk/unsubscribe.php?removeemail=' . $recipient . '">Here</a> to unsubscribe' .
       ' </div>' .
    ' </body>' .
    '</html>',
      'text/html' // Mark the content-type as HTML
    );
    
    }
    elseif ($template == "karting"){
    
    $subject = "day out";
    $message->setBody(
    '<html>' .
    ' <head></head>' .
    ' <body>' .
    ' <div align="center">' .
    '  <img src="' . // Embed the file
         $message->embed(Swift_Image::fromPath('../images/karting-mail/images/karting.gif')) .
       '" alt="outdoor venue" />' .
       ' </div>' .
    ' </body>' .
    '</html>',
      'text/html' // Mark the content-type as HTML
    );
    
    }
    $message->setSubject("$subject");
    
    
    $message->setFrom(array('info@mysite.co.uk' => 'John Smith'));
    $message->setReturnPath('info@mysite.co.uk');
    // Send the email
    
    
    	
       $message->setTo($recipient);
       $mailer = Swift_Mailer::newInstance($transport);
    
    
    
    
       $numSent = $mailer->send($message);
       
       $sqlquery = mysql_query("UPDATE testing SET campaignsent = 'no' WHERE email = '$recipient'");
       
    }
    
    if (!$mailer->send($message, $failures))
    {
    mail("info@mail.co.uk", "Error Log", $error . "<br />". $failures, "online");
    }
    
    
    $lines = file('mailshot-log.txt');
    $fopen = fopen("mailshot-log.txt", "w+"); 
    fwrite( $fopen, "The $template email template was sent to all previous $activity customers on<span class=\"font7\">: $time</span><br /><br />\r\n");
    foreach ($lines as $line) { fwrite( $fopen, "$line"); }
    fclose($fopen);
    
    
    header( "Location: http://www.mysite.co.uk/database/database.php");
    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its too difficult to follow poorly formatted code, so I went ahead and formatted this:
    PHP Code:
    $activity $_REQUEST['activity'];
    $template $_REQUEST['template'];

    /* Database config */

    $db_host        "localhost";
    $db_user        "user";
    $db_pass        "pass";
    $db_database    "database"
    $db_table       "testing";
    /* End config */

    $connectmail "info@mail.co.uk";
    $connectsubject "Contact form database";
    $connecterror "There was a problem with the online database connection <br /><br />The data is:<br /><br />Name: " $fname "<br />Email: " $from "Telephone: " $phone "Activiy: " $activity;
    $connectheader "From: online database";


    $con mysql_connect($db_host,$db_user,$db_pass) or mail($connectmail$connectsubject$connecterror "<br/><br />Error: Establishing a connection"$connectheader);


    mysql_select_db($db_database)  or mail($connectmail$connectsubject$connecterror "<br/><br />Error: Connecting to a database"$connectheader);



    $sql mysql_query("SELECT email FROM testing WHERE activity = '$activity' AND campaignsent = 'no' AND email REGEXP '^[^@]+@[^@]+\.[^@]{2,}$'");

    $recipients = array();
    while(
    $row mysql_fetch_array($sql)) {
        
    $recipients[] = $row['email'];

    }

    require_once 
    '../Swift-5.0.1/lib/swift_required.php';

    // Create the mail transport configuration
    $transport Swift_MailTransport::newInstance();

    // Create the message
    $message Swift_Message::newInstance();

    foreach(
    $recipients as $recipient){


        if (
    $template == "halloween"){

            
    $subject "Halloween";
            
    $message->setBody(
                    
    '<html>' .
                    
    ' <head></head>' .
                    
    ' <body>' .
                    
    ' <div align="center">' .
                    
    '  <img src="' // Embed the file
                    
    $message->embed(Swift_Image::fromPath('../images/halloween/images/halloween-advert.gif')) .
                    
    '" alt="Ancaster Leisure Halloween Laser Tag" />' .
                    
    '<br />Click <a href="http://www.mysite.co.uk/unsubscribe.php?removeemail=' $recipient '">Here</a> to unsubscribe' .
                    
    ' </div>' .
                    
    ' </body>' .
                    
    '</html>',
                    
    'text/html' // Mark the content-type as HTML
            
    );

        }
        elseif (
    $template == "karting"){

            
    $subject "day out";
            
    $message->setBody(
                    
    '<html>' .
                    
    ' <head></head>' .
                    
    ' <body>' .
                    
    ' <div align="center">' .
                    
    '  <img src="' // Embed the file
                    
    $message->embed(Swift_Image::fromPath('../images/karting-mail/images/karting.gif')) .
                    
    '" alt="outdoor venue" />' .
                    
    ' </div>' .
                    
    ' </body>' .
                    
    '</html>',
                    
    'text/html' // Mark the content-type as HTML
            
    );

        }
        
    $message->setSubject("$subject");


        
    $message->setFrom(array('info@mysite.co.uk' => 'John Smith'));
        
    $message->setReturnPath('info@mysite.co.uk');
        
    // Send the email



        
    $message->setTo($recipient);
        
    $mailer Swift_Mailer::newInstance($transport);




        
    $numSent $mailer->send($message);

        
    $sqlquery mysql_query("UPDATE testing SET campaignsent = 'no' WHERE email = '$recipient'");

    }

    if (!
    $mailer->send($message$failures))
    {
        
    mail("info@mail.co.uk""Error Log"$error "<br />"$failures"online");
    }


    $lines file('mailshot-log.txt');
    $fopen fopen("mailshot-log.txt""w+"); 
    fwrite$fopen"The $template email template was sent to all previous $activity customers on<span class=\"font7\">: $time</span><br /><br />\r\n");
    foreach (
    $lines as $line) { fwrite$fopen"$line"); }
    fclose($fopen);


    header"Location: http://www.mysite.co.uk/database/database.php"); 
    You have two calls to $mailer->send in here. The second is outside of the loop, and will only execute if the loop has iterated at least once since its in the improper location. It should only send an email to the last $recipient twice, all others should only receive once.
    I'd assume that logic is supposed to be inside of the foreach replacing the send call there.
    I don't know anything about the SwiftMail classes, but I would somehow assume that you shouldn't need to keep constructing new instances of it. Making one, and setting the recipient(s?) and sending should be sufficient.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,218
    Thanks
    12
    Thanked 338 Times in 334 Posts
    Quote Originally Posted by Fou-Lu View Post
    I don't know anything about the SwiftMail classes, but I would somehow assume that you shouldn't need to keep constructing new instances of it. Making one, and setting the recipient(s?) and sending should be sufficient.
    the SwiftMailer AFAIK uses a Singleton to prevent re-constructing. nevertheless, sending multiple emails is described in the documentation:
    - sending emails in a batch
    - sending personalised emails in a batch
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •