PHP mail() works for Yahoo but not for Gmail - php

This part of my code sends mail to yahoo but doesn't send mail to Gmail. I also referred a lot of related FAQs' and documentation on PHP mail() but those didn't work out for me.
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(!empty($_POST['submit']))
{
$to = $_POST['email'];
$subject = 'Annual Report';
$message = $_POST['template'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: from#example.com" . "\r\n";
$headers .= "Reply-To: replyto#example.com" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$headers .= "X-Priority: 1" . "\r\n";
$retval = mail($to,$subject,$message,$headers);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
else
{
echo "empty";
}
}

Related

Mail function always calling the else part

I have two domain name. For example https://example.com and https://example2.com.
I am sending test mail using mail function.
I am able to send the email from https://example.com and I am getting the success but the same code I am using for https://example2.com and I am not getting the email it's always calling the else part.
Test mail
<?PHP
$sender = 'xxx#xx.com';
$recipient = 'zzz#zz.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
$errorMessage = error_get_last()['message'];
echo $errorMessage;
}
?>
The From header is not valid as the next one is concatenated right after it:
$headers = 'From:' . $sender;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Should be something like:
// add \r\n
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
Try this simple php mail function
$to = $email_id;
$subject = "";
$message = "Your mail Body Content. you also use here HTML Tags for better performence. ";
$headers = 'From: xyz#domainname.com' . "\r\n" .
'Reply-To: xyz#domainname.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo "Message accepted";
}else{
echo "Error: Message not accepted";
}
Check if mail function is enabled on example2.com by using below:
if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
}
If disabled, enable it via php.ini or in cPanel.

Mail getting with HTML tags

My mail function is working fine, delivering mail after filling the form.
But the output in the mail is with HTML tags as shown attached.
Here is my code.
<php if (isset($_REQUEST['email'])) {
$admin_email = "abcdef#gmail.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['salespersonname'];
$location = $_REQUEST['location'];
$noofbags = $_REQUEST['noofbags'];
$print = '<div>Location : $location<br />No of Bags : $noofbags</div>';
$comment = $print;
mail($admin_email, "$subject", $comment, "From:" . $email);
echo "Thank you for contacting us!";
}
?>
How can i get Output like this.
Location : Hyderabad
No of Bags : 50
Use the below Headers to send email with HTML tags:
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Updated Code:
<php if (isset($_REQUEST['email'])) {
$admin_email = "abcdef#gmail.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['salespersonname'];
$location = $_REQUEST['location'];
$noofbags = $_REQUEST['noofbags'];
$print = '<div>Location : $location<br />No of Bags : $noofbags</div>';
$comment = $print;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($admin_email, "$subject", $comment, "From:" . $email, $headers);
echo "Thank you for contacting us!";
}
?>
Hope this will be help.
you have to set header's
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
Add following headers :
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Pass $headers as 4th parameter in mail function.
You need to set the Content-type header in your email message:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and pass into
mail($admin_email, "$subject", $comment, "From:" . $email, $headers);
Your message body also needs to be contained in <html> tags.

postfix send email subject breaks the FROM

I am using postfix to send an email to the user, but the problem is it breaks the words where it finds the space.
Here is the screenshot:
postfix-send-email
PHP code to send an email:
<?php
$subject = "Status Of mail";
$message = "Test Email using Postfix Apache2";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: 'The Travel Worthy' 'pathik#gmail.com"\r\n";
$send = mail('test#yahoo.com', $subject, $message, $headers);
if($send)
{
return 1;
}
else
{
return 0;
}
?>
Try replacing
$headers .= 'From: 'The Travel Worthy' 'pathik#gmail.com"\r\n";
with
$headers .= "From: The Travel Worthy <pathik#gmail.com>\r\n";

Reply to won't work on php form submitting to itself

I can't get the reply to function to work on a contact form that submits to itself.
The BCC is working fine. Any help is appreciated.
$to = 'email#email.com';
$subject = ''.$_POST['emailsubject'].'';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$headers .= 'Reply-To: '.$_POST['email'].'';
'X-Mailer: PHP/' . phpversion();
$headers .= 'Bcc: email#email.com' . "\r\n";
mail($to, $subject, $msg, $headers); $sendMessage = true; unset($_POST); } } ?> <?php if($invalidCaptcha) { ?>
<?php
$from_add = "test#gmail.com";
$to_add = "email#email.com";
$subject = "Test Msg";
$message="test msg.";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent OK";
}
else
{
$msg = "Error sending email!";
}
?>

PHP Mail Function waits and not working with foreach loop

I am trying to send mail to all users that have no created reports in one month, also trying to send mail to this users with foreach loop and with mail function, when i refresh one by one then it sends mail one by one, then it works. i want to send mail to all this users in one time.
$from = "xxx#xxx.com";
$subject = "";
$headers = "";
$inc = 0;
foreach($query->result_array() as $row)
{
$inc++;
$to = $row['us_email'];
$to_date = $row['report_date'];
if($to_date != "0000-00-00")
{
$subject = "Hello! (Reports Are Not Created).";
//begin of HTML message
$message = "1 month above you should create reports.";
}
else if($to_date == "0000-00-00")
{
$subject = "Hello! (Generate Reports).";
//begin of HTML message
$message ="generate reports to get more.";
}
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "To: User <".$to.">, Admin <".$from.">" . "\r\n";
$headers .= "From: Complete Online Marketing <".$from.">" . "\r\n";
$headers .= "Reply-To: Recipient Name <".$from.">";
$headers .= "Cc: [email]".$from."[/email]" . "\r\n";
$headers .= "Bcc: [email]".$from."[/email]" . "\r\n";
// now lets send the email.
if(mail($to, $subject, $message, $headers))
{
echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Success...\n";
$ins = array('report_mail'=>'0');
$this->db->update('se_user', $ins, array('us_id' => $row['us_id']));
}
else
{
echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Fail...\n";
}
}

Categories