I have written a php mail function to allow a user on my website to fill in a form and send the form to my email. as the question says the email is working once the user send the form however it only appear in my junk email folder instead, i am not a php developer but after doing some research i have noticed a lot people mentione about PHPMailer which i never heard of or used before.
i would much appreciate with a bit oh help.
$to="myemail.com";
//Errors
$nameError="";
$emailError="";
$errMsg="";
$errors="";//counting errors
$name="";
$email="";
$message="";
if(isset($_POST['send'])){
if(empty($_POST['yourname'])){ //name field empty
$nameError="Please enter your name";
$errors++; // increament errors
}else{
$name= UserInput($_POST['yourname']);
if(!preg_match("/^[a-zA-Z ]*$/", $name)){
$nameError="Only letters and white space accepted";
$errors++;
}
}
if(empty($_POST['email'])){
$emailError="Enter email";
$errors++;
}else{
$email = UserInput($_POST['email']);
if(!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)){
$emailError="Invalid Email";
$errors++;
}
}
if(empty($_POST['msg'])){
$errMsg="Enter message";
$errors++;
}else{
$message=UserInput($_POST['msg']);
}
if($errors <=0){//No errors lets setup our email and send it
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <' . $email . '>' . "\r\n";
$text = "<p>New Message from $name </p>";
$text .= "<p>Name : $name</p>";
$text .= "<p>Email : $email</p>";
$text .= "<p>Message : $message</p>";
mail($to, "Website Contact", $text, $headers);
$success="Thank your message was submitted";
$_POST= array(); //clearing inputs fields after success
}
}
//Filter user input
function UserInput($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Your using the email from their posted variable in your header. When your email server receives this it's going to look like it is spoofed because it is. Your site isn't going to be one of the mail servers setup for that domain.
When setting up MX and DNS records for email you use SPF or key signing to prove who sent the message and that it came from a trusted mail server for that domain. You may want to change the from to be an email and domain you control. You are getting their email in the body anyway.
Worst case if you don't have control over SPF records you could at least mark the from email, assuming it is something you control, as always trusted so it wouldn't go into your junk mail.
$headers .= 'From: <' . $to . '>' . "\r\n";
Related
I am trying to send email from a web page hosted on a shared platform over at fasthosts. I cannot for the life of me get this to work, I had a much more extensive script which checked the validity of email etc, but now I've been reduced to using the basic example from fasthosts and it is still not working.
Please could someone take a look and let me now where I am going wrong...
<?php
// You only need to modify the following two lines of code to customise your form to mail script.
$email_to = "contact#mywebsite.co.uk"; // Specify the email address you want to send the mail to.
$email_subject = "Feedback from website"; // Set the subject of your email.
// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("contact#mywebsite", $email_from);
// Get the details the user entered into the form
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The \r\n is the code to use a new line.
$headers = "From: " . $email_from . "\r\n";
$headers .= "Reply-To: " . $email_from . "\r\n"; // (You can change the reply email address here if you want to.)
// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "\r\nEmail: " . $email . "\r\nMessage: " . $message ;
// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);
// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$name .' Thank you for contacting us.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}
?>
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.
Im new to this PHP stuff so please excuse my ignorance
Im after having just one input text box in my flash website where a person just enters there email address and at the click of a button it sends an email to me to a pre defined email address with a predefined subject heading and the email address that was entered in the body of the email
Anyone know of any links or can give some help
all the ones i have found want names email subject message and so on
Any help is appreciated
Mark
EDIT
ok I have the following
In flash I have an input text converted to a movieclip called "addy". Inside the movie clip which has the inputbox which has the variable name "emailaddy"
A Button called "email"
The code i Have running when "email" is clicked is
on (release) {
form.loadVariables("email.php", "POST");
}
the email.php script is as follows
<?php
$sendTo = "mark#here.co.uk";
$subject = "Subscribe to Website";
$headers = "From: Website";
$headers .= "<" . $_POST["addy"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["addy"] . "\r\n";
$headers .= "Return-Path: " . $_POST["addy"];
$message = "Please Subscribe me to Website";
mail(recipient, subject, message, other headers);
mail($sendTo, $subject, $message, $headers);
?>
when I click the button nothing happens
what im after is when the button is clicked for and email to be sent in the following format
To: "mark#here.co.uk"
From: email address specified in text field "addy"
Subject: "Subscribe to Website";
body: "Please subscribe me to Website"
Your help is greatly appreciated
mark
The following code might help:
<?php
$user_mail=$_POST["mail"]; //or $user_mail=$_GET["mail"]; Set to your convenience!
$to_mail="abcde#xyz.com"; //Change to your email address
$message="New user's Email: ".$user_mail; //Change to your requirements
$subject="New user registered"; //Change to your preferred subject
$from="registration#yourwebsite.com"; //Change to your website mail id
mail($to_mail,$subject,$message,"From: $from\n");
To know more about the mail function, please see the documentation.
Well in depth you can do like this
<?php
if($_POST){
$userEmail = $_POST0["emailaddy"]; // textbox variable name comes here
$to = 'abc#xyz.com'; //write down here your email
$subject = 'Subscribe to website'; // your subject goes here
$message = 'Please Subscribe me to Website'; // Mail body message
$headers = 'From: ' . $userEmail . "\r\n" .
'Reply-To: ' . $userEmail . "\r\n" .
'Return-Path: ' . $userEmail; //can send x-Mailer also
mail($to, $subject, $message, $headers);
}else{
echo "Invalid Request";
return false;
}
Don't forget to check first $_POST is happened or not. No need to send Return-path instead of this use x-Mailer which sound good for other mail service providers.
To know more about this read documentation here.
If you want to connect to an SMTP server ,like Postfix or gmail there is a neat php library called PhpMailer.
It is well documentated, so you should be good to go by googleing it :)
I'm creating a simple mail form with checkboxes, a couple of input tabs, and a text input section. It uses PHP to retrieve the information then email it to a specific email. Right now I have it emailing to my own yahoo email just for testing. When I test the site on my hosting account jacobbuller.com/testsites/peacock/contact.php the form works perfectly and forwards the email from my generic "theski" server email. But when I upload the site to the actually live hosting account for peacockautoservice.com the contact form works - it executes and sends a ?msg=1 variable in the url - but I never receive the email in my Yahoo account...
Here's the PHP I am using to send the email.
<?php ob_start();
<?php
$required_field = array('name', 'email', 'message');
foreach($required_field as $fieldname) {
if (!isset($_POST[$fieldname])) {
$errors[] = $fieldname;
}}
if (empty($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$phone = $_POST['phone'];
$services = IsChecked('checkboxes', 'factory');
$services .= IsChecked('checkboxes', 'timing belt');
$services .= IsChecked('checkboxes', 'brakes');
$services .= IsChecked('checkboxes', 'computerized');
$services .= IsChecked('checkboxes', 'steering and suspension');
$services .= IsChecked('checkboxes', 'heating and air');
$services .= IsChecked('checkboxes', 'electrical');
$services .= IsChecked('checkboxes', 'other');
$body = "Customer:" . $name;
$body.= "Phone Number:" . $phone;
$body.= "Contact:" . $contact;
$body.= "Services:" . $services;
$body.= "Message:" . $message;
$to = "jcbbuller#yahoo.com";
$subject = "Peacock Auto Services Inquirey";
$from = $email;
$mailed = mail($to, $subject, $body, $from) or die("Error!");
}
if($mailed) {
redirect_to("../contact.php?msg=1");
}
?>
<?php
// IsChecked FUNCTION - Detemines what checkbox are/aren't checked on form.
function IsChecked($postname, $value){
if(!empty($_POST[$postname])) {
foreach($_POST[$postname] as $job) {
if ($job == $value) {
$project = " ". $value . " ";
return $project;
}
}
}
} //END IsChecked FUNCTION
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
?>
<?php ob_end_flush(); ?>
Please let me know if you see something wrong with the PHP above or if you know why their GoDaddy hosting account is not executing the email. I tried using their customer service network but they said that they can't help me with my code...
Your issue is mainly at server end. Mail function is working because of your check on it, if it had failed, it would have given you notification. So, mails are going definitely. If mail server is working properly at your production server, then check for SPAM folder at yahoo mail server. I would suggest you to ask your hosting provider to enable SPF and DKIM records because most of email providers requires sender authentication (if it is not a spam) and these records are helpful in it.
I can also see that your not using any headers, so I would suggest you to use extended headers to avoid providers identifying you as a spammer. I use below mentioned headers and my emails never go in spam on anyprovider, but again it depends on IP reputation of the server as well.
$headers .= "Reply-To: Awaraleo <email#domain.org>\r\n";
$headers .= "Return-Path: Awaraleo <email#domain.org>\r\n";
$headers .= "From: Awaraleo <email#domain.org>\r\n";
$headers .= "Organization: Awaraleo\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
and then use it like
mail($to,$subject,$message,$headers);
Here email#domain.org should be a valid email address created on the domain where this form is implemented.
Another authentic way is to use SMTP Authentication in coding.
I have a contact form on my website, which actually works :D
The problem is, that the email ends up in the spam folder. I have tried to do stuff with the header section, but nothing seems to work.
Can anyone help me with this?
thanks
<?php
ini_set("display_errors", "0");
$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
$name = $post_data["name"];
$email = $post_data["email"];
$phone = $post_data["phone"];
$website = $post_data["website"];
$message = $post_data["message"];
# select data that needs validation
$validate = array(
'required' => array($name,$email,$message),
'validEmail' => array($email),
'validNumber' => array($phone),
'validAlpha' => array($name)
);
$formcontent = "Name: $name \nE-Mail: $email \nPhone: $phone \nWebsite: $website \nMessage: $message \n";
$formcontent = wordwrap($formcontent, 70, "\n", true);
$recipient = "thomas.teilmann#gmail.com";
$subject = "Contact Messenbrink.eu";
/*$mailheader = "From: $email \r\n";**/
$mailheader .= "Reply-To: $name <$email>\r\n";
$mailheader .= "Return-Path: $name <$email>\r\n";
$mailheader .= "Content-Type: text/plain\r\n";
$mailheader .= "Organization: Sender Organization\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$mailheader .= "X-Priority: 3\r\n";
$mailheader .= "X-Mailer: PHP". phpversion() ."\r\n";
$mailheader .= "From: $name <$email>\r\n";
function sendMail() {
global $formcontent, $recipient, $subject, $mailheader;
mail($recipient, $subject, $formcontent, $mailheader);
}
# error messages
$errorsMsgs = array(
'required' => 'Please fill out all required fields.',
'validEmail' => 'is an invalid email address.',
'validNumber' => 'is an invalid number.',
'validAlpha' => 'contains invalid characters. This field only accepts letters and spaces.'
);
$errorMarkup = "<h1>We found a few errors :-(</h1><h2>Please fix these errors and try again</h2><ol>";
$errorMarkupEnd = "</ol>";
$successMarkup = "<h1>Success!</h1><h2>Your form was sent successfully.</h2>";
$backMarkup = "Back to form";
# begin state
$valid = true;
# loop through fields of error types
foreach ($validate as $type => $fields) {
# loop through values of fields to be tested
foreach ($fields as $value) {
# throw error if value is required and not entered
if ($type === 'required' && strlen($value) === 0) {
$errorMarkup .= "<li>$errorsMsgs[$type]</li>";
$valid = false;
break;
}
else if (
$type === 'validEmail' && !filter_var($value, FILTER_VALIDATE_EMAIL) ||
$type === 'validNumber' && !preg_match('/^[0-9 ]+$/', $value) ||
$type === 'validAlpha' && !preg_match('/^[a-zA-Z ]+$/', $value)
) {
if (strlen($value) === 0) {break;} # skip check if value is not entered
$errorMarkup .= "<li>\"$value\" $errorsMsgs[$type]</li>";
$valid = false;
continue;
}
}
}
if ($valid) {
sendMail();
$body = $successMarkup . $backMarkup;
$title = "Form sent";
} else {
$body = $errorMarkup . $errorMarkupEnd . $backMarkup;
$title = "Form errors";
}
# write html ouput
echo "<!DOCTYPE html><head><title>$title</title><style type=\"text/css\">body{margin:100px;font:16px/1.5 sans-serif;color:#111}h1{font-size:32px;margin:0;font-weight:bold}h2{font-size:18px;margin:0 0 20px 0}ol,li{list-style-position:inside;padding-left:0;margin-left:0}</style></head><body>$body</body></html>";
?>
It has nothing to do with the PHP code but make sure that the email are being sent from a domain that is hosted on the server. That means it would be a bad idea to use your gmail address for sending though PHP. Every email that leaves the server needs to be signed and needs to have SPF records
This is usually because in the DNS a correct SPF record has not been setup. This can be easily done depending on the software you use. If you use cPanel then this is pretty much a two step process.
Some links:
http://www.emailquestions.com/help-desk/2418-why-do-my-own-emails-go-into-spam.html
http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=spf+records
http://www.techtalkpoint.com/articles/setting-up-spf-records-in-cpanel-whm-and-namecheap/
Ensure the message source is valid. Other than that, there isn't a lot you can do in PHP. The rest is down to your message contents, your mail server and the recipient server's spam filter.
Try to ensure that the data in your headers matches what would normally appear in a normal message. For instance, it is best that your From and Return-Path domain name match the server you are sending from.
I notice you just use \n as a line delimiter in the body. It should be \r\n irrespective of the OS you are using.
Ideally, study RFC 2822. If you don't have time to do that, at the very least, compare your headers with other emails that you believe to be valid. Where possible, match the order of headers, including placing the same number of blank lines in the appropriate places.
Spam isn't filtered by some setting, it's filtered by your word choice and how the message looks and also how well known the sender email address is. If you want to avoid the spam list, you have to word it in a way that does not sound spammy.
My one suggestion would be to have the email's From address be the email address of the actual mail account you're using to send these rather than the customer's email account. Your email provider may think they're spoofing if the email comes from their email address but not from a server on that domain.
You could instea put the customer's email address in the body of the email so you know who it's from.
It's probably the smtp being used that is not valid.
If the contact form is just used to send emails to one mailfolder, I would just set up a rule there to not mark it as spam. Either that or actually validate your smtp.
I was having a similar problem with Gmail, I just selected the email and set to "Not Spam" now all emails sent by my server, when someone uses the contact form on my site end up on Inbox folder. Hope this helps.