Page 1 of 5

array() problem sending mass email with swiftmailing

Posted: Mon Jul 10, 2006 3:28 am
by kigoobe
Hi guys,

well, swift mail is having a script that works like this:

Code: Select all

$subject = 'Some subject';
$sender = '"George Bush" <george@bush.com>';
$recipients = array();
$result = mysql_query("SELECT name, surname, email from $my_table");
while ($row = mysql_fetch_assoc($result)) {
$recipients[] = array($row['name'].' '.$row['surname'], $row['email']);
}
$message = stripslashes($_POST['themessage']);
if ($mailer->isConnected())
{
	$mailer->send(
		$recipients,
		$sender,
		$subject,
                          $message
	);
	$mailer->close();	
}
This works fine. Now I wanted to add something like Hi .$row[name] at the beginning of every mail send. So, I have tried to modify this script in this way,

Code: Select all

$subject = 'Some subject';
$sender = '"George Bush" <george@bush.com>';
$recipients = array();
$thename = array();
$result = mysql_query("SELECT name, surname, email from $my_table");
while ($row = mysql_fetch_assoc($result)) {
$recipients[] = array($row['name'].' '.$row['surname'], $row['email']);
$thename[] = array($row['name']);
$message = '<p>Hi '.$thename.'</p>';
}
$message .= stripslashes($_POST['themessage']);
if ($mailer->isConnected())
{
	$mailer->send(
		$recipients,
		$sender,
		$subject,
                          $message
	);
	$mailer->close();	
}
Unfortunately, when the email is going, senders are getting this:

Code: Select all

Hi Array
Message goes here ...
Can anyone help me to figure out where I'm doing something wrong?
Thanks a lot.

PS. I think I am not even sure if it's possible. Swiftmailer is sending the mail once only for all the addresses, taking all the addresses in an array. Had it been sending seperate mail to each, this could have been possible, as in mail(). I still would like to see what other's think, and may be Swiftmailer is not the best solution to send mass mail in that case (and in this way).

PS2. I just read the interesting story of MsGruff in http://www.phpbuilder.com/board/archive ... 12185.html ... well, nice that things have been solved ... and if Chris is back again as a moderator of this site, may be he could tell if sending personalized mass mail as I am trying would be possible with Swiftmailing, as he is the author of that code ... :) Thanks.

Posted: Mon Jul 10, 2006 4:39 am
by Jenk
$thename is an array, containing an array with one indice, which contains the value of $row['name'];

a print_r would look similar to:

Code: Select all

array (
    [0] => array (
        [0] => 'name'
        )
    )
)
Change the line:

Code: Select all

$thename[] = array($row['name']);
to

Code: Select all

$thename = $row['name'];
HTH :)

Posted: Mon Jul 10, 2006 4:47 am
by kigoobe
Thanks Jenk. But it's giving me a name from the bottom most row of my db, for all the mails ... :wink:

Posted: Mon Jul 10, 2006 5:27 am
by Jenk
Then you'll need to change the mailing script to iterate through the $thename array. However, you will still need to change

Code: Select all

$thename[] = array($row['name']);
to just

Code: Select all

$thename[] = $row['name'];

Posted: Mon Jul 10, 2006 5:42 am
by kigoobe
Jenk wrote:Then you'll need to change the mailing script to iterate through the $thename array.
Right, this is what I need ...

Posted: Mon Jul 10, 2006 5:52 am
by Jenk
dude.. it's not going to be a difficult change.. the script must already iterate with the other arrays.. have you tried?

Posted: Mon Jul 10, 2006 5:55 am
by kigoobe
Not really Jenk, I am using Swift mailing, and couldn't find where the script iterating already ... :(

Well, the two files that're doing all the work are:

Pimptastic | *SNIP* | No need to post the entire application code

Posted: Mon Jul 10, 2006 6:12 am
by technofreak
There is a better way to post long lines of codes. Use a 'pastebin' and give the link in the forum :)

Posted: Mon Jul 10, 2006 6:21 am
by Jenk
ugh.. what a mess..

My advice: use a different mailer package, or create your own.

Posted: Mon Jul 10, 2006 7:03 am
by Weirdan
PS2. I just read the interesting story of MsGruff in http://www.phpbuilder.com/board/archive ... 12185.html ... well, nice that things have been solved ... and if Chris is back again as a moderator of this site, may be he could tell if sending personalized mass mail as I am trying would be possible with Swiftmailing, as he is the author of that code ... Smile Thanks.
McGruff and Chris are different people (Chris is known here as d11wtq)

