Sending my newsletter to 3000 databased clients

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
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Sending my newsletter to 3000 databased clients

Post by geddi »

Hi,

I am implementing swift 4 and have read the docs.

( BTW - very nice job on the docs - very clear )

Now when giving an example for sending the email
it says this:
//Send the message
$result = $mailer->send($message);

/*
You can alternatively use batchSend() to send the message

$result = $mailer->batchSend($message);
*/
I have about 2000 clients that I want to startt sending a newsletter to.

I want both the subject and the body to have the clients name included so this
will have to be taken out of the database table.

There does not appear to be any information about using the batchsend()

With batchsend() do I still need to use the the Decorator plugin ?

And for what I need do I need to do something similar to this:

Code: Select all

class DbReplacements implements Swift_Plugins_Decorator_Replacements {
  public function getReplacementsFor($address) {
    $sql = sprintf(
      "SELECT * FROM user WHERE email = '%s'",
      mysql_real_escape_string($address)
    );
    
    $result = mysql_query($sql);
    
    if ($row = mysql_fetch_assoc($result)) {
      return array(
        '{username}'=>$row['username'],
        '{password}'=>$row['password']
      );
    }
  }
}
Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Sending my newsletter to 3000 databased clients

Post by Chris Corbyn »

You are correct on both counts ;) batchSend() just loops through each address it needs to send to, throws away the existing To: header and sets one with just that single address in it before calling send() itself. The end result being that each recipient sees only their own address in the To: field, but internally everything goes via send() so plugins do still work.
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Re: Sending my newsletter to 3000 databased clients

Post by geddi »

Thanks for your reply,

BTW - I just found you info page about batchsend :)

I will read through it all and try and merge it with the script I wrote
that was using the php mail() function.

Thanks
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Re: Sending my newsletter to 3000 databased clients

Post by geddi »

OK - I just thought about your statement,
but internally everything goes via send() so plugins do still work.
I have tried to incorporate the new version 4 objects with my newsletter sending script.

Just to explain.
Before a member gets the "current" version of the newsletter, they get two introduction
newsletters which explain certain aspects, then the get the current edition.

To check to see if they need the intros a field is checked in the client table
- you will see this in the code.

Now the bits I am not sure about are:

1) I have called some of the objects just once at the top of the script
- hope thats right.

2) I have placed my variables in the objects like this:
$message->setSubject('$subject'); ???

3) On the text only version I have written new lines
like this: MESSAGE-TEXT = "Dear $contact,/n/n
Hi, /n ";

Again, I don't know if this is correct.

4)
I may not be accessing the array variable propery
to display my failures.

here
<span class= \"lk\">$Rctr ) Failed to Send Edn 1 to :$user_id, at $failures</span>
Hopefully you can point out my errors

Maybe this post will help others as well.

Thanks.


Here is my script:

Code: Select all

<?php 
/*  member_news.php
* 
*    Sends newsletter to members.
* 
*/
@session_start();
 
if (@$_SESSION['auth'] != "yes" ){
     header("Location: /im/index.php");
     exit();
}  // end if
 
// Below is just to set up the display of this page
$page="control";
$title1 ="Control Panel"; 
$desc = "Controlling your";
require_once("mem_head.php");
// *********************************
 
require_once("my_functions.php");
require_once("bbcode_email.php");       
require_once 'lib/swift_required.php';
 
