在php中向两个不同的用户发送两个不同的邮件

时间:2016-08-09 07:44:37

标签: php email

成功执行命令后,我想通过PHP中的if else函数向两个不同的人发送两封不同的邮件。我可以使用以下代码发送邮件。如何使用不同的标题发送另一封邮件&内容。

// if new reservation has been successfully added to reservation table 
// send notification to admin via email
if($result){
    $to = $email;
    $subject = $reservation_subject;
    $message .= $reservation_message."\r\n\n";
    $message .= "Customer name:" .$provinsi."\r\n";
    $message .= "Special Request :" .$comment."\r\n";
    $from = $admin_email;
    $headers = "From:" . $from."\r\n".
    mail($to,$subject,$message,$headers);
    echo "OK";
}else{
    echo "Failed";
}

3 个答案:

答案 0 :(得分:2)

这就是您需要做的全部,只需设置新的接收方和发件人,然后再次致电mail()

Mail与任何PHP函数非常相似,您可以设置其参数并调用它。

if($result){
    $to = $email;
    $subject = $reservation_subject;
    $message .= $reservation_message."\r\n\n";
    $message .= "Customer name:" .$provinsi."\r\n";
    $message .= "Special Request :" .$comment."\r\n";
    $from = $admin_email;
    $headers = "From:" . $from."\r\n".
    if ( mail($to,$subject,$message,$headers) ) {
        echo "OK message 1 sent";
    } else {
        echo "FAILED message 1 sent";
    }

    $to = $email_2;
    $subject = $reservation_subject_2;
    $message = $reservation_message_2."\r\n\n";

    $from = $admin_email_2;
    $headers = "From:" . $from."\r\n".

    if ( mail($to,$subject,$message,$headers) ) {
        echo "OK message 2 sent";
    } else {
        echo "FAILED message 2 sent";
    }


}else{
    echo "Failed";
}

答案 1 :(得分:1)

if($result){
    createEmailOne();
    createEmailTwo(); 
}else{
    echo "Failed";
}

function createEmailOne(){
    $to = $email;
    $subject = $reservation_subject;
    $message = $reservation_message."\r\n\n";
    $message .= "Customer name:" .$provinsi."\r\n";
    $message .= "Special Request :" .$comment."\r\n";
    $from = $admin_email;
    $headers = "From:" . $from."\r\n".
    mail($to,$subject,$message,$headers);
    echo "OK";
}

function createEmailTwo(){
    $to = "john@snow.com";
    $subject = $reservation_subject;
    $message = "Something went wrong in this form, here is the info: \r\n\n";
    $message .= $reservation_message."\r\n\n";
    $message .= "Customer name:" .$provinsi."\r\n";
    $message .= "Special Request :" .$comment."\r\n";
    $from = $admin_email;
    $headers = "From:" . $from."\r\n".
    mail($to,$subject,$message,$headers);
    echo "OK";
}

答案 2 :(得分:1)

尝试此代码:

function send_mail($to_email,$from_email,$subject,$message){
    $nameToBeDisplayed = "XYZ";
    $headers  = 'From: ' . $nameToBeDisplayed . '<' . $from_email . '>' . "\r\n";
    $headers .= 'Reply-To: ' . $from_email . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

    $message = "";      

    mail($to_email, $subject, $message, $headers);
}

if($result){
     send_mail('to1@gmail.com','from1@gmail.com','your subject','your message');

     send_mail('to2@gmail.com','from2@gmail.com','your subject','your message');
}else{
    echo "Failed";

}