I am having a problem with my contact form and the php mail() function. For some reason, they work for every email address (#gmail, #yahoo, #outlook and even #facebook!) except the old dreaded hotmail. I am just curious as to where my code is missing something. I have checked the mail servers and there is apparently no issue with hotmail addresses.
The email does not even get delivered to the spam/junk folder (it does not reach hotmail). I had a look online and some say to change the headers to avoid being caught in the spam filter. Any pointers to this?
PHP CODE
<?php
header('Content-Type: application/json charset=utf-8');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'companyname#mail.com';
$to = 'myemail#hotmail.com';
$subject = $name . ' has sent you a message';
$human = $_POST['antispam'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['name']) && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '{"status":"1"}';
} else {
echo '{"status":"0"}';
}
}
else
{
echo '{"status":"2"}';
}
?>
The if statements is just a check if all forms are valid and the anti spam (2+2) is correctly entered. nothing much to do in this part. The issue I guess is somewhere in the header
Try using these changes:
<?php
header('Content-Type: application/json; charset=utf-8');
...
$from = 'companyname#mail.com';
$headers = 'From: '. $from. "\r\n";
...
if (mail ($to, $subject, $body, $headers)) {
...
?>
4th parameter of mail function is expected to be additional_headers, not just from address.
Related
I am trying to adjust my code to able to reply to sender email from PHP contact form. please check my code below to give advise. Thank you
<?php
$marke = $_POST['marke'];
$modell = $_POST['modell'];
$name = $_POST['name'];
$adresse = $_POST['adresse'];
$telefon = $_POST['telefon'];
$email = $_POST['email'];
$to = 'myemail#gmail.com';
$from = 'myemail#gmail.com';
$subject = 'Contact Form';
$body = "marke: $marke\n modell: $modell\n name: $name\n adresse: $adresse\n
email: $email\n telefon: $telefon\n";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.website.com/sent.php");
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
First make headers
$headers = "From: $from\r\nReply-to: $email";
Than fix calling of mail function to be
mail ($to, $subject, $body, $headers)
Didn't tried it from times when it was PHP 4 but it will probably work as you expected...
Addition:
I just checked on php.net... go to this url http://php.net/manual/en/function.mail.php and check "Example #2 Sending mail with extra headers."
Created a php form with a success message that appears just below the Submit button upon successful mission. After adding some additional code in the php to create an email confirmation, I'm now noticing that a number "1" has been inserted in the line after my success message - see below:
Any ideas on how to make that number 1 go away? Code below:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: Page Name';
$to = 'email#mysite.com';
$subject = 'Service Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Company: $company\n
Message:\n $message";
// Confirmation email.
$conf_subject = 'Your recent inquiry';
$conf_sender = 'MY SITE <no-reply#mysite.com>';
$msg = $_POST['name'] . ",\n\nThank you for your recent inquiry. A member of
our team will respond to your message as soon as possible.\n\nThanks,\n\nMy
Company Team";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $phone != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks for your inquiry, we will get back to you as soon
as we can!</p>';
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender
));
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!</p>';
}
}
?>
Thanks everyone!
This line:
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender));
You are echoing out the result of your call to the mail function. Since the mail was successfully handed over to the server it returns true. When you echo out a boolean true it gets converted to an integer which is 1. That's why you see that in your code.
Remove the echo to remove the 1 from being displayed in your output.
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));
This code is causing the echo, its echoing 1 as the mail() function is returning true. I'm not sure why you're echoing the mail function here any way, just remove the echo and all is good.
That is because of this code:
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));
You "echo" the result of the mail function. "1" is equal to "true" on mail sending success.
I made a PHP Contact Form using this tutorial and it works great, but I've encountered one potential security risk / inconvenience. Each email I receive comes from my admin login name.
I added $headers as this thread instructed, but to no avail.
My Current PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myClientsEmail#gmail.com';
$subject = 'Estimate Contact Form';
$headers = "From: $email\r\n"; /* I added this */
$headers .= "Reply-To: $email\r\n"; /* and this */
$body = "From: $name\n Phone: $phone\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
What exactly am I missing? Any help is greatly appreciated. Thank you!
Your mail() function call has an extra parameter it looks like. The correct mail() call should be:
if (mail($to, $subject,$body,$headers)) {
....
}
So just remove the $from portion and it should be good.
I have a rather simple HTML/PHP form that just needs to send the data to my email. The email I'm using is not using the same domain as the website.
I've been stuck on this for hours now and I cant seem to find the solution. Could someone take a look?
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email"];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
} else {
echo 'Something went wrong, go back and try again!';
}
}
?>
Assuming that your PHP code file is sendMail.php
let this be form.html
<form name="sendMail" id="sendMail" action="sendMail.php" method="post">
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<input type="text" name="message" />
<input type="text" name="gender" />
<input type="text" name="user_name" />
<input type="text" name="email" />
<input type="submit" name="submit" />
</form>
Basically, your PHP code should work according to the form above. Of course you may want to change gender field to radiogroup/dropdown and/or message to textarea.
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
}
else {
echo 'Something went wrong, go back and try again!';
}
}
?>
There is one point to correct that $email = $_POST['email"]; should be $email = $_POST['email'];.
And you should ensure that PHP mail settings have to be set properly. I suggest you to use PHPMailer which is so simple and runs smoothly with too few configuration.
Just write isset() in first if condition.
try above code again.
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email"];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
} else {
echo 'Something went wrong, go back and try again!';
}
}
The PHP
The fourth parameter of the mail() function should be a list of additional email headers. You're passing it a user name. You should have something like this:
$email = $_POST['email'];
$email = preg_replace('/[[:cntrl:]]/', '', $email);
$headers = "From: $email\r\n";
...
mail ($to, $subject, $body, $headers);
The second line filters out control characters from the email address. Without this, a malicious user could insert newline characters to add their own email headers, such as CC: headers to send unsolicited spam.
Mixing domains
Also, you might not be allowed to send email with a From: address from a different domain name than your web site (or mail server). You could contact the administrator of your server/web site and ask what your options are.
Some things to consider:
You could try to use a local From: address and leave the actual (external) email address in the Reply-To: header. Eg:
$headers = "From: Me#mydomain.example\r\n";
$headers .= "Reply-To: Someone#otherdomain.example\r\n";
Some email clients might not respect the Reply-To: header, though.
If the address in the From: header is not the actual sender of the email, you should specify the real sender in a Sender: header. Eg:
$headers = "From: Someone#otherdomain.example\r\n";
$headers .= "Sender: Me#mydomain.example\r\n";
You may need to specify the From: address in an additional parameter to the Mail Transfer Agent:
mail ($to, $subject, $body, $headers, "-f '$email'");
Sending email from a different domain name may count against you by SPAM filters. You may need an SPF record to ensure that your emails go through.
You might be better off using a full-featured email class, such as PHPMailer rather than the crude mail() function.
See also
Documentation
RFC 5322 - Internet Message Format: 3.6.2. Originator Fields
The PHP mail() function
SPF - Sender Policy Framework
Stack Overflow
Should I use the Reply-To header when sending emails as a service to others?
How do you make sure email you send programmatically is not automatically marked as spam?
Potential issues using member's "from" address and the "sender" header
Hello I am fairly new to PHP and do not know a lot at the moment. I have modified a contact form an have come into some problems regarding the mail going straight to junk.
I assume this is for the reason that (unknown sender) keeps displaying in the email header. I would appreciate it if someone could help me correct this. The following is the code that I have implemented into the website:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Wirral PT Enquiry';
$to = 'joebloggs#hotmail.com';
$subject = 'Wirral PT Enquiry';
$human = $_POST['human'];
$headers = "enquiry#wirralpt.co.uk";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '2') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p> 1+1=2!! </p>';
}
} else {
echo '<p>You need to fill everything!!</p>';
}
}
?>
$from = 'From: Wirral PT Enquiry'; should contain the 'from' email address, not just the name:
$from = 'From: Wirral PT Enquiry <enquiry#wirralpt.co.uk>';
Try that?
try using
$headers = "Reply To :enquiry#wirralpt.co.uk";
Might work for you
also,
$headers = "From :enquiry#wirralpt.co.uk";
try both of these with you relevant email IDs
Change your headers to this:
$headers = 'From: enquiry#wirralpt.co.uk' . "\r\n" .
'Reply-To: enquiry#wirralpt.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
and your mail should look this this:
mail ($to, $subject, $body, $headers)
This error can also be caused if while using SMTP settings for PhpMailer, $mail->IsSMTP(); is missed