Send two different email messages php - 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?

-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 = "recipient_1#example.com";
$from = "from_recip#example.com";
$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 = "recipient_2#example.com";
$from = "from_recip#example.com";
$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);

Just like this.
<?php
//first mail ////
$to = '1stRe#example.com';
$subject = 'the subject';
$message = '1st Message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
//second mail ////
$to = '2ndRe#example.com';
$subject = 'the subject';
$message = '2nd Message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Related

send html format through php mail() [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 4 years ago.
This is in code-igniter. To send mail I have written a function using php mail(). The code is:
function mailsend()
{
$to="mymail#gmail.com";
$subject="test";
$message="<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>";
echo $message;
mail($to,$subject,$message);
}
When I send mail from server side, I get the html code in my mail, not the real table. But the $messageshows the real table in browser. Is there any way in mail() to get the real table in the mail?
You can send the html content in email via setting the header portion. Below is the sample code you can use.
<?php
$to = 'maryjane#email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker#email.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
In order to send html content in your email with mail function you will need to add header options to it, like so:
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: test#yourdomain.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($sendTo, $subject, $message, $headers);
Add an header to your mail function like this
$headers = 'From: test#yourdomain.com' . "\r\n" .
'Reply-To: hello#example.com' . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n" .
'X-Mailer: PHP/' . phpversion();

What is wrong with my PHP Send Mail Function below?

What is wrong with my PHP Send Mail function? I am hosting this script on a live server.
$to = 'myemail#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: exampleemail#gmail.com' . "\r\n" .
'Reply-To: exampleemail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){}
else{
echo "Email sending failed";
}
I keep receiving the "Email sending Failed" message.

Drupal sending mail issue

My problem begins when I had to add a single landing page to an existing Drupal website. I've never worked with this CMS, so I just created a folder for this page in the root folder and put all the content there.
Then it appeared that I need to send mail from this page after submitting a form there. As I understood I can't use drupal_mail() function there, so I tried something like that:
$to = $email;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
But that gave me no result.
Then I installed SMTP auth module for Drupal and tried to send mail, but again I had no result. However, test messages from SMTP module have been sent correctly.
<?php
$to = $email;
$subject = 'the subject';
$message = 'hello';
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\n";
$headers .= "From: $email" . "\n";
$headers .= "Reply-To:: $email" . "\n";
mail($to, $subject, $message, $headers);
?>
Some hosts require you to use an additional parameter, -f, to send mail.
Try:
mail($to, $subject, $message, $headers, "-f".$email);

PHP - Not Sending Emails with Header Information

I am having a problem sending emails when I add header information.
However when I just remove the header parameter it works. What is wrong? Is it the code? Or some setting I need to change on the web server admin panel to say "Allow-Headers" or something?
I am trying to send to hotmail in case this has any relavance in determining the problem.
Any help would be greatly appreciated. Thanks.
Below Doesn't Send Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message, $headers);
?>
Below Sends Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message);
?>
I use these headers in my php mailing function and it works well. Note: I also use a third party mail-routing service to avoid having my mails marked as coming from a spammy IP. You might want to look into that also.
$headers = 'From: '.$from.'#foo.net' . "\r\n" .
'Reply-To: '.$from.'#foo.net' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
I also use the optional fifth parameter to mail() to set the envelope address, e.g.:
$parameters = '-f '.$from.'#foo.net';
so the final call is:
mail($to, $subject, $message, $headers, $parameters);
You can just delete the "FROM:" from the headers list .. it prevents it in some hosts .But the real question then will be how ca I change the sent from email address to a specific email that I want

Sending Email in PHP from a button

Let me start by saying I do not have a lot of programming knowledge. I use a program called PHPRunner to generate most of the php that I do. It is mainly for small office stuff nothing fancy.
My question is:
I have a table that has 6 fields one of them called email. When I go into a record I wanted to put a button called "send notification" that would email the record details to the email address associated with that record.
Any help would be greatly appreciated. I know it is probably out here on the web, possibly searching the wrong way.
You can use the mail function in php. Example from the provided link:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
For sending email using PHP, use mail() function
http://php.net/manual/en/function.mail.php
Example:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$from = 'webmaster#example.com';
$headers = 'From: '.$from . "\r\n" .
'Reply-To: '.$from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Categories