PHP mail function not sending email to Gmail account? - php

I am working on a WordPress plugin in which I send email using the PHP mail function, but there is a problem with the mail function. It sends emails to my non-Gmail account, but it's doesn't send emails to my Gmail account. I am using following code:
function send_mail()
{
global $wpdb;
$to = 'mymail#gmail.com';
$subject = 'Hello';
$name='my name';
$from="name#mydomain.com";
$message = "
<html>
<head>
<title>my title</title>
</head>
<body>
<div>
<tt> ".Hii How Are you."</tt>
</div>
</body>
</html>";
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1\r\n";
$header .= "From: ".$name."<".$from.">\r\n";
mail($to, $subject, $message, $header);
}
Is there something wrong with my code, or is there some issue with the mail function? If any alternate method is available to send email, please give me the link.

Check if adding fifth variable works for you... here is my code for sending emails.
if( mail( $recipient, $subject, $message, $headers, "-f noreply#mydomain.com"))
return "success";

Check the spam folder, It might be there. Its a server issue, it hapened to me also many times. Gmail blocks mails or sends to spam from some servers due to some reasons. Ask your server provider to check why mails are not going to gmail inbox.

use the code sample as below, email address in <> and that last paramter worked for me.
$headers = 'From: <test#test.com>' . "\r\n" .
'Reply-To: <test#test.com>';
mail('<myEmail#gmail.com>', 'the subject', 'the message', $headers,
'-fwebmaster#example.com');
?>
It is the answer provided in PHP mail() function will not send to gmail but will send to my non-gmail account by ARH3, which i have tried and tested

Related

PHP Sending Email Problems

I have a few questions regarding sending email in PHP. I've been on google for the last few days and I'm still having trouble getting this to fully work.
My first question is how do I change the "From" section of my email? I have "To: support#mydomain.com" in my "from" section:
I'd like to have just the proper name of my domain (eg: "testingstuff.com" -> "Testing Stuff"). How could I achieve this?
Once I actually open the email everything in it is fine and correct, including the From email address being "support#mydomain.com".
Also my mail won't send to gmail addresses. It shows up in my mail queue and my logs say it is sent but it never is received on my gmail. Do I have to take extra steps for Google to accept my email? If so what are those? Do other major mail provides require the same steps, or are they different steps?
This is my code so far:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("sendmail_from", "support#mydomain.com");
class email {
public static function send($to, $subject, $message) {
$headers = "From: Testing Stuff <support#mydomaincom>\r\n";
$headers .= "Reply-To: support#mydomain.com\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
}
}
?>
Usage:
require_once("../mail.php");
email::send("support#mydomaincom", "testing email subject", "testing email body");
Am I doing anything wrong in my code?
You need to check if the email is sent properly checking the mail() result, in this way:
$result = mail($to, $subject, $message, $headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
this is inside your static function,
Also check your spam folder if the mail function return "success".

Why is this PHP script failing to send HTML mail?

I have this piece of test code to send an HTML email. For the longest time, it was sending, but not sending in HTML format. And now, it's not sending at all.
I've been comparing with code on the PHP manual, and with similar questions on this site, and I can't see anywhere that the code could be wrong. It's driving me crazy.
Why is this code not sending, and why is it not sending in HTML?
$to = "myemail#mysite.com";
$subject = "Confirmation code for registration";
$message = "<html>
<head>";
$message .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
$message .= "<title>TITLE</title>
</head>
<body>";
$message .= "Thank you for registering! \r\n";
$message .= "
</body>
</html>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf-8\r\n";
$headers .= "From: My Site <registration#mysite.com>r\n";
$mailresult = mail($to, $subject, $message, $headers);
echo "mail sent! ";
echo $mailresult;
$mailresult comes back as 1, which I assume means success, but no mail is being recieved at the mail account specified.
According to php.net:
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.
Have you configured a mail server to send the mail?
Your code worked on my server perfectly but I received the email as Spam - so check your spam. And do your research to check if your server is configured properly.
Try doing
mail($to, $subject, $message, $headers);
without $mailresult

PHP Mail Don't Show "To" in "To" Header

I want to make an email forwarder similar to cPanel's, where I have a database of email addresses, and where they should forward to, and I set a catch-all to pipe to my script.
I have completed this script, however, I would like to make the "To:" field in the header show the address it was sent to, rather than who is was being delivered to. For example, the email was sent to user001#mydomain.com, and the script forwards it to me#gmail.com. How can I make PHP send mail to me#gmail.com, but still show user001#mydomain.com in the headers, like cPanel does?
You can use the headers of the mail function:
$to = 'me#gmail.com';
$subject = 'Testing';
$message = 'This is a test';
$headers .= 'To: User001 <user001#mydomain.com>, User002 <user002#mydomain.com>' . "\r\n";
$headers .= 'From: My Email Script <me#gmail.com>' . "\r\n";
mail($to, $subject, $message, $headers);

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

mail script in php

I am hosting my web application(pnpmkt.com) from GODADDY.com they have given me some email account like info#pnpmkt.com. I want to send welcome message to new user's to their mail account in other mail servers like google, yahoo.for example, my mail function is-
<?php
$address = "piysuh#gmail.com";
$Subject = "PNP Solutions";
$body = "Welcome to PNP";
$mailsend = mail("$address", "$Subject", "$body.");
print("$mailsend");
?>
what other configurations are required?Any path name or server name??
Here is a wrapper function I use to send emails (text or html):
// $Sender: Sender email
// $Recipient: Recipient email
// $Subject: Email subject
// $Detail: Plain text or HTML (should include <html> and <body> tags)
// $Type: TEXT or HTML
function sendmail( $Sender, $Recipient, $Subject, $Detail, $Type )
{
switch ( $Type )
{
case "TEXT":
$Header = "From: $Sender\n";
break;
case "HTML":
$Header = "From: $Sender\n";
$Header .= "MIME-Version: 1.0\n";
$Header .= "Content-type: text/html; charset=iso-8859-1\n";
break;
}
return mail( $Recipient, $Subject, $Detail, $Header );
}
Can you clarify whether you are having trouble sending emails using this method? From what I can see, your code is good, and should operate without any problems.
I just tested this code, myself, and it works fine.
It should be noted that the mail() function returns TRUE on success and FALSE on failure so echoing that is not a terribly useful thing to do. (You'll just get "1" if it worked and "0" if it didn't.)
If you are wanting to include more advanced features and facilities in your emails, however, you may want to look at PHPmailer, which is a PHP Class allowing for the sending of HTML Emails, changing various settings like the Sender's email address and name, etc.

Categories