Lulu G
Lulu G

Reputation: 23

Send two different email messages php

I'm looking for a way to send 2 different email messages with different recipients.

I know that I can send the same message to a list of emails, but what I need is to send one text to certain recipients and other text to other list of emails.

I need this because my message contains approval information (which should only be seen by an admin) and I need to sent at the same time other mail just for telling the user "your request have been sent and will be reviewed".

Is there a function in mail() that can do this?

Upvotes: 2

Views: 13063

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

-As requested-

Unfortunately, PHP's mail() function can only handle this by using seperate mail() functions.

You can send the same email to multiple recipients at the same time, but to send two different messages (and two different subjects) intended for two different recipients requires using two different mail() functions, along with two different sets of recipients/subjects/messages/headers.

For example:

/* send to 1st recipient */
$to_1 = "[email protected]";
$from = "[email protected]";
$subject_1 = "Subject for recipient 1";
$message_1 = "Message to recipient 1";

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


mail($to_1, $subject_1, $message_1, $headers_1);

/* send to 2nd recipient */
$to_2 = "[email protected]";
$from = "[email protected]";
$subject_2 = "Subject for recipient 2";
$message_2 = "Message to recipient 2";

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

mail($to_2, $subject_2, $message_2, $headers_2);

Upvotes: 7

Derple
Derple

Reputation: 863

Just like this.

<?php
//first mail ////
$to      = '[email protected]';
$subject = 'the subject';
$message = '1st Message';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);



//second mail ////
$to      = '[email protected]';
$subject = 'the subject';
$message = '2nd Message';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

?>

Upvotes: 0

Related Questions