I've created a PHP file to work with my form on my jQuery mobile site. The form works perfectly and sends the email and errors work and all. But I keep getting the email from an (unknown sender). Subject line and email information is there. (Email coming from the form to host the host email address). Thanks for any help that can be provided.
<?php
if(isset($_POST['email'])){
// Here is the email to information
$email_to = "hostemail#email.com";
$email_subject = "Customer Service Form";
$email_from = "Company";
//error code
function died($error){
echo "We are sorry, but there were errors found with the form you submitted.";
echo "These errors appear bellow.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
//validation
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
//$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z] {2,4}$/';
//(!preg_match($email_exp, $email)) {
//$error_message .='The Email Address you entered does not appear to be valid.<br/>';
// }
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $name)){
$error_message .= 'The name you entered does not seem to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The comments you entered do not appear to be valid.<br/>';
}
if(strlen($error_message) > 0 ) {
died($error_message);
}
$email_message = "Form details below. \n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string ($name) . "\n";
$email_message .= "E-Mail:" . clean_string ($email) . "\n";
$email_message .= "Message:" . clean_string ($message) . "\n";
//create email headers
$headers = 'From:' .$email_From . "\r\n" . 'Reply-To:' . $email. "\r\n" .
'X-MAILER: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success Message Goes Here -->
Thank you for contacting us. We will be in touch with you shortly. <br/>
Please Click here to go back to the contact page.
<?php
}
?>
The format for the From header is:
Display Name <email address>
For example:
Company <foo#company.com>
Right now, you're just using "Company", which is neither a valid e-mail address on its own, nor has an e-mail address at the end.
Related
How would I go about making this form able to send to multiple recipients? At the moment it's only allowing me to send it to 1 email only, and when I try typing multiple in (e.g "user1#example.com, user2#example.com") it returns the message for when it's invalid. What do I need to do to fix this?
EDIT: The user has to input the email address it wants to sent to, but it only works with 1, which is why I'm asking for help on how I can edit the code to work with multiple emails/recipients and to be separated with a comma and space.
Here is the code
<?php
if(isset($_POST['email'])) {
$email_to = array($_GET["celebrant_emails"]);
$email_subject = "Email from website Contact Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the email you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go <a href='http://celebrantsaustralia.asn.au/celebrants-trial.htm'>back</a> and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments']) ||
!isset($_POST['celebrant_emails'])) {
died('We are sorry, but there appears to be a problem with the email you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$celebrant_emails = $_POST['celebrant_emails']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Message you entered does not appear to be valid.<br />';
}
if(!preg_match($email_exp,$celebrant_emails)) {
$error_message .= 'The Celebrant Email(s) you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Email details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<html>
<head>
<title>Email sent!</title>
</head>
<body>
<b>Thank you for contacting us. We will be in touch with you shortly.</b>
<p>(Page will auto-direct in a moment, if it doesn't click here)</p>
</body>
</html>
<?php
}
?>
There are numerous ways of doing this.
$email_to = "jhewitt#amleo.com,some#other.com,yet#another.net";
$email_to = 'Mary <mary#example.com>, Kelly <kelly#example.com>';
If you need to add emails as CC or BCC, add the following part in the variable you use as for your header :
$headers .= "CC: sombodyelse#noplace.com".PHP_EOL;
$headers .= "BCC: hidden#special.com".PHP_EOL;
Source: PHP form send email to multiple recipients
Also check here: Example #1 Sending mail.
$email=$_POST['email']; // write multiple emails with comma's
$emails=explode(',', $email); // explode email with comma's
foreach($emails as $one_email)
{
$touser=$one_email;
$subjectAdmin= "subject";
$headersAdmin = "From: noreply#talentswype.com\r\n";
$headersAdmin .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$messageuser ='msg here';
$emailSenduser = mail($touser,$subjectAdmin,$messageuser,$headersAdmin);
}
Write one mail function per email.
mail('example1#example.com', 'My Subject', $message);
mail('example2#example.com', 'My Subject', $message);
mail('example3#example.com', 'My Subject', $message);
mail('example4#example.com', 'My Subject', $message);
mail('example5#example.com', 'My Subject', $message);
or
foreach($mail as $mails ){
mail($mail, 'My Subject', $message);
}
I am trying to send a data from a form to my email.
I try using the form in my server (free hosting) and it is fine. When I try to use it on another server (runs through cloudflare ) it gives me 504 gateway error. Any idea why?
Is it on my side or do I have to fix something?
My server page : http://mywg1.x10host.com/m1/
Cloudflare run page : http://wholesaledrywalltoronto.therenopros.ca/
the PHP for the form :
<?php
if(isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email.x#gmail.com";
$email_subject = "Drywall Pros Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Phone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$Name = $_POST['Name']; // required
$Email = $_POST['Email']; // required
$Phone = $_POST['Phone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$Name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($Name)."\n";
$email_message .= "Email: ".clean_string($Email)."\n";
$email_message .= "Phone: ".clean_string($Phone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Fixed it . For anyone who is having same errors . It is probably because the server you are working with only allows SMTP and not the PHP mail function.
I am trying to make a contact form using HTML and PHP. The PHP for the form is below:
<?php
if(isset($_POST['email'])) {
// Email to information
$email_to ="personalemail#email.com";
$email_subject ="Contact";
$email_from ="Person";
// Error code
function died($error) {
echo "We are sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
// Validation
if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['email']) || !isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message .= 'The email address you entered does not appear to be valid.<br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $fname)) {
$error_message .= 'The first name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $lname)) {
$error_message .= 'The last name you entered does not appear to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br/>';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string($fname) . clean_string($lname) . "\n";
$email_message .= "Email:" . clean_string($email) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";
// Create email headers
$headers = 'From: ' .$email_From . "\r\n". 'Reply-To:' . $email. "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
Thankyou for contacting me. I will be in contact with you shortly. <br/>
Please click here to go back to the main website
<?php
}
?>
The issue is that when the form is submitted, it never sends an email to my own personal email. Is there something more I have to set up with the web hosting or is the problem purely in the code? I did some research about the appropriate code to put in and have tried many different options but none seem to work.
Forms that send email are errorprone, and worse yet, if something goes wrong neither you nor the end user usually get an error message. They will just think your customer service is not responding. Things that do go wrong: webserver email server down or malconfigured, recieving mailbox full, spamfilter eat email, rules on webserver's emailserver change and throws away your mails.
I recommend a cloud hosted form, that stores the form submissions and notify you by email, but email is not the primary data storage.
Something like this perhaps, has a free plan:
http://www.wufoo.com/features/
This question already has answers here:
How to do email form with multiple recipients and different body?
(3 answers)
Closed 9 years ago.
I have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.
here my previous code :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "admin#gmail.com";
$email_subject = "Lifemailer Sales Enquiry";
$email_to_user= "Name: ".clean_string($name)."\n";
function died($error) {
// your error code can go here
$URL = "error.html";
header("Location: $URL");
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['contact']) ||
!isset($_POST['email']) ||
!isset($_POST['email_sub']) ||
!isset($_POST['remarks'])) {
died('We are sorry, but there appears to be a problem with the form your
submitted.');
}
$name = $_POST['name']; // not required
$contact = $_POST['contact']; // required
$email = $_POST['email']; // required
$email_sub = $_POST['email_sub']; // required
$remarks = $_POST['remarks']; // required
$error_message = "";
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
$string_exp = "^[0-9 .-]+$";
if(!eregi($string_exp,$contact)) {
$error_message .= 'The Contact Number you entered does not appear to be valid.
<br />';
}
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.
<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Contact Number: ".clean_string($contact)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Email Subject : ".clean_string($email_sub)."\n";
$email_message .= "Remarks/Enquiry : ".clean_string($remarks)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
$URL = "thank-you.html";
header("Location: $URL");
?>
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
In the same way you can send a second mail with different subject, email_to, email_message after sending it to admin.
just concatenate
$email_to = ' admin#gmail.com ' . ',' ;
$email_to . = ' admin#gmail.com ' ;
Right now, I've set up a PHP e-mail form and everything works fine. However, while testing it, I'm only getting the sender e-mail address as the name. What I want is the name of the sender, like:
John Doe
Subject Subject Subject
Instead of:
j.doe#website.com
Subject Subject Subject
Below is the code...
Can someone help me please? Thanks.
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ricky#rickytsang.ca";
$email_subject = $_REQUEST['subject'];
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['full_name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$full_name = $_POST['full_name']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject'];
$message= $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The e-mail you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$full_name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered doee not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Full Name: ".clean_string($full_name)."\n";
$email_message .= "E-mail: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Right after your 4 lines that say $email_message .= ... add a line:
$email_from = $full_name.'<'.$email_from.'>';
THen you need it in this form:
"John Doe <j.doe#website.com>"
Usualy you simply need to reformat the from address like this:
"Ricky, Dear" <dearricky#yahoo.com>
http://php.net/manual/en/function.mail.php
In the headers you must set the From header like this:
<?php
...
$headers = 'From: ' . clean_string($full_name) . ' <' . clean_string($email_from) . ">\r\n";
..
?>
Thank you stimms!
So, your whole code portion would look like this:
// prepare email body text
$message = "";
$message .= "Thanks for registering .....";
$message .= "\n";
$message .= "More stuff here";
$headers = "From: John Smith <info#yourcoolsite.com>";
mail($recipientemail, "Hey thanks for joining!", $message, $headers);
Then the email recipient will see an email in their inbox that looks like this:
FROM: John Smith
SUBJECT: Hey thanks for joining!
MESSAGE: Thanks for registering ..... More stuff here
You set the e-mail address like this
$email_to = "Ricky <ricky#rickytsang.ca>";
http://php.net/manual/en/function.mail.php
change these 4 lines
$full_name = $_POST['full_name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message= $_POST['message'];
to:
$full_name = $_POST['full_name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message= '"' . $full_name . '" <' . $_POST['message'] . '>';
please make sure u copy paste this as it is.
Drupal e-mail Form Sender Name Instead Of E-mail with Domain Basis?
Example:
function hook_mail_alter(&$message) {
if($message['id'] == "put_id" && $domain_id == 368){
$default_from = variable_get('site_mail', ini_get('sendmail_from'));
if($message['from'] == $default_from){
$message['from'] = '"'. variable_get('site_name', 'Drupal') .'" <'.
$default_from .'>';
$message['headers']['From'] = $message['from'];
}
}
}