Page 1 of 1

Issue sending to users in Database

Posted: Sat Jan 03, 2009 11:52 am
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!

Re: Issue sending to users in Database

Posted: Sat Jan 03, 2009 2:32 pm
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";
 

Re: Issue sending to users in Database

Posted: Sat Jan 03, 2009 7:49 pm
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.

Re: Issue sending to users in Database

Posted: Sat Jan 03, 2009 8:32 pm
by hoopplaya4
Thanks for the replies. Let me give it a try, and I'll report back.

Re: Issue sending to users in Database

Posted: Sat Jan 03, 2009 9:45 pm
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

Re: Issue sending to users in Database

Posted: Sun Jan 04, 2009 12:23 am
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 ...
 

Re: Issue sending to users in Database

Posted: Sun Jan 04, 2009 11:14 am
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.

Re: Issue sending to users in Database

Posted: Sun Jan 18, 2009 10:22 am
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?

Re: Issue sending to users in Database

Posted: Mon Jan 19, 2009 10:36 pm
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.