$transport = Swift_SmtpTransport::newInstance('smtp.my-site.com', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;
    
$mailer = Swift_Mailer::newInstance($transport);
 
?>   
 
<div class="page_name">
<h1>Control Panel</h1>
</div> <!-- End div: page_name --> 
 
<div class="pg_whole">
<span class="pg_head">Send Newsletter to Members.</span>
<br><br>
 
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<strong>Do you really want to send the newsletter? </strong>
<input type="submit" value="Yes" name="submit_email">
<input type="button" value="No">
</form>
 
<?php
if (isset($_POST['submit_email'])) {
$sql = " SELECT * FROM newsletters WHERE confirm = 'y' and status = 'c' and type = 'm'";
$result = mysql_query($sql) or die("could not execute find Newsletters query". mysql_error());  
$num = mysql_num_rows($result);
  if ($num == 0 ) {
    $temp_body = "There is no confirmed, current members newsletter !";
    $link1 = "Error: No Confirmed, New Members Newsletter !";
        echo "<div class=\"listerdiv\">
    <span class= \"lk\">$link1</span>
        <span class= \"by\">$temp_body</span>
        </div>";
        require_once("footer.php"); 
        exit();
        }  // endif 
  else{       
      while($row = mysql_fetch_assoc($result)){
        $cur_nws_id = $row[news_id];
        $cur_nws_hd = $row[news_head];
          $cur_nws_bd = $row[news_body];
            }  // end while
         }  // end else
        
$sql = " SELECT * FROM clients WHERE (type =  'm' || type =  'E') AND confirm = 'y'";
$result = mysql_query($sql) or die("could not execute find Clients query". mysql_error());  
$num = mysql_num_rows($result);
  if ($num == 0 ) {
    $temp_body = "There are no clients !";
    $link1 = "No New Members!";
        echo "<div class=\"listerdiv\">
    <span class= \"lk\">$link1</span>
        <span class= \"by\">$temp_body</span>
        </div>";
        require_once("footer.php"); 
        exit();
        }  // endif 
  else{       
 
echo "<div class=\"ctrl_lister\">
<span class= \"lk\">Starting to process clients and send newsletters ...</span>
</div>";    
 
    $Rctr = 1;
    while($row = mysql_fetch_assoc($result)){
      extract($row);
$join_dt = date('j F Y',$create_date);  
 
// Now we check to see if they need need to 
// have the first intro newsletter
// - $eml comes from the client record
 
if ($eml == 1) { 
 
$subject = "$contact: As Promised ...no. 1 Blah blah.";
 
$message = "<b>Blah blah Newsletter. No. 1</b><br>
Dear $contact,
 
Hi, 
Intro number one
Blah blah Blah blah
Blah blah Blah blah";
 
$message-text = "Blah blah Newsletter. No. 1/n
Dear $contact,/n/n
Hi, /n 
Intro number one /n
Blah blah Blah blah/n
Blah blah Blah blah";
 
 
/* 
*   The code below will go into a separate php file called news_send.php
*/
 
Swift_Message::newInstance()
 
// set the sender
$message->setFrom(array('Editor@my-site.com' => 'James Burke'));
 
// set the recipient
$message->setTo(array('$email' => '$contact'));
 
// set the subject
$message->setSubject('$subject');
 
// set the html body
$message->setBody('$message', 'text/html');
 
// set the text body
$message->addPart('$message-text', 'text/plain');
 
//Send the message
if (!$mailer->send($message, $failures)) {
    echo "<div class=\"ctrl_lister\">
        <span class= \"lk\">$Rctr ) Failed to Send Edn 1 to :$user_id, at $failures</span>
        </div>";
        } // end if
else
   // update database
     $sql = "UPDATE clients SET eml = eml+1 WHERE user_id = '$user_id'";
     mysql_query($sql) or die("could not execute adverts update query". mysql_error()); 
 
      echo "<div class=\"ctrl_lister\">
    <span class= \"lk\">$Rctr ) Sent Edn 1 to :$user_id, Type: $type, Contact: $contact, Joined:$join_dt</span>
    </div>";
    } // end else
 
$Rctr = $Rctr +1;           
/*
*  end of news_send.php
*/
 
} // end if
 
 
// Now we check to see if they need need to 
// have second intro newsletter
// - $eml comes from the client record
 
if ($eml == 2) {
 
$subject = "$contact: As Promised ...no. 2 Blah blah.";
 
$message = "<b>Blah blah Newsletter. No. 2</b><br>
Dear $contact,
 
Hi, 
Intro number two
Blah blah Blah blah
Blah blah Blah blah";
 
$message-text = "Blah blah Newsletter. No. 2/n
Dear $contact,/n/n
Hi, /n 
Intro number two /n
Blah blah Blah blah/n
Blah blah Blah blah";
 
 
require("news_send.php");
       
} // end if
 
// Now we check to see if they need need to 
// have the CURRENT newsletter
//  - $eml comes from the client record
 
if ($eml > 2) { 
$subject = "$contact: ".$cur_nws_hd;
 
$message = "Dear $contact: <br>".$cur_nws_bd;
$message=nl2br($message);
$message=eml_bbcode($message);
$message-text = "none yet";
 
require("news_send.php");
         
$sent_curr = "y";
        
  } // end if
 }  // end while
 
  if($sent_curr == "y") {   
     // update client table
     $sql = "UPDATE newsletters SET 
         confirm = 's',
         status = 's',
         sent_date = '$today'
         
         WHERE news_id = '$cur_nws_id'";
         
     mysql_query($sql) or die("could not execute newsletters update query". mysql_error()); 
   } // end if
 
 } // end else
 
} // end if
 
?>
 
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Re: Sending my newsletter to 3000 databased clients

Post by geddi »

I would really appreciate a few pointers on my scripting attempt
in the above post.

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Sending my newsletter to 3000 databased clients

Post by Chris Corbyn »

I haven't had a time to decipher everything here (the script is very twisty and complicated to follow), but some things I picked up on when looking over it.

a) It's \n, not /n
b) $message->setSubject('$subject'); will not work. $message->setSubject($subject) or $message->setSubject("$subject") will though.
c) $message->setBody('$body'); won't work for the same reason as above (single quotes can't be used around the variable)
d) $message-text is not a valid variable name.
e) You can't echo an array. You can implode() it or loop over it though.


I think you need to learn some basic PHP in order to correctly use Swift Mailer. All of these are very basic errors.
geddi
Forum Commoner
Posts: 39
Joined: Sun Feb 24, 2008 11:01 am

Re: Sending my newsletter to 3000 databased clients

Post by geddi »

Thanks for taking the time to reply.

I have fixed those points and I now have a working code.

Because I don't use OOP, only proceedural php, I am not familiar with
the $message->setSubject('subject'); type of syntax.
Post Reply