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";
}
}
Related
Like the title says, mail returns FALSE to I call error_get_last() and it returns a NULL. The email is never received. The thing is, nearly identical code in another page on this site has no problem sending an HTML email, so the server must be okay.
I searched and found this question "PHP mail() returns false but no error is logged", but the answer doesn't seem to apply in my case.
My php code is:
<?php
$MSG_Info = array(
'', // 0 = Sender's name
'', // 1 = Sender's email address
'', // 2 = Subject of the message
'');// 3 = The message itself
$MSG_SendTo ='XXXX#yahoo.com';
function GetMessageText()
{
global $MSG_Info;
return
"<blockquote>".
"Name: ".$MSG_Info[0]."<br />".
"Email: ".$MSG_Info[1]."<br />".
"Subject: ".$MSG_Info[2]."<br />".
"Message: ".wordwrap($MSG_Info[3], 70, "\r\n")."<br />".
"</blockquote>";
}
function GetHTMLHeader()
{
return
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'.
'<html>'.
'<head>'.
'<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
'<title>Marguerite Gabe Braun</title>'.
'</head>'.
'<body>'.
'<p><font size="+2" color="#0000AA">'.
'<i>A Message from Marguerite’s Web Site</font></i></p>';
}
function SendMessageEmail($Dest)
{
global $MSG_Info, $MSG_SendTo;
$from = $MSG_SendTo;
$subject = $MSG_Info[2];
$message = GetHTMLHeader();
switch ($Dest)
{
case 1:
$to = $MSG_SendTo;
$replyto = $MSG_Info[1];
$message .= "<p>".$MSG_Info[0]." has sent you a message from your web site.</p>\n";
break;
case 2:
$to = $MSG_Info[1];
$replyto = $MSG_SendTo;
$message .= "<p>Here is the message you sent to Marguerite Gabe Braun from her web site.</p>\n";
break;
default:
$to = $MSG_SendTo;
$replyto = $MSG_SendTo;
$message .= "<p>".$MSG_Info[0]." has sent you a message from your web site.</p>\n";
}
$message .= "<p>".GetMessageText()."</p>\n"."</body></html>";
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: " . $from . PHP_EOL;
$headers .= "Reply-To: " . $replyto . PHP_EOL;
// Send the email.
error_reporting(E_ALL);
$MSG_Result = mail($to, $subject, $message, $headers);
if ($MSG_Result)
{
$MSG_Error = NULL;
}
else
{
$MSG_Error = error_get_last();
}
return $MSG_Error;
}
?>
I've inserted all kinds of code (not shown here) to verify that I'm not screwing up the email addresses, that mail() is indeed returning FALSE, and error_get_last() is returning NULL, that the correct information is being passed in POST, etc.
Thanks in advance.
Edit.
Brute force debugging, commenting out one line at a time, narrowed it down the the line in the header where I specify "From:". If I comment out that line the mail sends and it says it is from the webmaster at my site.
So I test more and it looks like the word "From:", or any line that starts with that, is removed from my message. So strange. See the code below. The code for $message for the Reply-to lines, the line with three "froms" in it, and the simple $from line show in the message in my email. All others are missing. Or maybe this is clearer. Message lines 2, 5, 6, 7, 8, and 10 show in my message. The others, which all start with "From:", do not. It's the dangest thing.
<?php
$message .= "From: ".$from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "From: "."\r\n";
$message .= "FROM: "."\r\n";
$message .= "From, from, FROM: "."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= $from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "From: ".$from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "<p>".GetMessageText()."</p>\n"."</body></html>";
// This commented-out code is used on another page and with works correctly.
// $headers = "MIME-Version: 1.0\r\n";
// $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
// $headers .= "From: ".$from."\r\n";
// $headers .= "Reply-To: ".$replyto."\r\n";
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
// $headers .= "From: " . $from . PHP_EOL;
$headers .= "Reply-To: " . $replyto . PHP_EOL;
// Send the email.
$MSG_Result = mail($to, $subject, $message, $headers);
if ($MSG_Result)
{
$MSG_Error = NULL;
}
else
{
$MSG_Error = error_get_last();
if ($MSG_Error == NULL) {$MSG_Error[0] = "Unknown mail() error.";}
}
return $MSG_Error;
}
?>
I want to send a email from php mail function. This is my code
<?php
$to = "modiv2301#gmail.com";
$subject = "HTML email";
$message = "
<h1>this is msg</h1>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <modiv2301#gmail.com>' . "\r\n";
$success=mail($to,$subject,$message,$headers);
if (!$success) {
$errorMessage = error_get_last()['message'];
print_r($errorMessage);
}else{
echo "Success";
$errorMessage = error_get_last()['message'];
print_r($errorMessage);
}
?>
when i run this code its showing me Success but i am not Receiving any email
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";
}
}
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";
I am using this script to send notificaitons to users friends. the problem is that all recepieints get to see who else got this email. How do i tweak the code so the emails still get sent to all but they can't see who else got it?
Code:
$sql = "SELECT STRAIGHT_JOIN DISTINCT email from
friend_email_ids WHERE my_id='$id'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
$emails = array();
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
array_push($emails, $row['email']);
{
$email = $row['email'];
print("");
}
}
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
mail(implode(",", $emails), "Subject: $subject",
$message, "$headers" );
echo "";
Just use BBC for all recipients:
Bcc: recipients get a copy of the email, but their email address is
automatically deleted at delivery. Nobody except you and the Bcc:
recipient will know that they got a copy, and their email address will
not be exposed.
-> http://email.about.com/od/emailmanagementtips/qt/How_to_Send_an_Email_to_Undisclosed_Recipients.htm
Use the additional_headers fields to add a BCC* address . See the manual
From the manual page:
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
the "birthdaycheck" email is hidden.
*(Blind Carbon Copy)
In you script it would become something like this:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
////////////pay attention here
$headers .= "BCC: ".implode(",", $emails)."\r\n";
$to = "youremail#domain.com"; //the mail in the "TO", visible to all. there has to be 1.
////////////////////////
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
mail($to, "Subject: $subject",
$message, "$headers" );
echo "";
Put the actual sending of messages in the loop. That way you will send the e-mail to each recipient individually instead of all at once.
From PHP.net you'll find that the Bcc feature of mail() is what you need to use.
Like zoy (for multiple peeps):
$headers .= 'Bcc: someone#example.com,someone2#example.com,someone3#example.com,someone4#example.com,' . "\r\n";
Happy Haxin!
_wryteowl