PHPMailer Foreach循环 [英] PHPMailer Foreach Loop

查看:51
本文介绍了PHPMailer Foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用PHPMailer,它从一个已上传的.txt文件中获取电子邮件地址列表.当前系统运行良好,但是我尝试添加一个跟踪系统以查看是否查看了电子邮件.

I currently am using PHPMailer which gets the list of email addresses from an uploaded .txt file. The current system works fine, but I am trying to add a tracking system to see if the email is viewed.

我们通过在每封电子邮件中添加一个图像来实现这一目的,当通过php更新时,我们会知道该电子邮件已被查看.

We are doing this by adding an image to each email which when viewed updates via php to let us know the email has been viewed.

当前,我们的PHPMail看起来像这样,

Currently our PHPMail looks something like this,

foreach ($email_addresses as $line_num => $line) {
            $ismatch = preg_match('/^[\s,]+$/',$line);
            $isvalid = preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', trim($line));

            // found a blank line, ignore
            if($ismatch)
                echo "";

            // found an invaid email address, add to string
            else if($isvalid==0)
                $strInvalidEmailAddresses .= $line. '<br />';

            // no issues, add to BCC
            else{
                $mail->addBCC($line,$line); 
            }
}

当前系统将每封电子邮件添加到密件抄送.我希望每封电子邮件都发送到每个单独的地址,而无需密件抄送.

The current system adds each email to BCC. I would like each email to be sent to each individual address without BCCing them.

在尝试将此图片显示到HTML电子邮件末尾时,我遇到了问题.

I ran into issues when I tried this image to the end of the HTML email.

$track_image = '<img src="http://domain.com/email_test/email_beacon.php?campaign='.$intCampaignID.'&email='.$line.'" alt="" />';

$mail->MsgHTML($html_page.$track_image);

每当我尝试包含跟踪图像时,$ line(或电子邮件)总是相同的,但是我需要根据.txt文件中的电子邮件进行更改.

Whenever I try to include the tracking image, the $line (or email) is always the same, but I need it to change depending on which emails are in the .txt file.

推荐答案

您可以使用以下方法代替使用BCC:

Instead of using BCC, you'd do something like:

... initialize PHP mailer ...
... set options common to ALL emails ...
foreach( ... loop over all recipients ...) {
    $mail->ClearAddresses(); // remove previous email addresses
    $mail->AddAddress($new_recipient_here);
    $mail->Body = <<<EOL
... customized html here
<img src="http://domain.com/email_test/email_beacon.php?campaign={$intCampaignID}&email={$address}" alt="" />
... more html here
EOL;

    $mail->send();
}

这篇关于PHPMailer Foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