This question already has answers here:
i want to use bcc or cc function in this mail function?
(3 answers)
Closed 6 years ago.
How can i add cc and bcc in the following script
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $property_id \n Priority: $priority \n Type: $type \n Message: $message , $proptitle";
$recipient = "myemailid#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='#' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
You need to set headers for setting cc and bcc:
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: My Name <myemail#example.com>'. "\r\n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1#example.com, myboss2#example.com' . "\r\n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3#example.com, myboss4#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
$mailheader .= 'Cc: joe#bloggs.com' . "\r\n";
$mailheader .= 'Bcc: john#doe.com' . "\r\n";
As per the PHP documentation here.
http://uk3.php.net/manual/en/function.mail.php
You will extend your $mailheader.
$mailheader = "From: $email \r\n" .
"Bcc: someone#example.com' . "\r\n";
**First off all you have to concat $recipient with cc and Bcc variable use like this**
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $property_id \n Priority: $priority \n Type: $type \n Message: $message , $proptitle";
$recipient = "myemailid#gmail.com";
$recipient. = "CC: cc#gmail.com\r\n";
$recipient .= "BCC: bcc#gmail.com\r\n";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='#' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Related
I created a web forum with the script below. I am able to receive inquiries from my site; however, the visitor's email address is not showing in the email sent from the webmaster. I would like to have some guidance here to fix the problem.
Here is my PHP script:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "abc#gmail.com";
$subject = "Contact Form";
mail($recipient, $subject, $formcontent) or die("Error!");
echo "Thank You!";
?>
Here is what I got from the webmaster:
From: xx
Message: xx
No email address listed in the email sent by the webmaster.
I found some similar scripts on the web with two additional rows:
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
This actually gives me an Error output.
How can I fix the issue?
Thanks!
The third parameter of mail() is just the actual body of the message, which you have saved as $message. The fourth variable is where you define the headers, and is where you define who the message is From. Note that you don't actually need to pass the message as a header, and as such, your variable $formcontent, shouldn't contain your From: header. However, you do need to provide carriage returns in the form of "\r\n" after the provided email address.
The modified code would look like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name" . "\r\n";
$recipient = "abc#gmail.com";
$subject = "Contact Form";
mail($recipient, $subject, $message, $formcontent) or die("Error!");
echo "Thank You!";
?>
Hope this helps! :)
You could add headers to the mail function.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "abc#gmail.com";
$subject = "Contact Form";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: <$email>" . "\r\n";
mail($recipient, $subject, $formcontent, $headers);
FYI - I would santize and validate all POST values before using them in this manner
So I am mailing the contents of a form to a client and he would like the person who sent the form to be Cc'd in.
I have done some research and it appears I need to use the header code to set the from, subject and cc but my code is set up differently - please see below:
<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$recipient = "email#mydomain.com";
$subject = "More information regarding $relatedproduct";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
Would it be possible to do it like this?
<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
// Mail it
mail($to, $subject, $formcontent, $headers);
?>
php.net/mail
Yes, as you have it:
...
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthday#example.com' . "\r\n";
mail($to, $subject, $content, $headers);
?>
Incidentally, please sanitise your POST variables before you inject them (e.g. $to = $_POST['email'];).
As #Rhopercy says in their comment, perhaps an email library will help you as it takes care of most things for you. Take a look at PHPMailer or SwiftMail.
Should be like this:
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email\r\nReply-To: $email";
$headers .= 'Cc: test#test.com\r\n';
$headers .= 'Bcc: test#test.com\r\n';
$headers .= "Return-Path: <info#premierinspectiontn.com>\r\n";
$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
// Mail it
mail($to, $subject, $formcontent, $headers);
I have a contact form and I am using the below PHP Script to get emails from my contact form. I want to send a copy of the same email to the sender email also. Can anyone help me?. Thank you!
<?php
$errors = '';
$myemail = 'chocolatehills_adventurepark#yahoo.com, chocolatehills88#yahoo.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['subject']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Message from: $name";
$email_body = "New message received. ".
" Here are the details:\n \n NAME: $name \n
SUBJECT: $subject \n
EMAIL ADD: $email_address \n
MESSAGE: $message";
$headers = "From: $email_address\n";
//$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-us.php');
}
?>
You can use cc n header
$headers .= 'Cc: somebody#domain.com' . "\r\n";
More idea
http://php.net/manual/en/function.mail.php
Examples of using CC and BCC.
Example #4 Sending HTML email
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
You can also try to add another mail function like this
mail($email_address,$email_subject,$email_body,$headers);
I set up a simple php contact form. It is working so far, I get the emails, that is not the issue. The only thing that is not working, is the "From" and "Reply-To" field. The email I receive is from www-data#hostname.com and it also replies to that address. I don't know what I might have overlooked :(
<?php
$vorname = $_POST['vorname'];
$nachname = $_POST['nachname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$club = $_POST['club'];
$handicap = $_POST['handicap'];
$spieler = $_POST['spieler'];
$bemerkungen = $_POST['bemerkungen'];
$from = 'Von: Kontaktformular';
$to = 'edited for this question';
$subject = 'Anmeldung';
$body = "Von: $vorname $nachname\n E-Mail: $email\n Telefon: $phone\n Club: $club\n Handicap: $handicap\n Spieler: $spieler\n Bemerkung: $bemerkungen ";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(!isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
header("Location: edited.html");
} else {
header("Location: edited.html");
}
}
?>
Thanks for any hint!
Looks like $headers is not being used in your mail function.
Ok so I am writing a php contact form that will send two emails. One to the webmaster with the info and a similar confirmation email sent to the form submitter.
The problem is with the confirmation email. I am trying to configure the From: header and the Reply-to: but and for some reason catching a hurdle.
Originally I had my php set up as so declaring each header parameter...
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$guests = $_POST['guests'];
$type = $_POST['type'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$message = $_POST['message'];
$formcontent="THIS IS A FORM SUBMISSION FROM domain.COM... \n \n PARTY INQUIRY \n \n From: $name \n Email: $email \n Phone: $phone \n # of Guests: $guests \n Type: $type \n Date Requested: $month $day, $year \n \n Additional Info: $message";
$comfirmcontent="THIS IS A CONFIRMATION OF YOUR FORM SUBMISSION TO domain.COM... \n \n PARTY INQUIRY \n \n From: $name \n Email: $email \n Phone: $phone \n # of Guests: $guests \n Type: $type \n Date Requested: $month $day, $year \n \n Additional Info: $message \n\n\n If you have any further questions please email info#mydomain.com";
$confirmsubject="Confirmation for your inquiry to mydomain.COM";
$confirmheader="From: mydomain.com" . "\r\n" .
"Reply-To: info#mydomain.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$recipient = "info#domain.com";
$subject = "Party Inquiry from Website";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, $confirmsubject, $comfirmcontent, $confirmheader) or die("Error!");
header('Location: party-form-thank-you.html')
This as I understand is the proper way, but I am still having the From: say "mydomain.com#myhostdomain.com as both the senders name and senders email. Which is how it was before I even declared the headers and it was null.
So then I tried an override using
mail($email, $confirmsubject, $comfirmcontent, $confirmheader,'-finfo#mydomain.com') or die("Error!");
Which returned the same results.
So I have resorted to simply using the following, w/o declared headers.
mail($email, $confirmsubject, $comfirmcontent, null,'-finfo#mydomain.com') or die("Error!");
This give me the proper from/reply-to address, but also puts it as the senders name.
So my question is if there is any way to write the headers so the email formulates as such:
Senders Name: myDomain.com
Senders Email: info#mydomain.com
I use an array for my headers, then implode them on "\r\n":
$headers = array();
$headers[] = 'Content-type: text/html; charset="UTF-8";';
$headers[] = 'Date: ' . date('r', $_SERVER['REQUEST_TIME']);
$headers[] = 'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . (empty($_SERVER['SERVER_NAME']) ? '' : '#' . $_SERVER['SERVER_NAME']) . '>';
// here is the from and reply-to part:
$headers[] = 'From: "' . $fromName . '" <' . $fromAddress . '>';
$headers[] = 'Reply-To: "' . $replyToName . '" <' . $replyToAddress . '>';
$headers[] = 'X-Mailer: PHP v' . phpversion();
if (!empty($_SERVER['SERVER_ADDR'])) {
$headers[] = 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'];
}
// Use the implode function to collapse the array into a single string
mail($to, $subject, $message, implode("\r\n", $headers));