В настоящее время я работаю над базовой системой php mysql, которая отправит электронное письмо нескольким получателям из базы данных. Im уже выполняет поиск в форуме, и много ответов использует loop to email recipient. И затем я пытаюсь использовать цикл в получателе электронной почты, но просто могу отправить 1 письмо (хотя в базе данных есть 5 или более получателей электронной почты). Можете ли вы сказать мне, где мой код неправильный? Ниже мой код:Отправить письмо с базы данных с помощью phpmailer
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;
}
Что вы получите, если вы делаете 'var_dump ($ alamatmail);'? –
они возвращают значение массива Array ([0] => Array ([email] => [email protected]) [1] => Array ([email] => [email protected]) [ 2] => Array ([email] => [email protected])) , и результат верный, –
yup, я пытаюсь это сделать. –