PHP email sent successfully but not received by Gmail - php

I have some code that has worked for me in the past, but isn't anymore, for some reason I don't understand. I'm pretty sure the problem isn't in the code itself, since 1) I haven't changed anything, and 2) it's worked for me in the past. I am using XAMPP and PHP.
My webpage allows a user to fill out a contact form and send an email through PHP. It seems to execute successfully, since it takes the user to "thankyou.html" as a result. To troubleshoot the issue, I have 1) cleared my browser's cache and 2) checked my spam folder in Gmail.
But I still can't find my email, and don't know why. I'm not familiar with how SMTP works; if the problem has to do with that, then I would appreciate an explanation.
Here is the code:
<?php
$field_first_name = $_POST['cf_first_name'];
$field_last_name = $_POST['cf_last_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$field_reference = $_POST['cf_reference'];
$mail_to = 'myself#gmail.com';
$subject = 'Message from a website visitor '.$field_first_name.' '.$field_last_name;
$body_message = 'From: '.$field_first_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message."\n";
$body_message .= 'Reference: '.$field_reference;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
//alert('Thank you for the message. We will contact you shortly.');
window.location = 'thankyou.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to someemail#gmail.com');
window.location = 'home.php';
</script>
<?php
}
?>
Any input is much appreciated!

At least AOL automatically drops all emails sent by PHP's internal mail (I don't know how they detect it, it simply will not show up in the inbox).
A solution is to use a email library like PHPMailer and send your emails via a real SMTP server.
Edit: Does your local machine have a dynamic or fixed IP address? Almost every serious SMTP server does reject emails sent by dynamic IP addresses.

