Well my PHP contact us form is sending email too well
But I am getting 7 emails per submission
Problem is I have no idea why I am getting 7 emails per submission.
Any thoughts?
CODE
<?php
if(isset($_POST['Send'])){
$first_name =trim($_POST['first_name']);
$last_name=trim($_POST['last_name']);
$phone_number=trim($_POST['phone_number']);
$email=trim($_POST['email']);
$msg=trim($_POST['msg']);
$name=$first_name." ".$last_name;
if($first_name == '' ||$last_name =='' || $phone_number == '' || $email == ''|| $msg == '' ){
$merror = "<p style='color:red;'> * Kindly fill all Fileds<p>";
}else{
foreach($_POST as $value){
if(stripos($value, 'Content-Type:')!== FALSE || $_POST['Address']!== "" ) {
$merror = "<p style='color:red;'> * The information you have entered has a problem</p>";
}else{
require_once "class.phpmailer.php";
$mail= new PHPMailer();
if(!$mail->ValidateAddress($email)){
$merror = "<p style='color:red;'> * Please enter a valid email address</p>";
}else{
$email_body = "";
$email_body = $email_body . "Name: ". $name ."<br>";
$email_body = $email_body . "Phone: ". $phone_number. "<br>";
$email_body = $email_body . "Email: ". $email . "<br>";
$email_body = $email_body . "Message: " . $msg . "<br>";
$mail->SetFrom($email, $name);
$address = "s#example.co";
$mail->AddAddress($address, Trial);
$mail->Subject= "Ess contact form message ".$name;
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
echo"<script>window.open('Contact.php','_self')</script>";
}
echo"<script>window.open('Contact.php?status=thanks','_self')</script>";
}
}
}
}
}
?>
You also have a very common error a lot of people have with "Contact Us" forms.
$mail->SetFrom($email, $name);
This will break SPF and also cause DMARC to fail and you will never get the message from some people, if your mail server you use has DMARC enabled on it and GMAIL does.
Since DMARC is a more recent protocol, a lot of the old cookie cutter code for contact us forms - doesn't take this into account.
You can read more about that here: "DMARC - Contact Us Form Nightmare"
The suggested workaround will be to do:
$mail->SetFrom("<Your email Adddress>, $name);
You have the the customers contact email in the body of the message which is perfect.
This way - you avoid the issue outline in the article. You won't quickly be able to hit the "Reply" button, but at least you'll get the emails from those customers who have DMARC enabled.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
So I am trying to send an email to notify the office admin of when an email has been submitted through a form. Everything worked with no problems when I used an email ending in ".com" However, when I tried using an email ending in ".org"
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$message = $_POST['message'];
if(isset($_POST['submit'])){
$to = "officeadmin#example.org"; // Office Admin Email
$from = $email_address; // User's Email
$subject = "Form Submission";
$content_admin = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $message;
$content_user = "Thanks again for contacting us. A dedicated staff member will contact you as soon as possible. Your message was: \n\n" . $message;
$headers_admin = "From:" . $from;
$headers_user = "From: Example Office";
mail($to,$subject,$content_admin,$headers_admin); //Notification email for admin
mail($from,'Thanks for contacting us',$content_user, $headers_user); //Copy of email sent to user
header('Location: contact_form_success.html'); //Redirect here after submit is clicked
}
Basic mail() function is not well configured so sometimes problem may arise, so you
Try PHPMailer, it is well configured php mail class for sending mail.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So have a single form which only gets the email of the person and sends it to the email address I assigned. But I guess I wrote the code in a wrong way since I am getting errors. I have seen other stackoverflow problems but I am not sure why this isnt working.
This is the form in html.
<form style="margin-bottom:50px;" name="contactform" action="contact-form-handler.php" class="news-letter "method="post">
<div class="subscribe-hide">
<input type="text" name="email" class="form-control" placeholder="Email Address" >
<button type="submit" class="btn"><i class="fa fa-envelope"></i></button>
</div><!-- /.subscribe-hide -->
</form><!-- /.news-letter -->
and here is the PHP code in a different file.
<?php
$errors = '';
$myemail = 'masnadhossain#live.com';
empty($_POST['email']))
{
$errors .= "\n Error: all fields are required";
}
$email_address = $_POST['email'];
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 = "Contact form submission";
$email_body = "You have received a new message. ".
" Here are the details:".
"Email: $email_address\n ";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The error I get is server error 500
Your code have syntax error pleas check it.
Line no 3 should be:
if (empty($_POST['email']))
{
$errors .= "\n Error: all fields are required";
}
1.) The first problem you have is that you are sending the email address from a form input via post but you did not utilize it.
2.) To and From variables are having the same email address masnadhossain#live.com which is creating conflicts
3.) The Preg_Match for checking Email address validity is errorneous. I have re-written your code below.
If you are posting the users email(eg gmail,yahoomail etc) from a form input, this code will get you going.
In case you want to intialize the email within the code just change
$email_address = strip_tags($_POST['email']);
to may be
$email_address = 'user1#gmail.com';
Finally, the From variable must be the email address pointing to your website address. in this case I think it is masnadhossain#live.com
This code is working. please mark it as correct answer if it solve your problem....
<?php
//Users email address coming from form input eg. gmail,yahoomail etc.
$email_address = strip_tags($_POST['email']);
//validate the email address
$email_val= filter_var($email_address, FILTER_VALIDATE_EMAIL);
if (!$email_val){
echo "<font color=red><b>Invalid Email Address</b></font>";
exit();
}
$to=$email_val;
$subject = "Contact form submission";
$message = "Here is the message;
// set the from variable to any email pointing to your website
$from = "masnadhossain#live.com";
$headers = "From:" . $from;
$sent=mail($to,$subject,$message,$headers);
if($sent) {
print "<br><font color=green><b>Your mail was sent Successfully</b></font>";
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
print "<br><font color=orange><b>We encountered an error sending your mail.</b></font>";
}
?>
I have VERY little experience with PHP.
I am testing out a contact form for this website. I am only getting the "subject, message, and header" fields in the email.
I need the telephone and name fields to show in my email message as well.
What am I doing wrong?
Any help is appreciated -- thanks!
<?php
$name;$email;$message;$captcha;$telephone;
if(isset($_POST['name'])){
$name=$_POST['name'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['telephone'])){
$telephone=$_POST['telephone'];
}if(isset($_POST['message'])){
$message=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form. <a href=http://www.website.com#contact/>Back to Form</a></h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcYjgcT****q9vhur7iH_O4dZPl4xUAVwW8=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}else
{
// Checking For Blank Fields..
if ($_POST["name"] == "" || $_POST["email"] == "" || $_POST["telephone"] == "" || $_POST["message"] == "") {
echo "Fill All Fields..";
} else {
// Check if the "Sender's Email" input field is filled out
$email = $_POST['email'];
// Sanitize E-mail Address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email) {
echo "Invalid Sender's Email";
} else {
$to = 'email#gmail.com';
$subject = 'Contact Form';
$message = $_POST['message'];
$name = $_POST['name'];
$headers = 'From:' . $email . "\r\n";
// Sender's Email
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message .= "\r\n Name: " . $name;
$message .= "\r\n Email: " . $email;
$message .= "\r\n Telephone: " . $telephone;
$message .= "\r\n Message: " . $message;
// Send Mail By PHP Mail Function
if (mail($to, $subject, $message, $headers)) {
echo "Your mail has been sent successfully!<a href=http://wwwwebsite.com>Back</a>";
} else {
echo "Failed to send email, try again.";
exit ;
}
}
}
}
?>
The $message variable is the content of the mail that will be sent to your adress, add the values you want to get in your mail to that variable.
like so : (since you're not using HTML mail I think)
$message .= "\r\n Name : " . $name;
$message .= "\r\n Email : " . $email;
etc..
Before you call the mail function.
ALSO :
You're adding Phone number, Email, and message in your $email variable. you should give these their own variable.
I have setup a really simple php message form on my website but for some reason it is not working, when the designer showed it to me from his website it worked perfect but now hosted in mine doesnt work. I am using godaddy.
This is the code:
<?php if (isset($_REQUEST['email'])){
$email = $_REQUEST['email'] ;
$message = " Message: ".$_REQUEST['message']." Name : ".$_REQUEST['name']." Phone : ".$_REQUEST['phone'] ;
mail( "myemail#myemail.com", "Customer Service Email", $message, "From: $email" ); echo "<script> alert('Thanks for your message!'); </script>";}?>
I put myemail#mymail.com as representation, I know im supposed to put my own email.
Hmm. My experience with the mail function is that it relies on the underlying system's mail feature(s). Godaddy probably has it blocked. I had this similar problem and ultimately I used a different mail client other than php's native "mail" function.
I dont know why you are using REQUEST, maybe because you use both GET and POST with this send mail file from two different sources but here is a quick upgrade of your actual code + a check that will return an error if your php mail() is not set/turned on in php.ini config file.
<?php
// First we retrieve our values
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
// Here we make sure that the user has filled in the values
if ( empty($email) ){
echo "<script> alert(\"Please make sure that you filled in your email address!\"); </script>\n";
} elseif ( empty($message) ){
echo "<script> alert(\"Please make sure that you entered a message!\"); </script>\n";
} elseif ( empty($name) ){
echo "<script> alert(\"Please make sure that you filled in your name!\"); </script>\n";
} elseif ( empty($phone) ){
echo "<script> alert(\"Please make sure that you filled in your phone number!\"); </script>\n";
} else {
// Here you can validate data inside of each strings...
// Yes he does, Configure email before sending.
$to = "myemail#mymail.com";
$title = "Customer Service Email";
$message = "Message: " . $message . " | Name: " . $name . " | Phone: " . $phone . " | Email: " . $email . "";
// Mail it!
$send = mail( $to, $title, $message);
// Check if it has been sent
if(!$send){
echo "<script> alert('Hoho, your server cannot send email by using mail() function..'); </script>\n";
} else {
// Show prompt to user!
echo "<script> alert('Thanks for your message!'); </script>";
}
}
?>
I m trying contact form in php.I receive the mails in gmail,yahoo etc but doesnt recieve any mails in hotmail.I tried a lot but doesnt know whats the problem.Why i cant recieve mail in hotmail? Here is the code.
if (empty($error)) {
$to = 'abc#gmail.com, ' . $email;
$subject = " contact form message";
$repEmail = 'abc#gmail.com';
$headers = 'From: Contact Detail <'.$repEmail.'>'.$eol;
$content = "Contact Details: \n
Name:$name \n
Batch: $batch \n
Email: $email \n
mobile: $mobile " ;
$success = "<b>Thank you! Your message has been sent!</b>";
mail($to,$subject,$content,$headers);
}
}
?>
Check the server admin's email for bounce messages. I would guess it has to do with hotmail rejecting it either because it doesn't like your host or because you don't have SPF or DKIM signing set up. Either way, the blame is probably on your server email administrator more than on your code. Email is actually kinda complex to set up nowadays given all the anti-spam stuff you have to do.
You might want to consider using a third party email service. SMTP through a gmail account or Amazon SES so they'll take care of more of these details. This can be set up by the server admin too, or done in PHP with libraries. Is there a SMTP mail transfer library in PHP
this is more likely
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = 'Order' ;
$message =
'Product Name :'. $_REQUEST['proname']."\n". //these all are example details
'Name :'.$_REQUEST['name']."\n".
'Phone :'. $_REQUEST['phone']."\n".
'City :'.$_REQUEST['city']."\n".
'Address :'.$_REQUEST['address']."\n".
'Quantity :'. $_REQUEST['select'];
if ($email == "" || $subject == "" || $message == "" )
{
echo 'Please fill the empty fields';
}
else{
mail("anything#something.com", $subject,
$message, "From:" . $email);
echo 'Thank you ! we will contact you soon';
}
}