PHP Mail Not Working

Hello,
This code of mine was working well for a long time. It queries a database & sends a winzip file located on the server as an email attachment to users in the database. Suddenly it has stopped working. The message I get is Sorry but the email could not be sent. Please go back and try again! I’m very sure that the winzip files are on my server & the file names are correct. I’ve also tested the query ($query1) directly on PHP MyAdmin & the query works fine.

I don’t know why. Could you please help to debug the mystery please? Thanks.

<?php
include("/path/to/my/server/public_html/db_connect_wordpress.php");

$today = date('Y-m-d');
$first_day = date('Y-m-01');
$thismonth = date('M_Y');
$thisday = date('D');
$lastmonth = date('M_Y', strtotime('- 28 days'));

if ($thisday == 'Mon') $Text_Box_1 = "commercial_$thismonth.zip";
if ($thisday == 'Tue') $Text_Box_1 = "condo_$thismonth.zip";
if ($thisday == 'Wed') $Text_Box_1 = "land_$thismonth.zip";
if ($thisday == 'Thu') $Text_Box_1 = "new_prop_$thismonth.zip";
if ($thisday == 'Fri') $Text_Box_1 = "house_$thismonth.zip";
if ($thisday == 'Sat') $Text_Box_1 = "commercial_$thismonth.zip";
if ($thisday == 'Sun') $Text_Box_1 = "condo_$thismonth.zip";
if ($today == $first_day) $Text_Box_1 = "everything_$lastmonth.zip";

//read the file_emailer folder code begins below
if ($dir = opendir("/path/to/my/server/public_html/file_emailer/"))
           {
               while (($file = readdir($dir)) !== false) 
               {
                   if($file != ".." && $file != ".")
                   {
                       $filelist[] = $file;
                   }
               } 
               closedir($dir);
           }
//read the file_emailer folder code ends above

$query1=("SELECT wp_usermeta.user_id, wp_users.user_email FROM wp_usermeta INNER JOIN wp_users ON wp_users.ID = wp_usermeta.user_id WHERE wp_usermeta.user_id IN (SELECT user_id FROM wp_usermeta WHERE meta_key =  'rcp_status' AND meta_value =  'active') AND (meta_key =  'rcp_subscription_level') AND (meta_value > 1) AND (user_id != '17') "); 

$result1=mysql_query($query1); 
$num1=mysql_num_rows($result1);

if ($num1 > 0) {

while ($data = mysql_fetch_array($result1)){
$email_to = $data["user_email"];

$fileatt = "/path/to/my/server/public_html/file_emailer/$Text_Box_1";               
$fileatt_type = "application/octet-stream"; // File Type  
$fileatt_name = "$Text_Box_1"; // Filename that will be used for the file as the attachment

$email_from = "excel_file@mysite.com"; // Who the email is from
$email_subject = "Listing Database $today $Text_Box_1"; // The Subject of the email  

$semi_rand = md5(time());  
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers = "From: ".$email_from;     
$headers .= "\nMIME-Version: 1.0\n" .  
            "Content-Type: multipart/mixed;\n" .  
            " boundary=\"{$mime_boundary}\"";
			  
$email_message = "Good Day :-)<br><br>The $Text_Box_1 file for $today is attached.<br><br>You can download these files yourself at http://www.mysite.com/<br><br>Site Admin";
$email_message .= "This is a multi-part message in MIME format.\n\n" .  
                "--{$mime_boundary}\n" .  
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" .  
               "Content-Transfer-Encoding: 7bit\n\n" .  
$email_message . "\n\n";  

$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file);  


$data = chunk_split(base64_encode($data));  

$email_message .= "--{$mime_boundary}\n" .  
                  "Content-Type: {$fileatt_type};\n" .  
                  " name=\"{$fileatt_name}\"\n" .  
                  //"Content-Disposition: attachment;\n" .  
                  //" filename=\"{$fileatt_name}\"\n" .  
                  "Content-Transfer-Encoding: base64\n\n" .  
                 $data . "\n\n" .  
                  "--{$mime_boundary}\n";  
unset($data); 
unset($file); 
unset($fileatt); 
unset($fileatt_type); 
unset($fileatt_name);

$ok = mail("$email_to,test@hotmail.com", "$email_subject", "$email_message", "$headers","-ftest@yahoo.com");
//$ok = mail('test@yahoo.com', 'My Subject', 'hello how are you?');

if($ok)
		{  
echo "$Text_Box_1 was successfully sent to $email_to"; 
echo "<br>"; 
		}
else
		{  
die("Sorry but the email could not be sent. Please go back and try again!");  
		}

}
			  }
?>

Hi transfield,

If the code was working OK before then the first thing I’d do is to check with your hosting company. It could be that they’ve changed something that is now preventing the email from being sent.

1 Like

You were right, fretburner.

My webhost had turned off the php mail() function without informing me. I am now re-writing the script using SMTP authentication…

Thank you once again for your advice. I appreciate it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.