使用PHPmailer的电子邮件中的动态内容

时间:2017-08-06 12:01:10

标签: php phpmailer

我有来自php邮件示例的这个邮件列表:https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps

所以我把foreach循环放在while循环中,所以它遍历整个表,我使用一个令牌数组来替换名称和任何动态内容,内容正在运行,但它一直在输入错误的名称电子邮件。

名称和电子邮件正确插入vac_mailing_system表。但是当它被选中时,似乎抓住一个随机名称并将其分配给随机的电子邮件地址。所以这让我觉得select语句存在问题。

 $finish_table = $dbh->prepare("SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts < 5");  

if($finish_table->execute()) {
   $finish_table->setFetchMode(PDO::FETCH_ASSOC);
}

while($row = $finish_table->fetch()) {  

    $vac_id = $row['vac_id'];   
    $vac_name = $row['vac_name'];   
    $vac_comp = $row['vac_comp'];   
    $full_name = $row['full_name']; 


    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = '';
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Port = ;
    $mail->Username = '';
    $mail->Password = '';
    $mail->setFrom('', 'Information');
    $mail->addReplyTo('', ' Information');
    $mail->Subject = "Information";

    $mail->Body = strtr(file_get_contents('new_vac_email.html'), array('%full_name%' => $full_name, '%vac_id%' => $vac_id, '%vac_name%' => $vac_name, '%vac_comp%' => $vac_comp));

    //Same body for all messages, so set this before the sending loop
    //If you generate a different body for each recipient (e.g. you're using a templating system),
    //set it inside the loop
    //msgHTML also sets AltBody, but if you want a custom one, set it afterwards
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    //Connect to the database and select the recipients from your mailing list that have not yet been sent to
    //You'll need to alter this to match your database
    $mysql = mysqli_connect('host', 'user', 'pass');
    mysqli_select_db($mysql, 'db_name');


    $result = mysqli_query($mysql, "SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts < 5 LIMIT 0, 50");
    foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+


        $mail->addAddress($row['email_address'], $full_name);


    if (!$mail->send()) {
        echo "Mailer Error (" . str_replace("@", "&#64;", $row["email_address"]) . ') ' . $mail->ErrorInfo . '<br />';
          //Mark it as sent in the DB
        mysqli_query(
            $mysql,
            "UPDATE vac_mailing_system SET sent_status = false, retry_attempts = +1 WHERE email_address = '" .
            mysqli_real_escape_string($mysql, $row['email_address']) . "'"
        );

        break; //Abandon sending
    } else {
        echo "Message sent to :" . $full_name . ' (' . str_replace("@", "&#64;", $row['email_address']) . ')<br />';
        //Mark it as sent in the DB
        mysqli_query(
            $mysql,
            "UPDATE vac_mailing_system SET sent_status = true WHERE email_address = '" .
            mysqli_real_escape_string($mysql, $full_name) . "'"
        );
    }
    // Clear all addresses and attachments for next loop
        $mail->clearAddresses();
        $mail->clearAttachments();
    }

    sleep(60);
    echo "emails sent";

    }

1 个答案:

答案 0 :(得分:1)

试试这段代码:

是的,非常容易使用include和一个简短的帮助函数:

function get_include_contents($filename, $variablesToMakeLocal) {
    extract($variablesToMakeLocal);
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('new_vac_email.php', $data); // HTML -> PHP!
$mail->Send(); // send message