-3

Below is code of my email sending script, one year this same code worked fine but now it is not sending any email, please help me to solve this error out

Message: mail(): Multiple or malformed newlines found in additional_header

$encoded_content = chunk_split( base64_encode( $variables['pdfFileContents'] ));

$uid = md5( time() );

$fileName = str_replace( " ", "_", $variables['emailContains'] );
$fileName .= ".pdf";

////// attachment header ///////////////////////////////
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: cashxcash.com<{$sender}>";
$headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "--{$uid}";
$headers[] = "Content-type:text/plain; charset=iso-8859-1"; // Set message content type
$headers[] = "Content-Transfer-Encoding: 7bit";
$headers[] = ""; // Dump message
$headers[] = "--{$uid}";
$headers[] = "Content-Type: application/octet-stream; name=\"{$fileName}\""; // Set content type and file name
$headers[] = "Content-Transfer-Encoding: base64"; // Set file encoding base
$headers[] = "Content-Disposition: attachment; filename=\"{$fileName}\""; // Set file Disposition
$headers[] = $encoded_content; // Dump file
$headers[] = "--{$uid}--"; //End boundary

return mail(
    $reciever,
    "{$variables['emailContains']} Report from Cashxcash!",
    "", 
    implode("\r\n", $headers)
);
NDM
  • 6,731
  • 3
  • 39
  • 52
Jakyy B
  • 55
  • 1
  • 10

1 Answers1

0

You must be sure that the MTA (mail transfer agent) on your host is not QMAIL as it is known to replace LF with CRLF. Then you have to move all your headers into the 4-th argument of mail() and the body of your email into the 3-rd argument of mail(). Something like this:

$encoded_content = chunk_split( base64_encode( $variables['pdfFileContents'] ));

$uid = md5( time() );

$fileName = str_replace( " ", "_", $variables['emailContains'] );
$fileName .= ".pdf";

$headers = array();
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'From: cashxcash.com<'.$sender.'>';
$headers[] = 'Content-Type: multipart/mixed; boundary="'.$uid.'"';

$body = 'This is a multi-part message in MIME format.

--'.$uid.'
Content-type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

--'.$uid.'
Content-Type: application/octet-stream; name="'.$fileName.'"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="'.$fileName.'"

';
$body.= $encoded_content; // Dump file
$len = strlen($encoded_content);
if(!($encoded_content{$len-2} == "\r" AND $encoded_content{$len-1} == "\n")) $body.= "\r\n";
$body.= '--'.$uid.'--'; //End boundary

return mail($reciever,$variables['emailContains'].' Report from Cashxcash!',$body, implode("\r\n", $headers) );
IVO GELOV
  • 13,496
  • 1
  • 17
  • 26
  • It may solve the problem but when i added your script it gave me some other errors like 'uid' and 'encode-content' and some other variable are not defined, can you tell me bit more about this because around one year ago this same script was sending emails and but i'm getting error, is this happen by any PHP upgrade or any other reason? – Jakyy B Aug 03 '16 at 17:19
  • Please read this article, as it is exactly your case - http://mindofdes.blogspot.bg/2015/07/error-with-php-mail-multiple-or.html – IVO GELOV Aug 04 '16 at 12:45