0

trying to send mail using native mail() but its showing error in the header part

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

It is showing an error in the last line of the code which is:

$mail = mail($email, $subject, $message, $headers, $returnpath);

code:

    $email = $id;
    $subject = $mydetails_name.' had sent Contact Details';
    $message = $html;
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: CloudBigTechnology' . "\r\n";
    $semi_rand = md5(time());  
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
    // Headers for attachment  
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";  
    // Multipart boundary  
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  
    if(!empty($file_element_name['name'])) { 
        $filesCount = count($explodedString); 

        for($x=0;$x<count($explodedString);$x++) {
            $file_namex = basename($file_element_name['name'][$x]); 
            $file_sizex = filesize($file_element_name['tmp_name'][$x]);
            $message .= "--{$mime_boundary}\n"; 
            $fp = @fopen($file_element_name['tmp_name'][$x], "rb"); 
            $datam =  @fread($fp, $file_sizex); 
            @fclose($fp); 
            $datam = chunk_split(base64_encode($datam)); 
            $message .= "Content-Type: application/octet-stream; name=\"".$file_namex."\"\n" .  
                "Content-Description: ".$file_namex."\n" . 
                "Content-Disposition: attachment;\n" . " filename=\"".$file_namex."\"; size=".$file_sizex.";\n" .  
                "Content-Transfer-Encoding: base64\n\n" . $datam . "\n\n"; 
        }
    }

    $message .= "--{$mime_boundary}--"; 
    $returnpath = "-f" . $email; 
    $mail = mail($email, $subject, $message, $headers, $returnpath);  
     

How to fix it?

Andrew Halil
  • 1,195
  • 15
  • 15
  • 21
  • 1
    Please format your code properly. It's hard to read when the indention is all over the place. – M. Eriksson Jan 11 '22 at 07:17
  • Btw, you've tagged this with CodeIgniter... then why don't you use their [email class](https://codeigniter.com/user_guide/libraries/email.html) instead? – M. Eriksson Jan 11 '22 at 07:24
  • what is the solution in the above code @Eriksson –  Jan 11 '22 at 07:25
  • 1
    My suggestion would be to use the tools you have to your disposal, like Codeigniters Email class instead. – M. Eriksson Jan 11 '22 at 07:26
  • Kindly any one guide me how to fix the issue in the above code –  Jan 11 '22 at 07:29
  • Since you don't seem to want to use Codeigniter's email class as suggested I suggest you head over to the PHP [mail](https://www.php.net/manual/en/function.mail.php) manual and read the topic on the `additional_headers` parameter. Pay extra attention on "_Multiple extra headers should be separated with a CRLF (\r\n)._" – brombeer Jan 11 '22 at 07:31
  • Does this answer your question? [Error with PHP mail(): Multiple or malformed newlines found in additional\_header](https://stackoverflow.com/questions/30887610/error-with-php-mail-multiple-or-malformed-newlines-found-in-additional-header) – ficuscr Jan 11 '22 at 07:49

0 Answers0