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
    66
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Swift mailer loops twice

    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
    Regular Coder Nerevarine's Avatar
    Join Date
    Jan 2013
    Location
    Phendrana Drifts, Tallon IV, W-Class
    Posts
    285
    Thanks
    0
    Thanked 17 Times in 17 Posts
    Complete wrong section of the site, dude. This is Flash/ActionScript, that's (I'm guessing) PHP/MySQL?
    Time kills us in our sleep and we watch it happen in our dreams. -K.K.
    THE END-ALL PROGRAMMING REFERENCE: CLICK HERE (Courtesy of Major Payne)
    My username was previously L0adOpt1c. :: Please read this before posting in the HTML/CSS forum.
    Validate your HTML here and your CSS here. :: Need basic HTML/CSS tutorials? Click here, don't post about it.

  • #3
    New Coder seanmarkham's Avatar
    Join Date
    Dec 2011
    Location
    Lincolnshire, England
    Posts
    66
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Apologies, have put a thread in PHP
    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development


  •  

    Posting Permissions

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