array() problem sending mass email with swiftmailing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

array() problem sending mass email with swiftmailing

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post by kigoobe »

Thanks Jenk. But it's giving me a name from the bottom most row of my db, for all the mails ... :wink:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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'];
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post 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 ...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

dude.. it's not going to be a difficult change.. the script must already iterate with the other arrays.. have you tried?
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post 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
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

There is a better way to post long lines of codes. Use a 'pastebin' and give the link in the forum :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

ugh.. what a mess..

My advice: use a different mailer package, or create your own.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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();

?>
Last edited by Chris Corbyn on Thu Nov 09, 2006 12:11 pm, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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. :)
kigoobe
Forum Commoner
Posts: 38
Joined: Mon Jul 10, 2006 3:26 am

Post 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 ... :) :)
Post Reply