Issue sending to users in Database

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
hoopplaya4
Forum Newbie
Posts: 9
Joined: Wed Dec 17, 2008 6:16 pm

Issue sending to users in Database

Post by hoopplaya4 »

Hello all,

I'm trying to send a simple email to all users in my mySQL database. Here is what I currently have:

Code: Select all

 
error_reporting(E_ALL);
 
echo "Please be patient...";
 
    $html = stripslashes($_POST["bpost"]);
    $subj = stripslashes($_POST["subjectpost"]);
    $address = $_POST['address'];
    $fullname = $_POST['fullname'];
     
    require_once "../lib/Swift.php";
    require_once "../lib/Swift/Connection/SMTP.php";
    require_once "../lib/Swift/Plugin/Decorator.php";
    include("../connection.php");  //database connection
 
if ($_POST['entire'] == "yes") {
 
$smtp =& new Swift_Connection_SMTP("smtp.host.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
    $smtp->setUsername("username");
    $smtp->setpassword("password");
    
    $swift =& new Swift($smtp);
    
    $recipients =& new Swift_RecipientList();
    
      $sql = "SELECT usrID, usrEmail as '[name]', usrFirst, UsrLast";
      $sql .= " FROM tblUsers AS ";
      $sql .= " WHERE usrActive = '1'";
      $sql .= " ORDER BY usrEmail";
 
        $result=mysql_db_query($DBname,$sql,$link);
 
         while ($row=mysql_fetch_array($result)) {
 
      $recipients->addTo($row['usrEmail']);
 
     
    $bod = $html;
    $bod .= "<br>Your email is address is: [name]";
    $message =& new Swift_Message($subj, $bod, "text/html");
 
    $swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
}
 
  if ($swift->batchSend($message, $recipients, new Swift_Address($address, $fullname))) {
   print" <script>
  window.location=\"emailUsers.php?msg=1\"
  </script> ";
}
  else {
   print ("<p>No go!</p>");
}
    $swift->disconnect();
 
} //end IF
 
 
I'm not quite sure what I am doing incorrectly. Here is the following error(s) that I receive:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/kylerenk/public_html/pacificbb2/secure/swift.php on line 36

Notice: Undefined variable: message in /home/user/public_html/usr/secure/swift.php on line 48

Catchable fatal error: Argument 1 passed to Swift::batchSend() must be an instance of Swift_Message, null given, called in /home/user/public_html/usr/secure/swift.php on line 48 and defined in /home/user/public_html/usr/lib/Swift.php on line 484
Any help is much appreciated!
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Issue sending to users in Database

Post by sergio-pro »

Try changing your SQL to:

Code: Select all

 
       $sql = "SELECT usrID, usrEmail, usrFirst, UsrLast";
       $sql .= " FROM tblUsers ";
       $sql .= " WHERE usrActive = '1'";
       $sql .= " ORDER BY usrEmail";
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Issue sending to users in Database

Post by Chris Corbyn »

You're using the decorator plugin wrong too. You are repeatedly attaching it to the mailer so you're basically overwriting it each time. You need to attach it *after* your loop, once you've collected all recipients into an array.
hoopplaya4
Forum Newbie
Posts: 9
Joined: Wed Dec 17, 2008 6:16 pm

Re: Issue sending to users in Database

Post by hoopplaya4 »

Thanks for the replies. Let me give it a try, and I'll report back.
hoopplaya4
Forum Newbie
Posts: 9
Joined: Wed Dec 17, 2008 6:16 pm

Re: Issue sending to users in Database

Post by hoopplaya4 »

Thank you for the replies. That helped tremendously, as I was able to get the emails to send!

However, I now have a question in trying to "customize" each email using the decorator. I am trying to put a "for each" loop within the "while" statement. However, I'm not sure if I am putting this in the correct place. Sorry if this is something easy, I'm still learning! Here's what I currently have:

Code: Select all

 
if ($_POST['entire'] == "yes") {
 
$smtp =& new Swift_Connection_SMTP("smtp.host.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
    $smtp->setUsername("username");
    $smtp->setpassword("password");
   
    $swift =& new Swift($smtp);
   
    $recipients =& new Swift_RecipientList();
 
       $sql = "SELECT usrID, usrEmail, usrFirst, UsrLast";
       $sql .= " FROM tblUsers ";
       $sql .= " WHERE usrActive = '1'";
       $sql .= " ORDER BY usrEmail";
 
        $result=mysql_db_query($DBname,$sql,$link);
 
         while ($row=mysql_fetch_array($result)) {
 
    $names = $row['usrEmail'];    
          foreach ($names as $n) {
           $recipients->addTo($names);
           $replacements[$n] = array('[name]' => $n);
      }
         
    $bod = $html;
    $bod .= "<br>Your email is address is: [name]";
    $message =& new Swift_Message($subj, $bod, "text/html");
} 
    $swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
 
 
  if ($swift->batchSend($message, $recipients, new Swift_Address($address, $fullname))) {
   print" <script>
 window.location=\"emailUsers.php?msg=1\"
 </script> ";
}
  else {
   print ("<p>No go!</p>");
}
    $swift->disconnect();
 
} //end IF
 
And now I get the following error:
Warning: Invalid argument supplied for foreach() in /home/user/public_html/usr/secure/swift.php on line 40
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Issue sending to users in Database

Post by Chris Corbyn »

You're trying to foreach over a string which isn't going to work. You still haven't quite grasped how to use the decorator ;) I'll see if I can fix up your code.

The objective is to use just *one* message object and *one* plugin to target multiple users. If you place anything inside a loop it will happen multiple times, so here you're creating multiple message objects.

Code: Select all

$replacements = array(); //Create an empty array *before* the loop
 
while ($row=mysql_fetch_array($result)) {
  //Add to the replacements array *in* the loop
  $replacements[$row['usrEmail']] = array('[name]' => $row['usrEmail']);
  $recipients->addTo($row['usrEmail']);
}
 
//Add the plugin *after* the loop (now that $replacements is full)
$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
 
//Create the message *outside* of the loop
$bod = $html;
$bod .= "<br>Your email is address is: [name]";
$message =& new Swift_Message($subj, $bod, "text/html");
 
//Send the message
if ($swift->batchSend($message, $recipients, new Swift_Address($address, $fullname))) {
 // ... etc ...
 
hoopplaya4
Forum Newbie
Posts: 9
Joined: Wed Dec 17, 2008 6:16 pm

Re: Issue sending to users in Database

Post by hoopplaya4 »

Ahh, I see!! :D It now makes sense to me. Usually, I can pick these things up a bit quicker, but I'm still trying to get my mind into "work" mode after the holidays.

Thanks very much, Chris, for all of your help! I appreciate your "comments" in the code as well, it helped me grasp the concept much easier.
IshaDakota
Forum Newbie
Posts: 8
Joined: Sun Jan 18, 2009 8:01 am

Re: Issue sending to users in Database

Post by IshaDakota »

I have a related question (I hope this isn't seen as hijacking the thread).

I just started using swift mailer (with great success) after simply using the PHP mail command for my applications. In my previous applications I would do just as the original poster and create a new message object in each instance of the foreach loop. I see that the decorator plugin will allow me to do the same work with one message object.

My questions is what is the advantage of using the plugin? Is it simply speed or is it more than that?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Issue sending to users in Database

Post by Chris Corbyn »

IshaDakota wrote:My questions is what is the advantage of using the plugin? Is it simply speed or is it more than that?
Not speed as such. You can use one message object over and over (and in fact that's recommended) in any case, just replacing the bits that you need to change (e.g. the subject, or the body). It's more about convenience.

However, there's more than one way to skin a cat. You can solve this same problem in a number of ways.
Post Reply