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>";
}
?>
Related
I'm trying to create a contact form where the user will fill out their name, email, subject and message in order to contact me. It is suppose to send an email to my email account but every time i test it, it does not work. I was positive it was correct, but i guess it is not. Any help please?
<?php
//Get user input
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"]
$message = $_POST["message"];
//error messages
$missingName = '<p><strong>Please enter your name!</strong></p>';
$missingEmail = '<p><strong>Please enter your email address!</strong></p>';
$invalidEmail = '<p><strong>Please enter a valid email address!</strong></p>';
$missingSubject = '<p><strong>Please enter a Subject!</strong></p>';
$missingMessage = '<p><strong>Please enter a message!</strong></p>';
//if the user has submitted the form
if($_POST["submit"]){
//check for errors
if(!$name){
$errors .= $missingName;
}else{
$name = filter_var($name,FILTER_SANITIZE_STRING);
}
if(!$email){
$errors .= $missingEmail;
}else{
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$errors .=$invalidEmail;
}
}
if(!$subject){
$errors .= $missingSubject;
}else{
$message = filter_var($subject, FILTER_SANITIZE_STRING);
}
if(!$message){
$errors .= $missingMessage;
}else{
$message = filter_var($message, FILTER_SANITIZE_STRING);
}
//if there are any errors
if($errors){
//print error message
$resultMessage = '<div class="alert alert-danger">' . $errors .'</div>';
}else{
$to = "fanonxr#gmail.com";
$subject = "Contact";
$message = "
<p>Name: $name.</p>
<p>Email: $email.</p>
<p>Subject: $subject.</p>
<p>Message:</p>
<p><strong>$message</strong></p>";
$headers = "Content-type: text/html";
if(mail($to, $subject, $message, $headers)){
$resultMessage = '<div class="alert alert-success">Thanks for your message. We will get back to you as soon as possible!</div>';
header("Location: index.php");
}else{
$resultMessage = '<div class="alert alert-warning">Unable to send Email. Please try again later!</div>';
}
}
echo $resultMessage;
}
?>
it seems you are missing the form action and mailto since its Html i think those should be included.
your validation looks good to me.
In terms of sending email, try finding some answers here:
PHP mail form doesn't complete sending e-mail
If that doesn't address your question, please provide some more info, like what mailer you're using. Hope this helps :)
I've recently started using HTML, Javascript and PHP to create my own website.
I have been trying to make a PHP script that takes inputs from a form, validates with javascript and php, then sends the email.
I know the script is executed and runs fine, as it sucessfully redirects me to the new page at the end. However, it does not actually send any emails for some reason.
Here is the code:
<?php
$errors = '';
$myemail = '<removed due to privacy>';
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
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: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$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');
}
?>
Now I did see a few similar questions, but as I am very incompetent at php, they didn't really help.
If you can identify the reason, I'd be very grateful.
$sent = mail($to,$email_subject,$email_body,$headers);
put a condition like this
if($sent){ header('Location: contact-form-thank-you.html'); } else { echo 'mail failed';}
if it show "mail failed" configure your localhost to sent mails
follow this tutorials
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 7 years ago.
Can someone help me figure out why the below PHP is causing the email to be sent to Gmail spam? I tried following other directions for setting proper headers, but I still run into problems with my emails going into the spam filter in Gmail.
Any help would be appreciated!
<?php
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
$name = trim($_POST['txt_name']);
$fromemail = trim($_POST['txt_email']);
$inquiry = trim($_POST['txt_inquiry']);
$message = trim($_POST['txt_msg']);
//name can contain only alpha characters and space
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Please enter a real name";
}
if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
{
$error = true;
$fromemail_error = "Please enter a valid email address";
}
if(empty($inquiry))
{
$error = true;
$inquiry_error = "Please enter your subject";
}
if(empty($message))
{
$error = true;
$message_error = "Please enter your message";
}
if (!$error)
{
//send mail
$toemail = "myemail#gmail.com";
$subject = "inquiry from visitor " . $name;
$body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Inquiry: $inquiry \n Message: \n $message";
$headers = "From: $fromemail\n";
$headers .= "Reply-To: $fromemail";
$headers .= "Return-Path: $fromemail";
if (mail ($toemail, $inquiry, $body, $headers))
$alertmsg = '<div class="alert alert-success text-center">Message sent successfully. We will get back to you shortly!</div>';
else
$alertmsg = '<div class="alert alert-danger text-center">There is error in sending mail. Please try again later.</div>';
}
}
?>
It could be that your ip address of the server that you are sending the email with it used for other purposes that marked it as a spam address.
Most of the time when you add DKIM and SPF to your email server (if you use one) will solve this problem.
An essier solution would be to use an external mailing service so that you they can handle that for you
I often made the experience, that mails that are directly getting sent by PHP are recognized as SPAM...
my approach is now, to always use a SMTP-Server for sending those emails...
this post could help you!
I made a form for contacting me on my website.
This is the php code:
<?php
$errors = '';
$myemail = 'yourname#website.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
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: $name";
$email_body = "You have received a new message. ".
"name: $name \n\n email: $email_address \n\n\n $message";
$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_thanks.html');
}
?>
When I use the form on the website, I receive a mail with weird symbols. That is because I write in Hebrew in the form- English letters are delivered well.
When I change "myemail" to another mail, the message is delivered well, even in Hebrew, but I want the message to pass to the first mail.
I changed many times the encoding, but none seems to solve the problem, perhaps I hadn't tried the correct one yet.
I am not the one who wrote the code and my I don't know php well, so please give me answers that are easy to comprehend.
You have to specify content encoding in your mail header. For this reason, add this line:
$headers .= "\nContent-type: text/html; charset=UTF-8";
Hello Fellow Stackers,
New to PHP and I am putting together a multipage form from pre-built code.
Basically the user selects as many checkboxes as they want... then the form submits to this secondary page. This secondary page echo's the checkboxes they chose at the top of the page via $check.. then they can enter their contact information and all of the information gets submitted via form, along with the $check information.
Everything is working perfectly except $check isn't being entered into the form message, but it works up at the top of the page, displaying which options the user inputted.
Any help is appreciated!
<?php
$emailOut = '';
if(!empty($_POST['choices'])) {
foreach($_POST['choices'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
$emailOut .= $check."\n"; //any output you want
}
}
$errors = '';
$myemail = 'test#myemailHERE.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
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: $name";
$email_body = "You have received a new message. $check ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message \n $emailOut";
$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 thing in this situation is that when you get down to the email, $check is the last option displayed. You need to use the foreach statment to build the array or email output such as
$emailOut = "";
foreach($_POST['choices'] as $check) {
$emailOut .= $check."\n"; //any output you want
}
Then use your email variable in the same way
$email_body = "You have received a new message. Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message \n $emailOut";
UPDATE
From further investigation and more code submitted, it appears that you are working with a multi-form submssion issue. The issue is you have form 1 (checkboxes) that submits to form 2 (email).
Since when doing the checks after the checkbox submission, no name, email, etc was given so $errors were given and no email sent. When filling out the email form, the checkboxes were not sent again so $check or even $_POST['choices'] had values.
You can either put the two forms into one, or you can look into a way to save the values by passing them and filling a 'hidden' field (<input type='hidden' value='...'>) or use a session with PHP.