I am using this: (in my website and not in XAMPP)
$from = "mail#mail.de";
$to = "mail#mail.de";
$subject = "subject";
$mailtext = "blablabla";
mail($to, $subject, $mailtext, "From: $from ");
And it's working!
But I tried it with XAMPP as well and nothing happens :(
So I don't know how to use mail() with XAMPP.

As of Nov 2022
Anyone sending emails to Gmail accounts, including personal accounts ending in #gmail.com or googlemail.com, and Google Workspace accounts for work or school must set up either SPF or DKIM to avoid Gmail blocking or marking your emails as spam.
https://support.google.com/a/answer/33786

Related

auto reply not working only for gmail accounts using php mail()

I have a contact form on my website which sends an email to my account and an auto-response to the users who fills the form. I could able to send an auto-reply to non-Gmail accounts but not to Gmail accounts, it's not even sent to spam. I want to know is anything missing in the code, or any settings have to be changed, let me know
code is working fine with non-Gmail accounts
<?php
$email_to = 'mailme#example.com'; //your email
$business = 'company name.,'; //business name
//$topic = $_POST['topic'];
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$autoResponse = true; //set to false if you don't want to send an auto reply
$autoResponseSubject = "Demo Contact Form";
$autoResponseMessage = "Hi, thank you for contacting us, we will get back to you soon.";
$autoResponseHeaders = "From: $business <$email_to>\r\n";
$autoResponseHeaders .= "Reply-To: $business <$email_to>\r\n";
$headers = "From: $name <$email_from>\r\n";
$headers .= "Reply-To: $name <$email_from>\r\n";
if(#mail($email_to,$subject, $message, $headers)){
if($autoResponse === true){
mail($email_from, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
}
echo '1';
} else {
echo '0';
}
?>
I am not getting any errors.
Google, Microsoft, and the like, only accept email from mail servers that fulfill a number of requirements. These requirements are changing over time. This has mainly to do with preventing spam.
Things start with SPF, which is rather simple, but the normal site providing documentation has been down since feb 2019. Have a look at Wikipedia instead.
The next thing is DKIM. Without it mail certainly won't been accepted by GMail.
Then there is also DMARC.
After all of this there is still no guarantee that your mail will be accepted. Your IP could be blacklisted.
As you can probably guess by now, running your own mail server is a lot of work. I've stopped doing it years ago. I now use a third party service for this.

Sendmail failing to send to some domains

I've got a small site set up that allows people to send a prewritten email to their representatives, by putting in their own email and their representatives email.
It's set up using sendmail, and works for sending to gmail, a personal email I have, and various other domains. However, it fails to send to the one domain that I need it to send to.
I get the following fault
<www-data#localhost.localdomain>: Sender address rejected: Domain not found
I've been looking through god knows how many things and I can't seem to figure it out seeing as it's working for everything else.
Hopefully someone can explain it. Cheers!
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_recipient = $_POST['cf_recipient'];
$subject = 'Message Regarding Cuts in the Mental Health Budget';
$body_message = 'hello';
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$headers .= 'Return-Path: '.$field_email;
$mail_status = mail($field_recipient, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We hope this will get the TDs in gear.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, make sure all boxes have been filled and try again.');
window.location = 'index.html';
</script>
<?php
}
?>
You have failed to set "envelope sender" email address used in MAIL FROM: command in SMTP sessions. It can be set by passing -f command line option to sendmail. See additional_parameters in mail documentation

PHP - Sending email from form with mail function

I have been looking at this for hours now, and I can't figure out why this won't work. I'm trying to send an email using the mail function. For some reason this page works when hosted by iPage, but not by Godaddy. What is the reason for this?
The PHP:
<?php
// Run code if button pressed
if (isset($_POST['submit'])) {
// Makes sure all fields are filled
if (!$_POST['name'] | !$_POST['email'] | !$_POST['message'] ) {
?><script>alert('You forgot to fill in a field');window.location = "http://example.com/contact.php";</script>
<?php
exit;
}
// From the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
$to = 'email#gmail.com';
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
?><script>alert('Thanks! I will try to get back to you as soon as possible.');window.location = "http://example.com/contact.php";</script><?php
}
?>
You need to look at your if condition you need || to do a OR
<?php
// Run code if button pressed
if (isset($_POST['submit'])) {
// Makes sure all fields are filled
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ) {
?>
<script>alert('You forgot to fill in a field');
window.location = "http://example.com/contact.php";
</script>
<?php
exit;
}
You are setting From email id $email from user Input, that means its not configured at your server, add a Reply-To header to $email
$headers = "From: youremail#yourdomain.com \r\n";
$headers .= 'Reply-To: '.$email. "\r\n" .
$headers .= "Content-type: text/html\r\n";
Final note, I would suggest you to use Swift Mailer or PHP Mailer to make it simple
For people having trouble sending email on GoDaddy's servers, here's whats going on:
To prevent spam, GoDaddy's servers refuse to send your mail if your using a certain domain (see below for the list). So to solve the specific problem here, I had to change the domain I was using, so $to = 'email#gmail'; is now $to = 'email#something-else.com';
Here is GoDaddy's explanation and the list of blacklisted domains:
Forms are popular on websites; they let customers share their information with you, whether it’s for a newsletter or to register an account. Often times, these forms email a confirmation of the visitor’s submission to to you or the visitor submitting the information. Did you know that all email, even if it’s sent from a hosting account, must have information entered in the From value, though?
You can make that From value a particular email address, too. This lets you create a professional-looking email for your customers or something that’s easy to categorize for yourself.
However, we have to be careful with what users can specify as their From value to combat spamming attempts. Here’s a list of email address domain names we don’t let our customers use as the From value of their forms:
gmail.com
aol.com
aim.com
yahoo.com
hotmail.com
live.com
msn.com
If your email form uses one of these domain names for its From email address, our server will not send the email.

Sending Email Twice from PHP

I'm doing this simple testing on creating form where I want the form to send email twice:
to my email address, to notify me if somebody has reached me
to the sender's email address to notify him/her that the contact has been sent to me.
problem is, I could send the first email, but I couldn't send the second one. I thought this should be easy. I may miss a line or two here. Here's the code:
<?php
$field_email = $_POST['email'];
$mail_to = 'myemail#mydomain.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message .= 'E-mail: '.$field_email."\n";
$headers = 'From: '.$field_email."\r\n";
$headersTo = 'From: '.$mail_to."\r\n";
$body_messageTo = 'Thank you for your interest';
$subjectTo = 'Thank You from Gaban';
$mail_status = mail($mail_to, $subject, $body_message, $headers);
$autoreply = mail($field_email, $subjectTo, $body_messageTo, $headersTo);
?>
the $field_email should take data directly from the "email" form from the HTML code.
Unless $_POST['email'] contains an invalid email address, I can't see anything wrong with it. I'd say the problem is most likely to be found in the mailserver processing the mailbox where the second email is sent to.

PHP mail() function does not work on web-host

I'm having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it works there.
I think it has to do with the web-hosting company itself. Do any of you know what I need to do in order to make it work? Is there something I need to tell them to install? I think they're running on Apache.
Thanks,
Amit
For clarification purposes, here is the mail-script.
<?php
$to = 'my#email.com';
$subject = 'Contact from your website';
$message =
'Below are details from the Contact Us Form ' . "\n\n" .
'Name: ' . $_REQUEST['name'] . "\n\n" .
'Telephone Number: ' . $_REQUEST['phone'] . "\n\n" .
'E-mail address: ' . $_REQUEST['email'] . "\n\n" .
'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
//SPAM CHECK
$str = $_REQUEST['spam'];
$strE = $_REQUEST['email'];
if( $str != "10" || $strE == "")
{
echo "<div align='center' style='color:red'>One or more of the form fields were incorrect, you will be redirected to the contact page within 3 seconds.</div>";
?><meta http-equiv="refresh" content="3;URL=http://engineercreativity.com/samples/biz/contact"><!-- EDIT THIS -->
<?php
} else {
mail ($to, $subject, $message, $headers);
?>
<meta http-equiv="refresh" content="0;URL=http://engineercreativity.com/thankyou.html"> <!-- EDIT THIS AS WELL -->
<!--
<div class="text" align="center" style="text-align: center; color: green;">
<br/>
Thank you for contacting us!
<br/>
The message was succesfully sent!
</div>
-->
<?php
}
?>
If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)
I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)
Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.
Posting full headers also can help legitimate messages pass spam tests.
$headers = "Return-path: <sendingemail#test.com>\n";
$headers .= "Reply-to: <sendingemail#test.com>"."\n";
$headers .= "Content-Type: text/html; charset=windows-1252\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: <sendingemail#test.com>\n";
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Organization: My Organization\r\n";
$headers .= "\n\n";
Write a really simple script, like
<?php
mail('your_mail#example.com', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';
Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25 is sufficient).
If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?
Whatever the solution, check your mail()'s output.
Most common solution
Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).
Alternative
The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).
mail() function of php, will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user#serverhostname.tld and not someone#example.com like you had told the mail function to do.
If you use SMTP and view the original the email is actually sent from someone#example.com
You can download SMTP class from:
https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

Categories