I'm very new to PHP and am using a basic template 'send-mail' form on a contact page.
It's been requested that I send the email out to multiple email addresses when the "Submit" button is clicked. I've searched around & haven't quite found what I needed. What code do I need to add into the form below in order to send this out to multiple email addresses?
<?php
$mail_to = 'daniel30293#gmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$web = $_POST['sender_web'];
$company = $_POST['sender_company'];
$addy = $_POST['sender_addy'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Web Prayer Request from ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Prayer Request: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');
window.location = 'prayer-request.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator admin#bondofperfection.com');
window.location = 'prayer-request.php';
</script>
<?php
}
?>
Your help is greatly appreciated.
You implode an array of recipients:
$recipients = array('jack#gmail.com', 'jill#gmail.com');
mail(implode(',', $recipients), $submit, $message, $headers);
See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php
Receiver, or receivers of the mail.
The formatting of this string must comply with ยป RFC 2822. Some examples are:
user#example.com
user#example.com, anotheruser#example.com
User <user#example.com>
User <user#example.com>, Another User <anotheruser#example.com>
Just add multiple recipients comma seperated in your $mail_to variable like so:
$mail_to = 'nobody#example.com,anotheruser#example.com,yetanotheruser#example.com';
See
mail() function in PHP
Here is a simple example:
<?php
// Has the form been submitted?
// formSubmit: <input type="submit" name="formSubmit">
if (isset($_POST['formSubmit'])) {
// Set some variables
$required_fields = array('name', 'email');
$errors = array();
$success_message = "Congrats! Your message has been sent successfully!";
$sendmail_error_message = "Oops! Something has gone wrong, please try later.";
// Cool the form has been submitted! Let's loop through the required fields and check
// if they meet our condition(s)
foreach ($required_fields as $fieldName) {
// If the current field in the loop is NOT part of the form submission -OR-
// if the current field in the loop is empty, then...
if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {
// add a reference to the errors array, indicating that these conditions have failed
$errors[$fieldName] = "The {$fieldName} is required!";
}
}
// Proceed if there aren't any errors
if (empty($errors)) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
$email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );
// Email Sender Settings
$to_emails = "anonymous1#example.com, anonymous2#example.com";
$subject = 'Web Prayer Request from ' . $name;
$message = "From: {$name}";
$message .= "Email: {$email}";
$headers = "From: {$name}\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if (mail($to_emails, $subject, $message, $headers)) {
echo $success_message;
} else {
echo $sendmail_error_message;
}
} else {
foreach($errors as $invalid_field_msg) {
echo "<p>{$invalid_field_msg}</p>";
}
}
}
Related
my form is working as intended but for some reason the email will only send to one of my email accounts and not the other I am putting the right email in the email field so that isn't the issue however I can't seem to see where I am going wrong I assume it's because I'm using $email to grab the email address to where the 2nd email is suppose to go...here is my php where am I going wrong?
<?php
$from = 'Pixel Wars - Press Inquiry';
$to = "my-email#gmail.com, $email";
$subject = 'Press Inquiry from Pixelwars.com';
function errorHandler ($message) {
die(json_encode(array(
'type' => 'error',
'response' => $message
)));
}
function successHandler ($message) {
die(json_encode(array(
'type' => 'success',
'response' => $message
)));
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "Name: $name\r\n Email: $email\r\n\r\n Message:\r\n $message";
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message)) {
errorHandler('Header injection detected.');
}
// Check if name has been entered
if (!$_POST['name']) {
errorHandler('Please enter your name.');
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
errorHandler('Please enter a valid email address.');
}
// Check if message has been entered
if (!$_POST['message']) {
errorHandler('Please enter your message.');
}
// prepare headers
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/plain; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$email>" . PHP_EOL;
$headers .= "Return-Path: $to" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
// send the email
$result = #mail($to, $subject, $body . "\r\n\n" .'------------------ '. "\r\n\n" .'Hello '.$name.' we will contact you as soon as possible about your query.' ."\n". 'Dont forget to keep visiting www.pixelwars.com for more updates and awesome content.' ."\n". 'We will email you back on the provided email below, thank you and have a nice day.' . "\r\n\n" .'-- '.$email, $headers);
if ($result) {
successHandler('Thank You! we will be in touch');
} else {
errorHandler('Sorry there was an error sending your message.');
}
} else {
errorHandler('Allowed only XMLHttpRequest.');
}
?>
Thank you in advance if anyone can crack it
You don't have $email assigned when you are defining $to so your second address is not set.
Demo: https://3v4l.org/QIIJu
Solution, move the $to assignment to later in the script. Also use error reporting, this would have thrown an undefined variable notice.
e.g.
<?php
$from = 'Pixel Wars - Press Inquiry';
$subject = 'Press Inquiry from Pixelwars.com';
....
$to = "my-email#gmail.com, $email";
$result = #mail($to, $subject, $body ....
because at this point the $email is defined. Also don't use error suppression, that is just hiding useful information. If you don't want it displayed hide the error displaying but still log them.
You need to add the multiple email address in $to
$to = "address#one.com, address#two.com, address#three.com"
Needs to be a comma delimited list of email adrresses.
mail($email_to, $email_subject, $thankyou);
Thanks
I'm trying to make it so if the e-mail sends successfully the $emailwill get a automatic response from the $mail_to preferably with HTML signature included.
CODE:
<?php
$mail_to = 'REMOVED#EMAILERE.com';
// specify your email here //
$name = $_POST['name'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$message = $_POST['message'];
// Construct email subject
$subject = 'Enquiry Form Submission';
$body_message .= 'Stage Name: ' . $name . "\r\n";
$body_message .= 'E-mail Address: ' . $email . "\r\n";
$body_message .= 'Reason for Contacting: ' . $reason . "\r\n";
$body_message .= 'Message: ' . $message . "\r\n";
$body_message .= "IP Address: " . getUserIpAddr();
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){
?>
<script language="javascript" type="text/javascript">
window.location = 'http://www.sharpturnnetwork.com/forms/success';
</script>
<?php
}
?>
<?php
function getUserIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //if from shared
{
return $_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //if from a proxy
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
}
}
?>
If you're just trying to pass the user information to another PHP page (http://www.sharpturnnetwork.com/forms/success), form the URL using GET parameters (e.g., http://www.sharpturnnetwork.com/forms/success?ip=$ip). But honestly, I'm not really sure what you're asking.
Note: PHP mail() returns TRUE or FALSE, but it does NOT tell you whether the email was delivered successfully. This just is not possible in PHP.
From PHP documentation on mail():
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
I am struggling with the following.When a user fills in a form looking like this, the inquiry gets sent to us to follow up the lead, but I need to find a way to modify my email script to send a confirmation email to the user aswell
My email script looks like this:
MY QUESTION HOW DO I MODIFY THIS TO SEND A THANK YOU CONFIRMATION TO THE USER
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$eventType = $_POST['eventType'];
$nrGuests = $_POST['nrGuests'];
$state = $_POST['state'];
$suburb = $_POST['suburb'];
$msg = $_POST['msg'];
$refferal_page = $_POST['page'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!isset($refferal_page)){
$refferal_page="Source Unknown";
}
$to_email = "unknown#yahoo.co.nz"; //Recipient email
//TODO Sanitize input data using PHP filter_var().
$suburb = filter_var($_POST["suburb"], FILTER_SANITIZE_STRING);
$subject = "You Have a New Booking!";
//Add booking INFO here;
$message = "A new Enquiry has been received from http://www.xxxxx.com.au. Please find details below and follow up:";
//email body
$message_body = $message."\r\n\r\n".$name."\r\nEmail : ".$email."\r\nPhone Number : ". $mobile."\r\nEvent Type: ".$eventType."\r\nNumber Guests:".$nrGuests."\r\nState: ".$state."\r\nSuburb:".$suburb."\r\nIP ADDRESS:".$ip."\r\nMessage:".$msg."\r\nReferal Page Source:".$refferal_page;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if($send_mail)
{
echo '<h1 class="page-header">Enquiry Successfully submitted</h1>';
}
else if(!$send_mail){
echo'<h1 class="page-header" style="color:RED">OOPS...SOMETHING WENT WRONG, PLEASE TRY AGAIN!</h1>';
}
}//server request method
else{
echo'<h1 style="color:red">SUBMIT A FORM PLEASE!</h1>';
}
?>
EMAIL THAT COMES TO US
A new Inqury has been received from www.xxxx.com.au. Please find details below and follow up:
FROM: Ryan XXX
Email : aass#testms.com
Phone Number : 334533
Event Type: Aniversary
Nr Guests: 34
State: Melbourne
Suburb: XYZ
IP ADDR: 162.111.255.111
MSG: this is a test. please disregard
Referal Page Source:aniversary
Obviously I dont want user to see this I just want to add something like "Thank you we will get back to you soon"
Any idea how I can modify the above script for us to get inquiry form but also for user to get an confirmation email?
Any help greatly appreciated
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$eventType = $_POST['eventType'];
$nrGuests = $_POST['nrGuests'];
$state = $_POST['state'];
$suburb = $_POST['suburb'];
$msg = $_POST['msg'];
$refferal_page = $_POST['page'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!isset($refferal_page)){
$refferal_page="Source Unknown";
}
$to_email = "unknown#yahoo.co.nz"; //Recipient email
//TODO Sanitize input data using PHP filter_var().
$suburb = filter_var($_POST["suburb"], FILTER_SANITIZE_STRING);
$subject = "You Have a New Booking!";
//Add booking INFO here;
$message = "A new Enquiry has been received from http://www.xxxxx.com.au. Please find details below and follow up:";
//email body
$message_body = $message."\r\n\r\n".$name."\r\nEmail : ".$email."\r\nPhone Number : ". $mobile."\r\nEvent Type: ".$eventType."\r\nNumber Guests:".$nrGuests."\r\nState: ".$state."\r\nSuburb:".$suburb."\r\nIP ADDRESS:".$ip."\r\nMessage:".$msg."\r\nReferal Page Source:".$refferal_page;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if($send_mail)
{
echo '<h1 class="page-header">Enquiry Successfully submitted</h1>';
$EmailTo = $email;
$subject = "recipient subject";
$message_body = "Hi ".$name."recipient message";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: yourname<$youremail>\n";
$send_cust_mail = mail($to_email, $subject, $message_body, $headers);
}
else if(!$send_mail){
echo'<h1 class="page-header" style="color:RED">OOPS...SOMETHING WENT WRONG, PLEASE TRY AGAIN!</h1>';
}
}//server request method
else{
echo'<h1 style="color:red">SUBMIT A FORM PLEASE!</h1>';
}
?>
I'm using a basic PHP mail form on a website & my client has mentioned to me that he is getting a random email every day or so in his inbox that is totally blank. I read about the issue and was thinking that the mail form is sending an email every time the page is loaded, is that correct? Or is this another issue?
PHP
<?php
$mail_to = "email1#gmail.com, email2#gmail.com"; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$web = $_POST['sender_web'];
$company = $_POST['sender_company'];
$addy = $_POST['sender_addy'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Web Prayer Request from ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Prayer Request: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');
window.location = 'prayer-request.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator info#info.com');
window.location = 'prayer-request.php';
</script>
<?php
}
?>
Link to site: http://tinyurl.com/dy48jom
Your help is much appreciated!
You have to consider implementing captcha in your mail form so that automated scripts wont send mail from your domain .captcha also make sure that you make some of the fields as required (validate not only using js but also using server side php code).
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";