PHP Form Review - Cc and Bcc not working - php

I have an html form the links to a PHP email. The form works well, but I am having trouble with the Cc and Bcc not coming through.
Here is the entire code. Please review and help me understand what I am getting wrong on the Cc and Bcc parts in the headers.
Thanks:
<?php
$emailFromName = $_POST['name'];
$emailFrom = $_POST['email'];
$emailFromPhone = $_POST['phone'];
$email9_11 = $_POST['9-10'];
$email10_11 = $_POST['10-11'];
$email11_12 = $_POST['11-12'];
$email12_1 = $_POST['12-1'];
if (empty($emailFromName)) {
echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)#([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'The email address entered is invalid.';
} else {
$emailTo = "main#gmail.com" ;
$subject = "Family History Conference Registration";
if (!empty($emailFrom)) {
$headers = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$headers = 'From: Family History Conference <noreply#domain.org>' . "\r\n";
$headers .= 'Cc: $emailFrom' . "\r\n";
$headers .= 'Bcc: myemail#domain.com' . "\r\n";
}
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n\n";
$body .= "I would like to attend the following classes.\n";
$body .= "9:10 to 10:00: ".$email9_11."\n";
$body .= "10:10 to 11:00: ".$email10_11."\n";
$body .= "11:10 to 12:00: ".$email11_12."\n";
$body .= "12:10 to 1:00: ".$email12_1."\n";
/* Send Email */
if (mail($emailTo, $subject, $body, $headers)) {
echo "<h2>Thank you for Registering</h2>
<h3>You have registered for the following classes</h3>
<p>9:10 to 10:00am: \"$email9_11\" <br />
10:10 to 11:00am: \"$email10_11\"<br />
11:10 to 12:00: \"$email11_12\"<br />
12:10 to 1:00: \"$email12_1\"</p>
<p>We look forward to seeing you October 31, 2010</p>";
} else {
echo 'There was an internal error while sending your email.<br>';
echo 'Please try again later.';
}
}
?>

You're using single quotes
$headers .= 'Cc: $emailFrom' . "\r\n";
PHP won't interpret variables inside single quotes, you must use double quotes
$headers .= "Cc: $emailFrom\r\n";

Related

How to Send emails to multiple recipients in PHP [duplicate]

This question already has answers here:
PHP mail: Multiple recipients?
(5 answers)
Closed 1 year ago.
I need to send my forms to multiple recipients, but I can't figure it out which line I need to edit. Please see below. I appreciate your help.
I already tried adding more values to the emailto, but I can't get it to work.
I need to send my forms to multiple recipients, but I can't figure it out which line I need to edit. Please see below. I appreciate your help.
I already tried adding more values to the emailto, but I can't get it to work.
Hello there,
I need to send my forms to multiple recipients, but I can't figure it out which line I need to edit. Please see below. I appreciate your help.
<?php
// Configure your Subject Prefix and Recipient here
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$subjectPrefix = $_POST['subject'];
$privacyPolicy = $_POST['privacy-policy'];
$emailTo = stripslashes(trim($_POST['email-to']));
$name = stripslashes(trim($_POST['name']));
$email = stripslashes(trim($_POST['email']));
$phone = stripslashes(trim($_POST['phone']));
$message = stripslashes(trim($_POST['message']));
$spam = $_POST['textfield'];
$confirmMsg = $_POST['confirm'];
$captcha = $_POST['captcha'];
if (empty($name)) {
$errors['name'] = 'Please fill in all required fields.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Please fill in all required fields.';
}
if (empty($message)) {
$errors['message'] = 'Please fill in all required fields.';
}
if (empty($captcha)) {
$errors['captcha'] = 'TEST CAPTCHA';
}
if (empty($privacyPolicy)) {
$errors['privacy_policy'] = 'Please fill in all required fields.';
}
// if there are any errors in our errors array, return a success boolean or false
if (!empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
} else {
$subject = "Message from $subjectPrefix";
$body = '
<strong>Name: </strong>'.$name.'<br />
<strong>Email: </strong>'.$email.'<br />
<strong>Phone: </strong>'.$phone.'<br />
<strong>Message: </strong>'.nl2br($message).'<br />
';
$headers = "MIME-Version: 1.1" . PHP_EOL;
$headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: 8bit" . PHP_EOL;
$headers .= "Date: " . date('r', $_SERVER['REQUEST_TIME']) . PHP_EOL;
$headers .= "Message-ID: <" . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '#' . $_SERVER['SERVER_NAME'] . '>' . PHP_EOL;
$headers .= "From: " . "=?UTF-8?B?".base64_encode($name)."?=" . " <$email> " . PHP_EOL;
$headers .= "Return-Path: $emailTo" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
$headers .= "X-Originating-IP: " . $_SERVER['SERVER_ADDR'] . PHP_EOL;
if (empty($spam)) {
mail($emailTo, "=?utf-8?B?" . base64_encode($subject) . "?=", $body, $headers);
}
$data['success'] = true;
$data['confirmation'] = $confirmMsg;
}
// return all our data to an AJAX call
echo json_encode($data);
}
You can separate receivers by comma like
$to = "somebody#example.com, somebodyelse#example.com";
As indicated in the mail() documentation you can use this:
$emailTo = $mail1 . ', ' . $mail2;

