如何使用PHP发送给多个电子邮件收件人

时间:2014-08-18 02:55:22

标签: php email

我在使用一堆电子邮件收件人收到通知时遇到问题,收到的唯一收到通知的电子邮件就是最后一封电子邮件(email5@mysite.com),我一直在想为什么以及出现了什么问题?任何帮助都会被证实。

<?php

class QIF_Email {

function send_email(){
    $to= "email1@mysite.com,email2@mysite.com,email3@mysite.com,email4@mysite.com,email5@mysite.com,";
    $subject="Inquiry";

    $header="mysite.com - Inquiry";

    $message="Date: ".$_POST['date']." \r\n";
    $message.="Name: ".$_POST['name']." \r\n";
    $message.="Company Name: ".$_POST['company_name']." \r\n";
    $message.="Contact: ".$_POST['contact']." \r\n";
    $message.="Address: ".$_POST['address']." \r\n";
    $message.="City: ".$_POST['city']." \r\n";
    $message.="State: ".$_POST['state']." \r\n";
    $message.="Zip Code: ".$_POST['zip_code']." \r\n";
    $message.="Telephone / Fax: ".$_POST['telephone']." \r\n";
    $message.="Email Address: ".$_POST['email_address']." \r\n";
    $message.="Comments: ".$_POST['comments']." \r\n";  
    $sentmail = mail($to,$subject,$message,$header);    


}



}

$qif = new QIF_Email;
$qif->send_email();


?>

1 个答案:

答案 0 :(得分:0)

将收件人存储为阵列并将电子邮件发送给每个

class QIF_Email {

function send_email($to){
    $subject="Inquiry";

    $header="mysite.com - Inquiry";

    $message="Date: ".$_POST['date']." \r\n";
    $message.="Name: ".$_POST['name']." \r\n";
    $message.="Company Name: ".$_POST['company_name']." \r\n";
    $message.="Contact: ".$_POST['contact']." \r\n";
    $message.="Address: ".$_POST['address']." \r\n";
    $message.="City: ".$_POST['city']." \r\n";
    $message.="State: ".$_POST['state']." \r\n";
    $message.="Zip Code: ".$_POST['zip_code']." \r\n";
    $message.="Telephone / Fax: ".$_POST['telephone']." \r\n";
    $message.="Email Address: ".$_POST['email_address']." \r\n";
    $message.="Comments: ".$_POST['comments']." \r\n";  
    $sentmail = mail($to,$subject,$message,$header);    


}



}

$to[] = "email1@mysite.com";
$to[] = "email2@mysite.com";

for($i = 0; $i < count($to); $i++){
    $qif = new QIF_Email;
    $qif->send_email($to[$i]);
}

不是最佳实践,但它应该完成这项工作