1

当我在 phpmailer 的消息正文中放置“&email&”时,输出消息将“&email&”更改为 $to 数组中的电子邮件地址。但它只使用第一封电子邮件,它确实看到了其余的。我如何使它获得其余的电子邮件并将其相应地放置到电子邮件消息中?

$nq=0;            
for($x=0; $x<$numemails; $x++)
{
    $to = $allemails[$x];
    if ($to)
    {
        $to = ereg_replace(" ", "", $to);

        $message = ereg_replace("&email&", $to, $message);
        $subject = ereg_replace("&email&", $to, $subject);
        $qx=$x+1;

        print "Line $qx . Sending mail to $to.......";

        flush();
    }
}

=== 我不能在下面发布图片链接:

http://filevault.org.uk/testee/mailer_image.png

希望你现在明白了。

4

1 回答 1

3

你不应该再使用ereg_*它,因为它已被弃用 -preg_replace它是继任者,尽管看起来你只需要str_replace

$message = str_replace("&email&",$to,$message);

如果由于某种原因你真的必须使用 ereg:

您可能需要全局标志g

ereg_replace("&email&g",

每次更换都不一样

$to = array('email1@me.com','em2@me.com');
$text = 'asdkfjalsdkf &email& and then &email&';
$email_replacements = $to;
function replace_emails()
{
    global $email_replacements;
    return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}
var_dump(preg_replace_callback('#&email&#','replace_emails',$text));
//"asdkfjalsdkf email1@me.com and then em2@me.com" 

融合的:

$to = $allemails[$x];
$email_replacements = $to;
function replace_emails()
{
    global $email_replacements;
    return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}

if($to)
{
    $message = preg_replace_callback('#&email&#','replace_emails',$message);

    $subject = preg_replace_callback('#&email&#','replace_emails',$subject);
    $qx=$x+1;
    print "Line $qx . Sending mail to $to.......";

    flush();
于 2013-08-12T20:40:55.483 回答