execute mail once all item have been iterated

I have the following issues.
I receive separate emails for each iteration of the loop. I want mail to be only sent once with all of the items iterated.
<?php
// Honey pot trap
// Create a hidden input that is only visible to bots. If it's empty than proceed.
if (empty($_POST['humancheck'])){
// Proceeed if submit button have been pressed
$fullName = $_POST['fname'];
$email = $_POST['email'];
$stage = $_POST['stage'];
include("db.php");
$resources = "select * from resources where stage LIKE '%".$stage."%'";
$run_query = mysqli_query($con, $resources);
while($row = mysqli_fetch_array($run_query)) {
$data[] = array(
'format' => $row['format'],
'title' => $row['title'],
'costs' => $row['cost'],
'stage' => $row['stage'],
'topic' => $row['topic'],
'link' => $row['link']
);
}
foreach($data as $item) {
// Sanitize input data
$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
// Mail Set up
$to = $clean_email;
$to_us = "info#email.com";
// Email subject
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br>
<i> Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs: \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us. \r\n";
// Headers
// 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";
$headers .= 'From: <info#email.com>' . "\r\n";
}
mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);
}
?>
what happens is the while loops to the data that is stored in an array. That array is used in foreach. and outside of the loops, the mail is suppose to mail the result.
In theory this should of worked but its not working.
Try this. Edit last part of your code in this way. Take one time parameters of mail content outside the loop, and let loop make only the message content.
$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
$to = $clean_email;
$to_us = "info#email.com";
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
foreach($data as $item) {
$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
}
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br><i> Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs: \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us. \r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <info#email.com>' . "\r\n";
mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);

PHP Mail using HTML with PHP if statement embedded

I currently have PHP sending an HTML email that lists every item from a form even if the quantity is 0. Now I want to change it to only list items that are being ordered (Qty>0). After looking all over the internet to to and find a way to embed a PHP if statement inside an HTML email, this is what I have come up with and it's still not working...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
?>
<?php
$to = "me#company.com";
$subject = "Marketing Material Order from $name";
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email: $email<br>
Date Needed: $date
</p>
<?php if ($AV000100 != 0) {
$AV000100 - Brochures;
}
?>
<?php if ($AV000101 != 0) {
$AV000101 - Folders;
}
?>
<?php if ($AV000102 != 0) {
$AV000102 - Pens;
}
?>
<p>Comments: $comments</p>
</body>
</html>
";
$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n" .
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
?>
How do I change the embedded PHP if statement to show this as the email body...
Name: John Doe
Email: jon#mail.com
Date Needed: Today
2 - Brochures
2 - Pens
Comments: Thank you
Instead of...
Name: John Doe
Email: jon#mail.com
Date Needed: Today
2 - Brochures
0 - Folders
2 - Pens
Comments: Thank you
FYI...the way I have the PHP if statement written above, none of the items show up. Any help would be greatly appreciated!
Not quite sure what you want - but if you want to use the result of evaluating the PHP code as the message body then you need to use output buffering (or a lot of inline ifs - output buffering is cleaner).
For example, if you put all your code the generate the message body in a file body.php then you could use:
ob_start();
include('body.php');
$message = ob_get_Clean();
You don't need to have the body code in a seperate file - just include it instead of the include statrement betwen the ob_start() and ob_get_clean()
Here's what you want to do with what you have.
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email: $email<br>
Date Needed: $date
</p>
" . ($AV000100 != 0 ? $AV000100 . " - Brochures" : "") . "
" . ($AV000101 != 0 ? $AV000101 . " - Folders" : "") . "
" . ($AV000102 != 0 ? $AV000102 . " - Pens" : "") . "
<p>Comments: $comments</p>
</body>
</html>";
You can append the message line by line if you want using the concatenating assignment operator (.=), and put the if statements where you need them. Like so:
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= "<p>";
$message .= "Name: $name<br>";
$message .= "Email: $email<br>";
$message .= "Date Needed: $date";
$message .= "</p>";
if ($AV000100 != 0) {
$message .= "$AV000100 - Brochures";
}
if ($AV000101 != 0) {
$message .= "$AV000101 - Folders";
}
if ($AV000102 != 0) {
$message .= "$AV000102 - Pens";
}
$message .= "<p>Comments: $comments</p>";
$message .= "</body>";
$message .= "</html>";
You can just send $message as usual.
Here is the full code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
$to = "me#company.com";
$subject = "Marketing Material Order from $name";
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= "<p>";
$message .= "Name: $name<br>";
$message .= "Email: $email<br>";
$message .= "Date Needed: $date";
$message .= "</p>";
if ($AV000100 != 0) {
$message .= "$AV000100 - Brochures";
}
if ($AV000101 != 0) {
$message .= "$AV000101 - Folders";
}
if ($AV000102 != 0) {
$message .= "$AV000102 - Pens";
}
$message .= "<p>Comments: $comments</p>";
$message .= "</body>";
$message .= "</html>";
$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n" .
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
?>
Try like this
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email:$email<br>
Date Needed:$date
</p>
...
...";
Each time you break the string and then concatenate the string with the php results using . operator.

