PDA

View Full Version : array() problem sending mass email with swiftmailing


kigoobe
07-10-2006, 10:21 AM
Hi guys,

well, swift mail is having a script that works like this: $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, $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: 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).

mlse
07-10-2006, 03:27 PM
Hi,

The problem is that you have resolved the string $message before passing it to the mail function - your array $thename is resolved to the string "Array" during the assignment $message = '<p>Hi '.$thename.'</p>';

The solution you wish to use seems fine (i.e. using swiftmailer), personally I've not heard of it before, I just use the bog standard mail() built-in function. The problem is that you want to send a different email body to each user and so whichever solution you chose, you will have to send out a separate email to each user.

I hope that helps!

Mike.

raf
07-10-2006, 04:05 PM
welcome here, kigoobe!

if you want to personallise the emails, like adding the recepients name in the message, then you'll need to use something like

if ($mailer->isConnected()){
$result = mysql_query("SELECT name, surname, email from $my_table");
if (mysql_num_rows($result) >=1){
$subject = 'Some subject';
$sender = '"George Bush" <george@bush.com>';
$message_all = stripslashes($_POST['themessage']);
while ($row = mysql_fetch_assoc($result)) {
$recipients = array($row['name'].' '.$row['surname'], $row['email']);
$message = '<p>Hi '.$row['name']) .'</p>'. $message_all ;
$mailer->send(
$recipients,
$sender,
$subject,
$message);
}
}
}
$mailer->close();

with some extra checks on the posted message to prevent header-injections of course...

kigoobe
07-10-2006, 07:52 PM
Thanks guys. Well, mlse, I was also thinking something as you have told, but I wanted to be sure about that, if I can't send different mails to everyone thro' an array.

Raf, $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. :( Weird, isn't it?)

raf
07-10-2006, 09:43 PM
Raf, $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. :( Weird, isn't it?)
$message = '<p>Hi '.$row['name']) .'</p>'. $message_all ;
should be
$message = '<p>Hi '. $row['name'] .'</p>'. $message_all ;

else try just echoing out the name --> maybe it's something to do with the mailingclass that you use...

$result = mysql_query("SELECT name, surname, email from $my_table");
while ($row = mysql_fetch_assoc($result)) {
echo '<br />'. $row['name'];
}