PHP's mail() function doesn't work properly - php

I am using PHP's mail() function to send automated e-mails from my website and while testing, I noticed that when I sent mail from website by Gmail, then e-mails sent by php mailer are generating the following warning on the recipients end: This message may not have been sent by: example#gmail.com Learn more Report phishing. But when I use other emails (like yahoo, outlook), then I got no emails in my $contact_email. Please help me to solve this problem.
** I don't want to use Google app. and my domain email address.
** I just want If someone contact (fill the form ), then I just got email in my website.
Here the code:
<?php
global $_REQUEST;
$response = array('error'=>'');
$user_name = substr($_REQUEST['user_name'], 0, 20);
$user_email = substr($_REQUEST['user_email'], 0, 40);
$user_msg = $_REQUEST['user_msg'];
$contact_email = 'contact.arefin#gmail.com';
if (trim($contact_email)!='') {
$subj = 'Message from Official Website';
$msg = "Name: $user_name
E-mail: $user_email
Message: $user_msg";
$head = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To: $user_email\n"
. "To: $contact_email\n"
. "From: $user_email\n";
if (!#mail($contact_email, $subj, $msg, $head)) {
$response['error'] = 'Error send message!';
}
} else
$response['error'] = 'Error send message!';
echo json_encode($response);
die();
?>

There is no way around that and Google does it to prevent sapm. Try sending from 'noreply#yourdomain.com' with the 'reply-to' set as your gmail. youdomain should be the domain that the website you are sending from sits on.
On another note, I would also recommend using a library that wraps the mail function, take a look at SwiftMailer.

Related

Fasthosts shared platform PHP mail from web page issue -F

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);
}
?>

Is it possible for wp_mail to sometimes fail?

I have a simple function that uses wp_mail, like:
$headers = "From: $myName <$myEmail>" . "\r\n";
wp_mail( $myToEmail, $mySubject, $myMessage, $myHeaders );
When I put this code onto a site, it seems there are sometimes when the email does not send, but I do not see any way to replicate the issue and it generally works.
Where are potential points of failure?
Is it possible the email is failing on the server side occasionally?
You need to use WP_mail() function to your contact form.You can use this way
see this is example:
headers = "From: $myName <$myEmail>" . "\r\n";
//Here put your Validation and send mail
$sent = wp_mail( $myToEmail, $mySubject, $myMessage, $myHeaders );
if($sent) {
}//message sent!
else {
}//message wasn't sent

Emails aren't sent in Wordpress with headers

I'm running a new WordPress website on LEMP on Debian 7, sendmail installed.
I have a custom theme installed which has contact form.
Contact form says email sent but email actually not delivered. I have tried to remove $headers from email, email delivered. Simple php mail function working properly. Same custom theme sending and delivering email through form at Shared host.
Here is part of custom form:
What is wrong with this? can anyone point me out what is wrong or how I can start sending emails?
if(!isset($hasError) && ($correct == true)) {
$admin = get_bloginfo('admin_email');
$portfolio = get_bloginfo('name');
$portfolio_url = home_url();
$emailTo = $admin;
$pro = get_the_title();
$subject = "You have an message for $pro";
$body = "Hello,\r\n\r\nYou've received an message from ".$name.", for ".$pro." fun name.\r\n\r\nHere are message details:\r\n--------------------------------\r\nBuyer Name: ".$name."\r\nEmail: ".$email."\r\nMessage: ".$message." ".$CurrencyCode."\r\n\r\nMessage: ".$message."\r\n\r\n---------\r\n".$portfolio."\r\n".$portfolio_url;
$headers = "From: ".$portfolio." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
Thank you!
wp_mail has an open bug related to the Reply-To header. Try removing that header only and check if the email is sent correctly. If that's the case, you will have to create a plugin to wrap it or just avoid setting the Reply-To header.

PHP mail causing phishing warning on gmail

I am using PHPMailer to send automated e-mails from my website and while testing, I noticed that when I sent mail from website by Gmail, then e-mails sent by php mailer are generating the following warning on the recipients end:
This message may not have been sent by: example#gmail.com Learn more Report phishing.
But when I use other emails (like yahoo, outlook), then I got no emails in my $contact_email. Please help me to solve this problem.
PHP Mailer code:
<?php
global $_REQUEST;
$response = array('error'=>'');
$user_name = substr($_REQUEST['user_name'], 0, 20);
$user_email = substr($_REQUEST['user_email'], 0, 40);
$user_msg = $_REQUEST['user_msg'];
$contact_email = 'contact.arefin#gmail.com';
if (trim($contact_email)!='') {
$subj = 'Message from Official Website';
$msg = "Name: $user_name
E-mail: $user_email
Message: $user_msg";
$head = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To: $user_email\n"
. "To: $contact_email\n"
. "From: $user_email\n";
if (!#mail($contact_email, $subj, $msg, $head)) {
$response['error'] = 'Error send message!';
}
} else
$response['error'] = 'Error send message!';
echo json_encode($response);
die();
?>
When you send bulk emails and especially when you mock the sender address, you need to use best practices that may reduce how many servers block you as spammer.
Three things which I think you should do are:
1) Use appropriate mail headers
Add the following to your code - a notice that this is a bulk sender, and the OPT-OUT email address:
.= "X-mailer: YOUR_SITE_DOMAIN Server" . "\r\n"; // this will identify the real sender
.= "Precedence: bulk" . "\r\n"; // this will say it is bulk sender
.= "List-Unsubscribe:info#YOUR_SITE_DOMAIN\r\n"; // this will reveal the OPT-OUT address
Read more about it here
2) Make sure your server domain has a reverse DNS record. This will tell the recipient's server that your domain is REALLY hosted on your server.
3) Publish SPF record with your domain. You can read more about it here, and google it for other big handlers (like Yahoo).
In addition to those, make sure you are adding a footer with a "one click" OPT-OUT removal option and explanation note that this message is sent on behalf, and who is the original sender.
Cheers
You can either set up google apps for your site and get a Username#yourwebsite.com gmail account (more info: http://www.google.com/enterprise/apps/business/), or You will need to set up an e-mail address on your current server that is Username#yourwebsite.com and use that as the $mail->from address.
Your E-Mail recipients are receiving the message because you are telling google to send an e-mail from your server, and then you are telling them that the mail is coming from gmail, which it isn't, it's coming from your personal server. Since the from address and your server address don't match, they flag it as spam. This is googles way of preventing spam, to them it would be the same if you put $mail->from(YOURMOM#LOL.com). The e-mail would still send, but your domain name does not match the # address.
Try this code , In your code i found some mistake which i have solved here.
global $_REQUEST;
$response = array('error'=>'');
$user_name = substr($_REQUEST['user_name'], 0, 20);
$user_email = substr($_REQUEST['user_email'], 0, 40);
$user_msg = $_REQUEST['user_msg'];
$contact_email = 'contact.arefin#gmail.com';
if (trim($contact_email)!='') {
$subj = 'Message from Official Website';
$msg = "Name: $user_name
E-mail: $user_email
Message: $user_msg";
$head = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-Mailer: PHP/" . phpversion() . "\n"
. "Reply-To: $user_email\n"
. "To: $contact_email\n"
. "From: $user_email\n";
if (!#mail($contact_email, $subj, $msg, $head)) {
$response['error'] = 'Error send message!';
}else{
$response['error'] = 'success!';
}
} else {
$response['error'] = 'Error send message!';
}
echo json_encode($response); die();

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