Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail() function not sending on new server

Having some issues with a PHP script that sends emails (code below). Basically, it populates a vCard file with contact information stored in an sql db and attaches it to an email using the php mail() function.

I had this working perfectly on a shared hosting server a few days ago... but I recently migrated everything over to a VPS and it magically stopped working. Mail() continues to return true on send, but the actual email never arrives in my inbox.

//sendemail: emails a vCard when passed an email address and name
function sendemail($address, $scanreduser)
{   
    include('../dbconnect.php');
    $info = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE usernum='". $usernum ."' LIMIT 1 "));
    $vcard_content  = "BEGIN:VCARD\r";
    $vcard_content .= "VERSION:3.0\r";
    $vcard_content .= "N:".$info[lname].";". $info[fname] .";;;\r";
    $vcard_content .= "FN:".$info[fname]." ". $info[lname] ."\r";
    $vcard_content .= "ORG:".$info[company].";\r";
    $vcard_content .= "TITLE:".$info[title]."\r"; 
    $vcard_content .= "EMAIL;type=INTERNET;type=WORK;type=pref:".$info[email]."\r";
    $vcard_content .= "TEL;type=WORK;type=pref:".$info[phone]."\r";
    $vcard_content .= "item2.URL;type=pref:".$info[website]."\r";
    $vcard_content .= "item2.X-ABLabel:_$!<HomePage>!\$\_\r";
    $vcard_content .= "X-ABShowAs:COMPANY\r";
    $vcard_content .= "END:VCARD";

    $email_subject = "Your vCard from " . $info[fname] . " " . $info[lname];
    $fileatt_type = "application/octet-stream"; // File Type
    $fileatt_name = $info[fname] ."_". $info[lname] .".vcf";

    $headers = "From: [email protected]";
    $today = date("l, F j, Y, g:i a");

    $message = "<br />Simply open the attached vCard file to view/download the information<br />";
    $message .= $today." PST<br /><br />";
    $message .= $info[name]."<br />";

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

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

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $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" .
    $message . "\n\n";
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    echo @mail($address, $email_subject, $message, $headers);
    echo $address;


}
like image 761
Robert Avatar asked May 17 '26 00:05

Robert


1 Answers

It is probably sitting in a queue somewhere on your server. Check your php.ini.

Just to narrow it down a bit, try a really simple mail command...

mail("[email protected]", "Subject", "Testing 3, 2, 1...");

Also in your script, if there were a problem, you wouldn't see it. The @ in front disables displaying of errors. Since it is returning true though, I bet it's working just fine. The mail() function doesn't verify that the e-mail was successfully sent... only that it was handed off to something else. Your sendmail on the box may not be functioning. Or for Windows, your SMTP server may not be set correctly.

There is also always the case of the e-mail sitting in a spam filter somewhere.

like image 112
Brad Avatar answered May 19 '26 15:05

Brad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!