Not sure if anyone else has experienced this but i have a simple form that sends out a email.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="phone" id="phone" value="<?php echo $phone; ?>" />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
When submitted i have the following:
if ($_POST) {
$email_to = "myemail#yahoo.com";
$subject = "Contact Form";
$message = "Phone: {$phone}\r\nMessage: {$msg}";
$headers = "From: sendingemail#yahoo.com" . "\r\n";
mail($email_to,$subject,$message, $headers);
}
When the form is submitted the mail function returns true but no email gets sent however when i change the FROM email to anything else outside of yahoo such as something#gmail.com the email comes through. Anyone know how to solve this issue?
Yahoo marked your mail as spam and is probably just ignoring it.
This is probably true for hotmail as well.
Best thing todo is find yourself a good SMTP mail pluging/module (phpMailer for instance) and use the credentials of a legit mail account. This way you are sending mail trough a dedicated mail server and changes are you won't be marked as spam anymore.
Do notice however than when you sent loads of (simular) mails or your script gets hacked and is used for spamming, changes are that your legit mailserver becomes blacklisted or (if you are lucky) blocks your account as beeing unsafe.
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.
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.
i'm trying to send e-mail using the function mail().
But my email is being sent to junk, and it works perfectly on gmail, what am i doing wrong?
<?php
if(isset($_POST['submit'])){
$name = $_POST["name"];
$name .= " ";
$from = $_POST['from'];
$subject = $_POST["subject"];
$message = $_POST["message"];;
$to = $_POST["to"];
mail($to, $subject, $message, "from: $name \n $from \n");
echo $name;
}
?>
<form method="POST">
<input type="text" placeholder="from" name="from" />
<input type="text" placeholder="to" name="to" />
<input type="text" placeholder="name" name="name" />
<input type="text" placeholder="Subject" name="subject" />
<textarea type="text" placeholder="Message" name="message"></textarea>
<input name="submit" type="submit" />
</form>
The problem could be that the domain name in your $from field doesn't match the server that the email is being sent from.
The IP address that you are sending from could also be on the spam blacklist for the email client provider you are using.
There are some other guidelines that can affect how email clients will detect your email as junk, such as whitespace in the header fields, missing Reply-To and Return-Path headers etc.
One of the reasons Hotmail moves your mail to spam, is because you let the user enter the 'from' address. If the domain from which the mail is sent doesn't coincide with the from address in the header, the mail is seen as spam. Some servers reject the mail altogether.
Using PHPMailer or SwiftMailer sure helps with setting the right headers, but you should never send mails from other domains than your own.
Another thing than using different php libraries, there is also the matter of whiletilsts/blacklists which are lists of domains/ips which mail hosting companies use to quickly distingquish spam from proper mail so they sometimes require you to send some kind of email from admin#domain... to the moderator to prove you're not a spam bot, try checking this for hotmail.
You might want to read: http://smallbusiness.chron.com/domain-whitelisted-hotmail-46827.html
use smtp for sending an email then it will not go in junk
http://www.mendoweb.be/blog/php-send-mail-smtp-server-authentication-required/