This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm having a classic PHP form submission error. I tested the code earlier and it seemed to be working now when testing it again, the email never arrives.
This is the HTML (I changed the site/email address for security reasons of course)
<!-- Contact Form -->
<form method="post" action="http://example.net/assets/mail/mail.php">
<div class="row 50%">
<div class="6u 12u(mobile)"><input type="text" name="name" placeholder="Name" />
</div>
<div class="6u 12u(mobile)"><input type="email" name="email" placeholder="Email" />
</div>
</div>
<div class="row 50%">
<div class="12u"><textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>
This is the PHP file:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "es#example.net";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
The PHP file is located at the same place the HTML links to: http://example.net/assets/mail/mail.php and the email itself is working fine (use it daily). When I test the form out I get the echo/message saying thank you so is definitely finding the PHP file however, I simply don't get anything on my inbox.
Is such a simple code yet I'm really confused and unsure what is going on.
Any help is truly appreciate it.
The email could be blocked in the server's mail queue or blocked as spam by the email host. For linux based system, the email is usually logged in the /var/logs directory and there should be an equivalent on windows based servers. From here you should be able to see whether or not the server sends the email out into the world.
The code appears to be correct.
The real problem is the use of the mail function of php. If you look at the official documentation, you can see how it is necessary that the parameters passed to the function must meet special requirements.
In addition, there are some difference using email() with Windows or Linux.
My advice is to not use direct mail() but a library to send as PHPMailer https://github.com/PHPMailer/PHPMailer
Your code is right! It seems like a mail server problem. Try to change the recipient mail address and check if your server is in black list: http://mxtoolbox.com/blacklists.aspx
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm fairly new to php and I'm still learning the basics. I created a simple "contact us" form that should send the data to an email address. However, I'm not receiving the email. The "Thank you" message displays correctly, but the email is never sent.
Unfortunately my knowledge in php is slim so I'm having difficulty trouble shooting this one. I did successfully code a simpler form with only one field. That one is sending correctly. Since this form has multiple fields, it seems to be throwing something off.
<?php
if($_POST["submit"]) {
$recipient="myemail#gmail.com";
$subject="Contact Form";
$sender=$_POST["sender"];
$senderEmail=$_POST["senderEmail"];
$message=$_POST["message"];
$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
$thankYou="<p>Thank you! Your message has been sent.</p>";}
?>
<?=$thankYou ?>
<form method="post" action="company.php">
<input class="contact" type="text" name="sender"
placeholder="First Name" size="25">
<input class="contact" type="text" name="last"
placeholder="Last Name" size="25">
<input class="contact" type="text" name="title"
placeholder="Title" size="25">
<input class="contact" type="text" name="business"
placeholder="Business" size="25">
<input class="contact" type="email" name="senderEmail"
placeholder="Email" size="25">
<input class="contact" type="text" name="phone"
placeholder="phone" size="25">
<textarea class="contact" name="message"
placeholder="How can we help you?" rows="4" cols="56"></textarea>
<input class="blu-btn" type="submit" name="submit"
value="Send Message">
</form>
It's not throwing any errors, I'm just not receiving the email. I've checked spam, tried a separate email, I'm missing something. Thank you so much for your help!
You should check first if your server is truly sending the mail, changing your code a bit:
if($_POST["submit"])
{
$recipient="myemail#gmail.com";
$subject="Contact Form";
$sender=$_POST["sender"];
$senderEmail=$_POST["senderEmail"];
$message=$_POST["message"];
$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
if (mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"))
{
echo "<p>Thank you! Your message has been sent.</p>";
}
else
{
print_r(error_get_last()["message"]);
}
}
Take a look into the PHP Documentation for mail() function
Return Values
Returns TRUE if the mail was successfully accepted for
delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Probably the server itself isn't properly configured to send email.
Is a shared hosting? Or something like?
Kind regards!
The environment where you run this makes all the difference. Mail may not be configured correctly or, some spam filter blocked it. In this case, nothing in your code can make a difference.
If you have control of the server, and you know how, you could check the mail program. If you are limited to only writing code, you have other options. You can use SMTP and send email through an external service. Then you can use mailtrap.io to capture the outbound email. This is a good way to go for debugging and making sure that your code is right.
You can use SwiftMailer if you want to try an alternative mail client.
I'm trying to send email from my domain. Mail is delivering properly. But it showing some message mentioning that the delivered message is spam. Please help me to overcome that problem. This is the message I got Be careful with this message
This may be a spoofed message. The message claims to have been sent
from your account, but Gmail couldn’t verify the actual source. Avoid
clicking links or replying with sensitive information, unless you are
sure you actually sent this message. (No need to reset your password,
the real sender does not actually have access to your account!)
<?php
if(isset($_POST['submit'])) {
$email_to = "info#maxwell.com";
$email_subject = "Your email subject line";
$name = $_POST['name'];
$message = $_POST['message'];
$email_from = $_POST['mail'];
$email_message = "Form details below.\n\n";
$email_message .= "Name: ".$name."\n";
$email_message .= "Email: ".$email_from."\n";
$email_message .= "message: ".$message."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($email_to, $email_subject, $email_message, $headers)){
?>
<script>
window.location.href="contact.php?success";
</script>
<?php
// print("Thank you for contacting us. We will be in touch with you very soon.");
}
else{
?>
<script>
window.location.href="contact.php?fail";
</script>
<?php
}
// print("fail");
}
?>
<form method="post" >
<div class="form">
<div class="col-md-6 col-sm-12 col-xs-12 form-group">
<input type="text" class="form-control" name="name" placeholder="Your Name">
</div>
<div class="col-md-6 col-sm-12 col-xs-12 form-group">
<input type="email" class="form-control" name="mail" placeholder="E-mail Address">
</div>
<div class="col-xs-12 col-md-12 form-group">
<textarea name="message" placeholder="Message..."></textarea>
<!-- <input type="submit" value="SEND MESSAGE" class="btn-black bounce-top"> -->
</div>
<div class="col-xs-12 col-md-12 form-group">
<input type="submit" class="btn-black bounce-top" name="submit" value="SEND MESSAGE">
</div>
</div>
</form>
In order to avoid such situation. You can follow the following suggestions:
A Simple Implementation Example
<?php
mail("recipient#recipient.com", "Message", "A simple message.", "From: The Sender <sender#sender.com>");
?>
4 Ways To Make Your PHP mail() Emails Less Spammy
Use Headers
The Message Sender Domain and Server Domain Should Match
Be Sure to Properly Use the Content-type Attribute
Verify That Your Server Is Not Blacklisted
Detailed Explanation:
1. Use Headers
<?php
$headers .= "Reply-To: The Sender <sender#sender.com>\r\n";
$headers .= "Return-Path: The Sender <sender#sender.com>\r\n";
$headers .= "From: The Sender <senter#sender.com>\r\n";
?>
Be sure to replace the fourth parameter with the $headers variable as shown below.
<?php
mail("recipient#recipient.com", "Message", "A simple message.", $headers);
?>
2. The Message Sender Domain and Server Domain Should Match
Spammers are notorious for sending emails from one server and trying to make the recipient believe that it came from somewhere else. So if you are sending an email from example#example.com, it is a good idea the the script reside on example.com.
3. Be Sure to Properly Use the Content-type Attribute
The Content-type attribute enables a message sender to say whether or not an email is plain text or html, or whether it has attachments. Obviously, the easiest to use content type is text/plain. You just add your text as shown in the simple example, and you are done. But when you use the other content types, additional pieces might be expected. For example, with the text/html content type, an html body tag is expected. Not having this tag could result in your email being marked as spam.
4. Verify That Your Server Is Not Blacklisted
When a server is blacklisted, it means that that server has identified as one that has been sending a lot of spam. This results in recipient mail servers rejecting or filtering any mail that is received from that server.
So if your mail is not being received it is a good idea to verify that your server has not been blacklisted. This goes for both shared and dedicated servers. In a shared environment, it is common for other users on the server to be sending out spam. And in a dedicated environment, spammers may have found a way to exploit a vulnerability in a server or contact form to send out spam. So it is easy for either type of server to be blacklisted.
If you want a solution sure to not get marked as spam, look into Amazon's SES service. You will likely never exceed free tier pricing, and with a bit of configuration, you'll hit inboxes at much higher rates.
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 4 years ago.
I have set up a contact form which is to send email to anyone from WordPress page. It's sending email but all are going to SPAM folder. How to prevent it to go to Spam folder but go to Inbox?
however, I gave it try from another wordpress site, all emails going to inbox perfectly, and I've found a difference that is all email going to inbox has one extra line in details that is-
mailed-by: p6plcpnl0235.prod.phx3.secureserver.net
But the emails going to the SPAM folder doesn't have this above line, Is it can be a cause? If so how to enable this secureserver.net
Can anyone please help me?
Here is what I am using in page-
<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email'])) {
//Email information
$admin_email = "admin#mail.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];
//send email
mail($email, $subject, $comment, "From:" .$admin_email);
//Email response
header('Location: /email-sent');
}
//if "email" variable is not filled out, display the form
else {
?>
<div class="container send-email-form-wrapper">
<div class="row">
<div class="send-email-form">
<form method="post">
<li> Email: <input name="email" type="text" /> </li>
<li> Subject: <input name="subject" type="text" /></li>
<li> Message: <textarea name="comment" rows="5"></textarea></li>
<li><input type="submit" value="Submit" /></li>
</form>
</div>
</div>
</div>
<?php
}
?>
This is more about your hosters internal sendmail mail setup that is used by the mail() function. If possible, try to use an tested SMTP mailer like Swiftmailer or PHPMailer instead. That way you could work around the sendmail smarthost configuration issue.
I have a contact form that I wrote in the html document and this then is executed by an external php file. How do I validate it? All tutorials that I've looked at have shown the validation and the html form in the actual php file and so how can my validation be accomplished?
HTML5:
<form id="form-area" action="email-processor.php" method="POST">
<div id="name-area"><p>Name (required)</p><input class="form-input" type="text" name="name"></div>
<div id="email-area"><p>Email (required)</p> <input class="form-input" type="text" name="email"></div>
<div id="phone-area"><p>Telephone</p> <input class="form-input" type="text" name="phone"></div>
<div id="msg-area"><p>Message</p><textarea id="msg-input" name="message" rows="6" cols="25"></textarea><br /></div>
<input id="sendbtn" type="submit" value="Send">
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone Number: $phone \n \n Message: \n \n$message";
$recipient = "sampleemail#hotmail.com"
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
You need to put required behind the input fields. If you want to make an email required as the standard format xxx#xxx.xxx instead simple text use type="email". For the telephone number you can use type="number" to allow numbers only, otherwise simply use text.
NEW HTML
<form id="form-area" action="email-processor.php" method="POST">
<div id="name-area"><p>Name (required)</p><input type="text" class="form-input" type="text" name="name" required></div>
<div id="email-area"><p>Email (required)</p> <input class="form-input" type="email" name="email" required></div>
<div id="phone-area"><p>Telephone</p> <input class="form-input" type="number" name="phone" required></div>
<div id="msg-area"><p>Message</p><textarea id="msg-input" name="message" rows="6" cols="25" required></textarea><br /></div>
<input id="sendbtn" type="submit" value="Send">
</form>
As has already been pointed out, for client-side validation, you can use the required attribute, which will trigger appearance changes in most web browsers.
However, you MUST do server-side validation as well. Failure to do so will result in vulnerabilities in your application code. For example, your mail() call currently allows unsanitized input for the additional_headers parameter. That means that malicious actors can easily inject whatever headers they want to - e.g. injecting an additional To: or CC: header can turn your server into an open mail relay (i.e. that's bad). Attackers are ALWAYS looking for incorrect usage of the PHP mail() function such as demonstrated by your code.
Because of the poor design of the PHP mail() function, my view is that no one should directly call it. The function is actually much more complicated to use correctly since it is only a basic layer over sendmail and, without significant effort, ignores all sorts of IETF RFCs that govern e-mail. You should use a library such as Ultimate E-mail Toolkit, PHP Mailer, etc. that offer a nicer layer over mail() and/or SMTP to do the actual sending of the e-mail and avoid turning your server into an open relay.
The server is the final authority on what is and is not allowed. For this reason, I use CubicleSoft FlexForms, which aids me in generating HTML forms and processing user input server-side. How you handle things server-side is far more critical than client-side validation, which can and will be ignored by malicious users. You can't control what a client will send and there are plenty of malicious actors out there. So you have to make the unfortunate assumption that all users will attack your software. You should always start with server-side validation and then add client-side validation afterwards.
In addition, your code won't work as you expect. Most mail servers are configured to deny spoofing attempts. You can't assume that you can send e-mail From: someone whose e-mail servers you don't control. The messaging will bounce back and if you send enough spoofed mail messages your server will eventually be added to a global blacklist (via DNSRBL) and denied sending e-mail to anyone else. You can only send "From" an address that you have control over AND have set up things such as a SPF record or DMARC for. Sending e-mail is hard thanks to spammers and the lack of direction by the Internet Engineering Task Force (IETF) to solve the problem.
You can, however, use the Reply-To: header with any sanitized e-mail address that you want to use. Most e-mail clients respect the Reply-To header and will use it instead of the From header when it exists.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I've done my research on this but can't seem to get it to work.
I'm looking to add a contact form to my website that sends an email directly to me. I've watched videos and used code I've found online but nothing works. I even temporarily disabled my website and uploaded just a blank contact form (code below) and a php file (code below) with my only results being that the echo command at the end of the PHP file DOES show up. The email, though, still does not send.
What am I missing? Thank you!
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Temp</title>
</head>
<body>
<form method="post" action="send.php" name="contact_form">
<p>
<input name="name" type="text" />
</p>
<p>
<input name="email" type="text" />
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#myemail.co";
$subject = "Contact Form Submission";
mail ($to, $subject, $message, "From: " . $name);
echo "Your message has been sent. You can expect to hear from us soon.";
?>
I'm sure this is a duplicate, but I just wanted to mention a couple of things to check.
Does your mail() call return true? I would check that. It's possible that your PHP installation is not set up correctly to send mail. There are countless posts on how to check and configure that, which I would suggest reviewing if you haven't already (here's one, for example).
Depending on where you're hosting this, your host's configurations may restrict any outgoing mail that is not from the domain you're hosting. (I've had this problem myself on shared hosts.) Here you're setting the "from" header as the name of the person submitting the form (which would look something like: "From: John Doe"). This may be a problem either on the sending or receiving end of the email (either side rejecting it because it doesn't come from an email address, or a valid email address, etc). Try setting the "from" value to an email address valid on your host (e.g., "myname#mydomain.com"). Then, just include the person's name and email address in the $message of the email.
Hope that helps.