Tony Stark
Tony Stark

Reputation: 93

Sending two different mails to two different users in php

After successful execution of command I want to send two different mails to two different person through if else function in PHP. I am able to send a mail using below code. How to send another mail with different headers & contents.

// 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";
}

Upvotes: 0

Views: 141

Answers (3)

Randy
Randy

Reputation: 9849

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 = "[email protected]";
    $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";
}

Upvotes: 1

Aman Maurya
Aman Maurya

Reputation: 1325

Try This code :

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('[email protected]','[email protected]','your subject','your message');

     send_mail('[email protected]','[email protected]','your subject','your message');
}else{
    echo "Failed";

}

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94682

This is all you need to do, just set the new reciever and sender and message and call mail() again

Mail is much like any PHP function, you set up its parameters and call it.

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";
}

Upvotes: 2

Related Questions