Posted: Mon Jul 10, 2006 7:09 am
by jayshields
Weirdan wrote:
PS2. I just read the interesting story of MsGruff in http://www.phpbuilder.com/board/archive ... 12185.html ... well, nice that things have been solved ... and if Chris is back again as a moderator of this site, may be he could tell if sending personalized mass mail as I am trying would be possible with Swiftmailing, as he is the author of that code ... Smile Thanks.
McGruff and Chris are different people (Chris is known here as d11wtq)
Yeah, he knows that. He's referring to that thing d11 posted on phpbuilder ages ago when McGruff did whatever with the site and kicked all the mods off, I assume this guy doesn't know that issue was resolved a good while ago and d11 has been active again here for a while :)

Posted: Mon Jul 10, 2006 8:50 am
by Chris Corbyn
Ok enough on past events, that's all behind us now.

Your problem is not Swift related, but rather your use of the array:

Code: Select all

$message = '<p>Hi '.$thename.'</p>';
Should be

Code: Select all

$message = '<p>Hi '.$row['name'].'</p>';
PS: ~Jenk -- thanks :twisted:

Posted: Mon Jul 10, 2006 9:00 am
by Chris Corbyn
To followup, if you're trying to change the email slightly for each address you send to in a batch you might want to play around with this simple, untested template plugin I put together last week.... it's hardly tested so I can't really offer much support for it in this state but it does seem to work, at least for basic replacements.

Code: Select all

<?php

class Swift_Template_Plugin
{
   var $pluginName = 'Template';
   var $templateVars = array();
   var $swiftInstance;
   var $template = '';
   var $count = 0;
     //2-dimensional
   // First level MUST be numerically indexed starting at zero
   // Second level contains the replacements
   function Swift_Template_Plugin($template_vars=array())
   {
       $this->templateVars = $template_vars;
   }
     function loadBaseObject(&$object)
   {
       $this->swiftInstance =& $object;
   }
     //Split the headers from the mail body
   function getTemplate()
   {
       return substr($this->swiftInstance->currentMail[3], strpos($this->swiftInstance->currentMail[3], "\r\n\r\n"));
   }
     function getHeaders()
   {
       return substr($this->swiftInstance->currentMail[3], 0, strpos($this->swiftInstance->currentMail[3], "\r\n\r\n"));
   }
     function onBeforeSend()
   {
       if (empty($this->template)) $this->template = $this->getTemplate();
             foreach ($this->templateVars[$this->count] as $key => $replacement)
       {
           $this->swiftInstance->currentMail[3] = $this->getHeaders().str_replace('{'.$key.'}', $replacement, $this->template);
       }
   }
}

?>
Usage:

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

require('Swift_Template_Plugin.php');

$mailer = new Swift(new Swift_SMTP_Connection('smtp.host.tld'));

$addresses = array (
 '"Chris Corbyn" <chris@w3style.co.uk>',
 '"Chris Corbyn2" <chris@w3style.co.uk>',
 '"Chris Corbyn3" <chris@w3style.co.uk>'
);

$tpl = array (
 array ('foo' => 'First email'),
 array ('foo' => 'second email'),
 array ('foo' => 'third email')
);

$mailer->loadPlugin(new Swift_Template_Plugin($tpl));


$mailer->send(
        $addresses,
        '"Your name" <you@yourdomain.com>',
        'Some Subject',
        "It's only me! and this is my {foo}!!!"
);

$mailer->close();

?>

Posted: Mon Jul 10, 2006 9:49 am
by Jenk
d11wtq wrote:PS: ~Jenk -- thanks :twisted:
lol, my apologies.. I was actually referring to the post containing shedloads of code and with it wrapping as well (low res here at work) it's not easy on the eye.

My recommendation to use a different package was simply because I couldn't see where or how Swift is capable of mass mailing with different body for each message. :)

Posted: Mon Jul 10, 2006 12:53 pm
by kigoobe
Thanks guys for your kind replies ... Chris, $row['name'] is giving me the last row name from my db, for all the users. Like if I have 20 users in my database, and if the newest person signed up is Mike, all of the 20 guys are getting a mail where they are addresses as Hi Mike.

I will try the template plugin now, may be that is the only somution ... :( :( Let's see. Thanks again for all the help ... :) :)