Reputation: 59
I'm currently working on a php mysql base system that will send email to multiple recipients from database . Im already search in forum and a lot of answer is using loop to email recipient. And then i'm try using looping in email recipient,but just able to send 1 email (although in database have a 5 or more email recipient).Can you tell me ,where my code is wrong? Below is my code :
function send_message( $from, $to, $subject, $message_content )
{
require_once "function.php";
require_once( 'phpmailer/PHPMailerAutoload.php' );
//Initiate the mailer class
$mail = new PHPMailer();
//Check to see if SMTP creds have been defined
if( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) && defined( 'SMTP_LOCATION' ) && defined( 'SMTP_PORT' ) )
{
$mail->IsSMTP();
$mail->Host = SMTP_LOCATION;
$mail->SMTPAuth = true;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
if( defined( 'DEBUG' ) && DEBUG )
{
$mail->SMTPDebug = 1;
}
}
//Set the sender and receiver email addresses
$alamatmail=get_mail();
foreach ( $alamatmail as $datamail):
$from="[email protected]";
$to=$datamail['email'];
//Include the phpmailer files
$mail->SetFrom( $from, "" );
//We 'can' send to an array, in which case you'll want to explode at comma or line break
if( is_array( $to ) )
{
foreach( $to as $i )
{
$mail->addAddress( $i );
}
}
else
{
$mail->AddAddress( $to, "" );
}
//Set the message subject
$mail->Subject = $subject;
//Add the message header
$message = file_get_contents( 'email-templates/email-header.php' );
//Add the message body
$message .= file_get_contents( 'email-templates/email-body.php' );
//Add the message footer content
$message .= file_get_contents( 'email-templates/email-footer.php' );
//Replace the codetags with the message contents
$replacements = array(
'({message_subject})' => $subject,
'({message_body})' => nl2br( stripslashes( $message_content ) ),
);
$message = preg_replace( array_keys( $replacements ), array_values( $replacements ), $message );
//Make the generic plaintext separately due to lots of css and tables
$plaintext = $message_content;
$plaintext = strip_tags( stripslashes( $plaintext ), '<p><br><h2><h3><h1><h4>' );
$plaintext = str_replace( array( '<p>', '<br />', '<br>', '<h1>', '<h2>', '<h3>', '<h4>' ), PHP_EOL, $plaintext );
$plaintext = str_replace( array( '</p>', '</h1>', '</h2>', '</h3>', '</h4>' ), '', $plaintext );
$plaintext = html_entity_decode( stripslashes( $plaintext ) );
//Send the message as HTML
$mail->MsgHTML( stripslashes( $message ) );
//Set the plain text version just in case
$mail->AltBody = $plaintext;
$failed_error="email gagal dikirim";
//Display success or error messages
if( !$mail->Send() )
{
return 'Message send failure: ' . $mail->ErrorInfo;
return $failed_error;
}
else
{
//You'll usually want to just return true, but for the purposes of this
//Example I'm returning the message contents
// return $message;
return print_r($alamatmail);
}
endforeach;
}
Upvotes: 2
Views: 1415
Reputation: 663
Since you are sending the same mail to all the recipients there is no need to iterate over the message over and over again. All you need to do is set the sender and then add all your recipients to the addAddress array and then end the loop.
After that you just build you mail body and execute the send function.
You can use this:
<?
function send_message( $from, $to, $subject, $message_content )
{
require_once "function.php";
require_once( 'phpmailer/PHPMailerAutoload.php' );
//Initiate the mailer class
$mail = new PHPMailer();
//Check to see if SMTP creds have been defined
if( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) && defined( 'SMTP_LOCATION' ) && defined( 'SMTP_PORT' ) )
{
$mail->IsSMTP();
$mail->Host = SMTP_LOCATION;
$mail->SMTPAuth = true;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
if( defined( 'DEBUG' ) && DEBUG )
{
$mail->SMTPDebug = 1;
}
}
$mail->SetFrom( '[email protected]', "" );
//Set the sender and receiver email addresses
$alamatmail = get_mail();
foreach ($alamatmail as $datamail) {
$to = $datamail['email'];
//We 'can' send to an array, in which case you'll want to explode at comma or line break
if(is_array($to)) {
foreach( $to as $i ) {
$mail->AddAddress($i, "" );
}
}
else {
$mail->AddAddress($to, "" );
}
}
//Set the message subject
$mail->Subject = $subject;
//Add the message header
$message = file_get_contents( 'email-templates/email-header.php' );
//Add the message body
$message .= file_get_contents( 'email-templates/email-body.php' );
//Add the message footer content
$message .= file_get_contents( 'email-templates/email-footer.php' );
//Replace the codetags with the message contents
$replacements = array(
'({message_subject})' => $subject,
'({message_body})' => nl2br( stripslashes( $message_content ) ),
);
$message = preg_replace( array_keys( $replacements ), array_values( $replacements ), $message );
//Make the generic plaintext separately due to lots of css and tables
$plaintext = $message_content;
$plaintext = strip_tags( stripslashes( $plaintext ), '<p><br><h2><h3><h1><h4>' );
$plaintext = str_replace( array( '<p>', '<br />', '<br>', '<h1>', '<h2>', '<h3>', '<h4>' ), PHP_EOL, $plaintext );
$plaintext = str_replace( array( '</p>', '</h1>', '</h2>', '</h3>', '</h4>' ), '', $plaintext );
$plaintext = html_entity_decode( stripslashes( $plaintext ) );
//Send the message as HTML
$mail->MsgHTML(stripslashes($message));
//Set the plain text version just in case
$mail->AltBody = $plaintext;
$failed_error="email gagal dikirim";
//Display success or error messages
if(!$mail->Send()) {
return 'Message send failure: ' . $mail->ErrorInfo . $failed_error;
} else {
//You'll usually want to just return true, but for the purposes of this
//Example I'm returning the message contents
// return $message;
return print_r($alamatmail);
}
}
Since the first one includes all recipients in the to field in each email you can use this (I have ignored the error section so you can add it manually):
<?
function send_message( $from, $to, $subject, $message_content )
{
require_once "function.php";
require_once( 'phpmailer/PHPMailerAutoload.php' );
//Initiate the mailer class
$mail = new PHPMailer();
//Check to see if SMTP creds have been defined
if( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) && defined( 'SMTP_LOCATION' ) && defined( 'SMTP_PORT' ) )
{
$mail->IsSMTP();
$mail->Host = SMTP_LOCATION;
$mail->SMTPAuth = true;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
if( defined( 'DEBUG' ) && DEBUG )
{
$mail->SMTPDebug = 1;
}
}
$mail->SetFrom( '[email protected]', "" );
//Set the sender and receiver email addresses
$alamatmail = get_mail();
//Set the message subject
$mail->Subject = $subject;
//Add the message header
$message = file_get_contents( 'email-templates/email-header.php' );
//Add the message body
$message .= file_get_contents( 'email-templates/email-body.php' );
//Add the message footer content
$message .= file_get_contents( 'email-templates/email-footer.php' );
//Replace the codetags with the message contents
$replacements = array(
'({message_subject})' => $subject,
'({message_body})' => nl2br( stripslashes( $message_content ) ),
);
$message = preg_replace( array_keys( $replacements ), array_values( $replacements ), $message );
//Make the generic plaintext separately due to lots of css and tables
$plaintext = $message_content;
$plaintext = strip_tags( stripslashes( $plaintext ), '<p><br><h2><h3><h1><h4>' );
$plaintext = str_replace( array( '<p>', '<br />', '<br>', '<h1>', '<h2>', '<h3>', '<h4>' ), PHP_EOL, $plaintext );
$plaintext = str_replace( array( '</p>', '</h1>', '</h2>', '</h3>', '</h4>' ), '', $plaintext );
$plaintext = html_entity_decode( stripslashes( $plaintext ) );
//Send the message as HTML
$mail->MsgHTML(stripslashes($message));
//Set the plain text version just in case
$mail->AltBody = $plaintext;
$failed_error="email gagal dikirim";
foreach ($alamatmail as $datamail) {
$to = $datamail['email'];
//We 'can' send to an array, in which case you'll want to explode at comma or line break
if(is_array($to)) {
foreach( $to as $i ) {
$mail2 = clone $mail;
$mail2->AddAddress($i, "");
$mail2->send();
}
}
else {
$mail2 = clone $mail;
$mail2->AddAddress($to, "");
$mail2->send();
}
}
}
Upvotes: 0
Reputation: 1216
in the success case, your function is ended after the first iteration by
return print_r($alamatmail);
This line exits the function. The next iteration will not be called.
Move this line after
endforeach;
and your code should work (only it will not print the whole mails. You need to handle that separately)
Upvotes: 3