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
Related
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 have a problem with my mail in php. I code form to send email. I receive email on gmail but I have other mail address and I can't get email on it.
I checked in spam and there is no email also.
Below is my code.
<?php
$emailErr = "";
$endMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Proszę uzupełnić pole e-mail";
}
else if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$namesurname = $_REQUEST['name_surname'] ;
$email = $_REQUEST['email'] ;
$number = $_REQUEST['number'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$message = $subject . ": " . $message . " " . $number . " " . $namesurname . " " . $email;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
mail("szafor#szafor.pl", "Zamówienie pomiaru",
$message, "From: formularz#szafortest.pl \r\n"."Content-Type: text/plain; charset=UTF-8\r\n");
$endMessage = "Dziękuję za przesłanie wiadomości.";
}
}
?>
One important thing to consider with sending mail is that you should at least have the return path of the message be an email address that is actually hosted on the server that you are sending from.
You can set the From and the Reply-To address as any address, but the return-path should be set to a valid email address hosted on your server. Let's say that you want the "reply" button to send back to "this_email#wherever.com" but the server you are using hosts email for "mydomain.com". Create an email account on your server, "info#mydomain.com" for example.
$recipient = "sendto#email.com";
$subject = "Test email";
$message = "This is the message.";
$headers .= "From: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Reply-To: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Return-Path: Your Name Here <info#mydomain.com>\n\r";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .="X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .="MIME-Version: 1.0\r\n";
mail($recipient, $subject, $message, $headers);
I have found that the more valid header information that I provide, the more likely the email will be delivered. Right now these headers always work for me, and I have a scheduling program that is sending email to a hundred different email addresses every day. See if that works better for you.
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>";
}
}
}
Ive got a contact form that isnt sending but is outputting that the message is sent? Can anybody see a problem?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#email.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Subject: $subject,
Message: $message ";
//end of message
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Website Enquiry";
if (isset($_POST['name'])) {
// now lets send the email.
mail($to, $subject, $message, $headers);
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');
} else {
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');
}
?>
The "From" header should have a syntactically correct email address. You also need to check the return value of the "mail" function.
$header .= "From: Website Enquiry <enquiry#website.com>";
PS: Please improve your code formatting.
Try to enter an email at From: in $headers.
Like $headers .= "From: youremail#provider.com" or
$headers .= "From: Website Enquiry <youremail#provider.com>"
And you should change it to
if(mail(...)) {
//success
}
else {
//email failed
}
I'm trying to solve this puzzle here. The contact form on this page (http://www.b3studios.co.uk/) uses AJAX and PHP. I copied all the code and trying to make it work (reverse engineering PHP). I'm not sure how the PHP file should look like to handle the data given by AJAX. I tried the following PHP file:
<?php
$sender = $email;
$receiver = “test#email.com”;
$email_body = “Name: $name \nEmail: $email \nMessage: $message”;
if( mail( $receiver, $subject, $email_body, “From: $sender\r\n” .
“Reply-To: $sender \r\n” . “X-Mailer: PHP/” . phpversion()) )
{
echo “Success! Your message has been sent. Thank You.”;
}
else
{
echo “Your message cannot be sent.”;
}
?>
After pressing "Send Message" it gets stuck at sending message....
Any suggestions to troubleshoot would be greatly appreciated.
I am pretty sure you copied the code with those ugly double quotes “ (MSWord quotes), you should chage to "
try
<?php
$sender = $email;
$receiver = "test#email.com";
$email_body = "Name: $name \nEmail: $email \nMessage: $message";
if( mail( $receiver, $subject, $email_body, "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion()) )
{
echo "Success! Your message has been sent. Thank You.";
} else {
echo "Your message cannot be sent.";
}
?>