发送电子邮件到每个选中的行 - php

时间:2014-01-10 13:36:37

标签: php

我想知道如何创建一个循环,让它从php表中向每个选中的行发送电子邮件。

这是复选框的表格行:

echo "<input name=\"approve[]\" type=\"checkbox\" value='".$row["id"]."' >";

批准php:

    $pfw_header .= "MIME-Version: 1.0\r\n";
    $pfw_header .= "Content-type: text/html; charset=utf-8\r\n";

    $fromName = "HP EG NPI - Registracija";
    $fromEmail = "hp_eg_npi@eventregistration.rs";
    $toEmail = "$email";
    $subject = "Prijava prihvacena";

    function sendEmail ($fromName, $fromEmail, $toEmail, $subject, $emailBody, $pfw_header) {
    $mail = new PHPMailer();
    $mail->FromName = $fromName;
    $mail->From = $fromEmail;
    $mail->AddAddress("$toEmail");

    $mail->Subject = $subject;
    $mail->Body = $emailBody;
    $mail->isHTML(true);
    $mail->WordWrap = 150;

    if(!$mail->Send()) {
        return false;
    } else {
        return true;
    }
}

function readTemplateFile($FileName) { 
        $fp = fopen($FileName,"r") or exit("Unable to open File ".$FileName);
        $str = "";
        while(!feof($fp)) {
            $str .= fread($fp,1024);
        }   
        return $str;
}

    //Data to be sent (Ideally fetched from Database)
    $name = "$first_name";
    $lastname = "$last_name";
    $UserEmail = "$email";


    //Send email to user containing username and password
    //Read Template File 
    $emailBody = readTemplateFile("../html-email/mail2.html");

    //Replace all the variables in template file
    $emailBody = str_replace("#username#",$fnames,$emailBody);
    $emailBody = str_replace("#password#",$lnames,$emailBody);

    //Send email
    $emailStatus = sendEmail ($fromName, $fromEmail, $UserEmail, $subject, $emailBody, $headers);
        }
    }
}

我尝试过在互联网上搜索,但无法找到解决方案。

提前致谢!

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$mail = new PHPMailer();
$mail->FromName = $fromName;
$mail->From = $fromEmail;

foreach($emails as $email) {

    $mail->AddAddress("$email");
}

$mail->Subject = $subject;
$mail->Body = $emailBody;
$mail->isHTML(true);
$mail->WordWrap = 150;

if(!$mail->Send()) {
    return false;
} else {
    return true;
}

您不需要sendEmail功能。