sending different data to multiple email

I tried to query a table and retrieve some data, then I email them to an indivisual person with different data. However, I am only to send the 1st email, but not 2nd email. My query seem that not able to pass the 1st data found and pass it for 2nd time quer. Here is my code:

<?php require "connection.php"; $today = date("Y-m-d"); $query1 = mysql_query("SELECT no FROM invno WHERE date='$today'"); while ($row1 = mysql_fetch_array($query1, MYSQL_NUM)){ $no=$row1[0]; $headers .= "From: Your Valueble Hosting Partner \n"; $headers .= "X-Sender: \n"; $headers .= "X-Mailer: PHP\n"; // mailer $headers .= "X-Priority: 1\n"; // Urgent message! $headers .= "Return-Path: \n"; // Return path for errors /* If you want to send html mail, uncomment the following line */ $headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type $query = mysql_query("SELECT * FROM invoice WHERE no='$no'"); while ($row = mysql_fetch_array($query, MYSQL_NUM)){ $domain=$row[1]; $cname=$row[5]; $email=$row[7]; $expire=$row1[4]; $total=$row[9]; $amount=$row[12]; $id=$row[13]; $price=$total+$amount; } /* subject */ $subject="Your http://$domain Invoice"; /* recipients */ $recipient .= "$email"; /* message */ $message .= "Hello! $cname,

"; $message .= "Your domain http://$domain is going to be expired on $expire.
"; $message .= "Please click here to view your Invoice.

"; $message .= "Your TOTAL invoice due is $price.
"; $message .= "You can pay your invoice here or
"; $message .= "bank in your payment as below information that we provided:-

"; $message .= "----------------------------------------------------
"; $message .= "Banker: ABC Bank
"; $message .= "Account No: 123456
"; $message .= "Company Name: ABC Hosting
"; $message .= "Payment Due: $price
"; $message .= "----------------------------------------------------

"; $message .= "Please make your payment by the due date to advoid service interraption.
"; $message .= "If you have paid, pls ignore the email.

"; $message .= "Thank you for your business.
"; $message .= "Have a nice day!


"; $message .= "Remarks: This email is computer generated document.



"; $message .= "Regards,
"; $message .= "Sales Team of tech Hosting Service"; // start while // we use the sql var. $row and we use the field 0 to pickup the email address //echo "$domain $row[0]
"; mail($recipient, $subject, $message, $headers); } ?>

You have to set a message for each recipient and use individual mail functions for each one. That’s the simplest way of doing it. You could also set the message into an array, along with who its going to, the subject and headers, then use a foreach loop to send the emails out.

any example?

I hv modified the code, but the output shown like this:-


Hello! gbplas,

Your domain http://gbplas.domain.net is going to be expired on 31/12/2013.
Please “click here” to view your Invoice. (“click here” - actual with hyperlink)

Your TOTAL invoice due is $50.
You can pay your invoice “here” or (“here” - actual with hyperlink)
bank in your payment as below information that we provided:-


Banker: abc Bank
Account No: 123456
Company Name: abc Company
Payment Due: $50

Please make your payment by the due date to advoid service interraption.
If you have paid, pls ignore the email.

Thank you for your business.
Have a nice day!

Remarks: This email is computer generated document.

Regards,
Sales Team of abc Hosting Service

Hello! gbplas,

Your domain http://gbplas.domain.net is going to be expired on 31/12/2013.
Please “click here” to view your Invoice. (“click here” - actual with hyperlink)

Your TOTAL invoice due is $50.
You can pay your invoice “here” or (“here” - actual with hyperlink)
bank in your payment as below information that we provided:-


Banker: abc Bank
Account No: 123456
Company Name: abc Company
Payment Due: $50

Please make your payment by the due date to advoid service interraption.
If you have paid, pls ignore the email.

Thank you for your business.
Have a nice day!

Remarks: This email is computer generated document.

Regards,
Sales Team of abc Hosting Service

Hello! william,

Your domain http://william.domain.net is going to be expired on 12/1/2014.
Please “click here” to view your Invoice. (“click here” - actual with hyperlink)

Your TOTAL invoice due is $150.
You can pay your invoice “here” or (“here” - actual with hyperlink)
bank in your payment as below information that we provided:-


Banker: abc Bank
Account No: 123456
Company Name: abc Company
Payment Due: $150

Please make your payment by the due date to advoid service interraption.
If you have paid, pls ignore the email.

Thank you for your business.
Have a nice day!

Remarks: This email is computer generated document.

Regards,
Sales Team of abc Hosting Service

I received 2 emails for this since there are 2 outouts after queried Mysql. The 1st emails received with everything ok, but 2nd emails with 2 content included the 1st email content. So can anyone point me which part of my code is wrong?

Dear Richei, I don’t get you since I am a poor php writer.

What does your code look like since you updated it?

When you loop through sending the email it is adding to the message. After the email is sent ( before your loop restarts) clear the fields for the recipient and message. Then when your loop restarts it will be fresh and take in the message and recipients like it was the first email on every iteration.

astonecipher , u r right. I need to clear the email address such as $mail->ClearAddresses(); Now the problem is solved by using phpmailer.[php]<?php

require(‘connection.php’);
require(‘class.phpmailer.php’);
include(“class.smtp.php”); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = “mail.abc.com”; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "[email protected]"; // SMTP account username
$mail->Password = “password”; // SMTP account password
$mail->From = "[email protected]";
$mail->FromName = “Your valuable Hosting Partner”;
$mail->AddReplyTo(‘[email protected]’, ‘Your valuable Hosting Partner’);

$today = date(“Y-m-d”);

$query = mysql_query(“SELECT * FROM invoice WHERE date=’$today’”);

while ($row = mysql_fetch_array ($query)) {

                                            $domain=$row[1];
                                            $no=$row[0];
                                            $email=$row[7];    
                                            $cname=$row[5];                          
                                            $expire=$row[4];
			                $total=$row[9];
                                            $amount=$row[12];				               
                                            $id=$row[13];        
                                            $price=$total+$amount;

$mail->Subject =“Your http://$domain Invoice”;

$mail->Body = “Hello! $cname,


Your domain http://$domain is going to be expired on $expire.

Please <a href=http://abc.com/php/invout.php?no=$no&id=$id>click here to view your Invoice.


Your TOTAL invoice due is $price.

You can pay your invoice <a href=details.php?option2=user&id=$noticia[id]>here or

bank in your payment as below information that we provided:-


----------------------------------------------------

Banker: abc Bank

Account No: 123456789

Company Name: abc Company

Payment Due: $price

----------------------------------------------------


Please make your payment by the due date to avoid service interruption.

If you have paid, pls ignore the email.


Thank you for your business.

Have a nice day!



Remarks: This email is computer generated document.




Regards,

Sales Team of abc Hosting Service”;

//$mail->WordWrap = 50;

$mail->AddAddress($email, $cname);

if(!$mail->Send()) {

echo “Mailer Error (” . str_replace("@", “@”, $email) . ') ’ . $mail->ErrorInfo . ‘
’;

} else {

echo “Message sent to :” . $cname . ’ (’ . str_replace("@", “@”, $email) . ‘)
’;

}

// Clear all addresses and attachments for next loop
$mail->ClearAddresses();

}

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service