PHP processing form : unknown sender

I created a form with a processing PHP file. Everything works fine. I receive the mail but it's from "Unknow sender" in Gmail. Why please ?
I would like to see in my email box the name and the firstname of the person who fills the form. What's wrong in my code ?
<?php
if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
extract($_POST);
if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_telephone) && !empty($form3_message)) {
$to = 'XXXXXX#gmail.com'; // My real email
$subject = 'Contact from the site';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
$headers .= 'Reply-To:'.$form3_email. "\r\n";
$message = '<html><body>';
$message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$message .= '<table>';
$message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
$message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
$message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
$message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
$message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
$message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo "Form sent";
} else {
echo "Form not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
Replace the $form3_name with $form3_email
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
^^^^^^^^^^^^ //<----- Here
That's a name , not an email address , and that's the reason you get that error.
Also, you need to wrap them in tags <>
The right way..
$headers .= 'From:' .$form3_firstname. " ".'<'.$form3_email.'>'."\r\n";
replace "\r\n" with "\n" and your problem will be solved... and also put return-path in your headers...
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
$headers .= 'From: \''.$form3_firstname.'\' <'.$form3_firstname.'>\r\nReturn-Path: <'.$form3_firstname.'>
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\n";
$headers .= 'Reply-To:'.$form3_email. "\n";
please let me know if you want furtther guidance...
Because you do not supply an emailaddress in your "From:" header. There always needs to be an emailaddress.
Try something like:
$headers .= "From: $form3_firstname $form3_name <$form_email>\r\n";
Mind you, you may have to test and/or escape your form variables; for instance, check that there is no newline in there, otherwise your form might be abused for spamming.

sending all data from email with require field

I'm creating a user require form in my site. For this I put some validation on compulsory fields, and when a user fills in the form and presses submit and validation is correct then I receive a email on my email address.
But now I would like all user information in the email, like name, city, budget etc... so what changes do I need to make in my email.php script?
If some fields are not compulsory and the user doesn't fill them in, can they affect my script?
My script is:
<?php
$to = "test#networkers.in";
$subject = "a new requiremnet come!";
$message = "Hi,\n\nyou get a new require";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Sender-IP: " . $_SERVER["SERVER_ADDR"] . "\r\n";
$headers .= "From: " . stripslashes($name) . " <" . $email . ">" . "\r\n";
$headers .= "Priority: normal" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
echo "Your mail was sent successfully";
} else {
echo "We encountered an error sending your mail";
}
?>
and the data I recieve is:
$name = $_POST['fname'].' '.$_POST['lname'];
$email = $_POST['mail'];
$phone = $_POST['ph'];
$country = $_POST['country'];
$pt = $_POST['pt'];
$cwl = $_POST['cwl'];
$dyhyows = $_POST['dyhyows'];
$pb = $_POST['pb'];
$bpd = $_POST['bpd'];
$hdyhau = $_POST['hdyhau'];
You can add fields to the message body by concatenating them like so:
$message = "Hi,\n\nyou get a new require";
$message .= "\n Name: " . $name;
$message .= "\n Email: " . $email;
$message .= "\n Phone: " . $phone;
$message .= "\n Country: " . $country;
$message .= "\n pt: " . $pt;
$message .= "\n cwl: " . $cwl;
$message .= "\n dyhyows: " . $dyhyows;
$message .= "\n pb: " . $pb;
$message .= "\n bpd: " . $bpd;
$message .= "\n hdyhau: " . $hdyhau
Any fields that weren’t filled in by the user will simply be blank.

Categories