Jump to content

Bad parameters to mail() function


syrup890

Recommended Posts

Hello all, 

 

I'm pretty new to PHP and need some help. I am getting a "Bad parameters to mail() function" when trying to send an email with a pdf attachment. I know there are other threads (most quite old) about this, but I can't seem to get it to work for my situation. I have narrowed it down to the PHP_EOL that I'm using (from my understanding, it pretty much does a CRLF). But when I remove this from my code, I can't get the pdf to attach to the email...I just get the encoded jumble in the body of the message. I also tried doing just a "\n" and tried to shorten the header by separating out the message from the header. 

 

Also, I'm having this problem only when hosting this on godaddy's servers. I was testing this same code on bluehost with no issues. I'm using PHP 5.2 in both cases. 

 

Here's the relevant code snippet:

 

        $attachment = chunk_split(base64_encode($pdfDocument));

	/*
	some variables needed for creating the email with attachment
	*/
	$email_subject = "".$first_name. " ".$last_name."";
	$message = "Please See Attachment.";
	// a random hash will be necessary to send mixed content
	$separator = md5(time());
	// carriage return type (we use a PHP end of line constant)
	$eol = PHP_EOL;
	// attachment name
	$filename = "".$property." - ".$first_name." ".$last_name.".pdf";

	// main header (multipart mandatory)
	$headers  = "From: ".$property." - V.I.P.".$eol;
	$headers .= "MIME-Version: 1.0".$eol; 
	$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
	$headers .= "Content-Transfer-Encoding: 7bit".$eol;
	$headers .= "This is a MIME encoded message.".$eol.$eol;
	// message
	$headers.= "--".$separator.$eol;
	$headers.= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
	$headers.= "Content-Transfer-Encoding: 8bit".$eol.$eol;
	$headers.= $message.$eol.$eol;
	// attachment
	$headers .= "--".$separator.$eol;
	$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
	$headers .= "Content-Transfer-Encoding: base64".$eol;
	$headers .= "Content-Disposition: attachment".$eol.$eol;
	$headers .= $attachment.$eol.$eol;
	$headers .= "--".$separator."--";		

	// sending the email
	mail($webmaster_email, $email_subject, "", $headers);

 

Any help would be appreciated! Thanks in advance.

 

Link to comment
Share on other sites

jazzman1, that's just how I saw it being done. I moved the attachment to the the body of the email and now I can get the email to send without errors, but I can't open/get to the attachment at all. I just get a blank email (nothing in the actual body of the message) and it shows there's an attachment, but its like nothing's there. I'm using FPDF to generate the pdf form.

 

I guess my frustration is that my original code worked on bluehost (I believe it's a microsoft product), but I was just using it for testing. I'm actually hosting the site on godaddy and it's not working. I'll take a look at swiftMailer if needed, but I'm really trying to get my code to work... since I know in theory it should.

 

 Here's the modified code. 

$attachment = chunk_split(base64_encode($pdfDocument));

/*
some variables needed for creating the email with attachment
*/
$email_subject = "".$first_name. " ".$last_name."";
$message = "Please See Attachment.";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "".$property." - ".$first_name." ".$last_name.".pdf";
// main header (multipart mandatory)
$headers  = "From: ".$property." - V.I.P.".$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
// body
$body = "--".$separator."--".$eol;
$body .= "Content-Type: multipart/alternative; boundary=\"".$separator."\"".$eol;
$body .= "This is a MIME encoded message.".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= $message.$eol.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol.$eol;
$body .= "--".$separator."--"; 

// sending the email
mail($webmaster_email, $email_subject, $body, $headers);
header( "Location: $thankyou_page" );
Edited by syrup890
Link to comment
Share on other sites

Take this one.... tested:

 

<?php

$file = 'jazmzan_cv.pdf';

$file_size = filesize($file);

$handle = fopen($file, "r");

$content = fread($handle, $file_size);

fclose($handle);

$content = base64_encode($content);

$filename = "jazzmanCV.pdf";

$email_subject = 'Test attachment';

$from = 'jazzman';

$from_mail = 'jazzman@localdomain.localhost';

$replyto = 'jazman@gmail.com';

$subject = "Please See Attachment.";

$mailto = 'tuparov86@gmail.com';

$message = 'Test message from jazzman';

// a random hash will be necessary to send mixed content
$uid = md5(uniqid(time()));

$header = "From: " . $from . " <" . $from_mail . ">\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
if (mail($mailto, $subject, "", $header)) {
    echo "mail send ... OK"; // redirect the user
} else {
    
    echo "mail send ... ERROR!";
}

Result:

 

 

Test message from jazzman
pdf.gif jazzmanCV.pdf
43K   View   Download  

 

Link to comment
Share on other sites

The problem was that you wasn't get the size of the file.

 

With chunk_split() works too,

 

$file = 'jazzman_cv.pdf';

$file_size = filesize($file);

$handle = fopen($file, "r");

$content = fread($handle, $file_size);

fclose($handle);

$content = chunk_split(base64_encode($content));


continue.................
Edited by jazzman1
Link to comment
Share on other sites

Thanks jazzman1! I did a little bit of testing with your code and here's what I found...

 

- with godaddy, only lines 34-37 should be in the header. the rest of the items in the header should be passed in the mail function through the body. 

- I don't think the issue was with the file size. I am generating a pdf using fpdf right before creating and sending the email. I don't need to save the pdf before sending it, so I tried just using the string version of the file (a fpdf option) and did not need to get the size of it for it to work. I can see why you need to read the pdf using the file size if it was an already existing pdf though.

- I did have to use chunk_split() for the pdf. otherwise, I was not able to open the file due to decoding issues once received.

 

I think the real issue was with the PHP_EOL function I was using instead of the CRLFs. Either way, THANK YOU!

Link to comment
Share on other sites

I think the real issue was with the PHP_EOL function I was using instead of the CRLFs. Either way, THANK YOU!

 

No, the first time when I ran the script into my local server I've got a pdf file with 0 size.

 

Anyway I've made one more test this time from my server to godaddy (linux server).

 

Works fine, here is it:

 

<?php   
    $file = 'jazzman_cv.pdf';
     
    $file_size = filesize($file);
     
    $handle = fopen($file, "r");
     
    $content = fread($handle, $file_size);
     
    fclose($handle);
     
    $content = base64_encode($content);
     
    $filename = "jazzmanCV.pdf";
     
    $email_subject = 'Test attachment';
     
    $from = 'jazzman';
     
    $from_mail = 'jazzman@localdomain.localhost';
     
    $replyto = 'jazman@gmail.com';
     
    $subject = "Please See Attachment.";
     
    $mailto = 'tuparov86@gmail.com';
     
    $message = 'Test message from jazzman';
     
    // a random hash will be necessary to send mixed content
    $uid = md5(uniqid(time()));
     
    $header = "From: " . $from . " <" . $from_mail . "> \n";
    $header .= "Reply-To: " . $replyto . "\n";
    $header .= "MIME-Version: 1.0 \n";
    $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\n";
    $header .= "This is a multi-part message in MIME format.\n";
    $header .= "--" . $uid . "\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1 \n";
    $header .= "Content-Transfer-Encoding: 7bit \n";
    $header .= $message . " \n";
    $header .= "--" . $uid . "\n";
    $header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\n";
    $header .= "Content-Disposition: attachment; filename=\"" . $filename . "\" \n";
    $header .= $content . " \n";
    $header .= "--" . $uid . "--";
    
    if (mail($mailto, $subject, "", $header)) {
    echo "mail send ... OK"; // redirect the user
    } else {
    echo "mail send ... ERROR!";
    }

 

 

Don't use \r on an linux server or use PHP_EOL instead of it.

Edited by jazzman1
Link to comment
Share on other sites

"\r\n" worked on godaddy for me, but I understand the dangers of using it. I was able to switch to PHP_EOL with the correct formatting. Here's the code. Thank you, jazzman, again for all your help!

 

// this line calls another php method that uses fpdf to generate the pdf and saves sends it back 
// as a string to be saved in $content
$content = &createPDF($first_name, $last_name, $address, $apt_num, $zip, $phone, $phone_type, $email, $contact_date, $contact_time, $ampm, $movein_date, $staff_name, $property, $instructions); 
        $content = chunk_split(base64_encode($content));

        $filename = $property." - ".$first_name." ".$last_name.".pdf";
        $email_subject = $first_name." ".$last_name;
        $from = $property." - V.I.P.";
        $from_mail = $property." - V.I.P.";
        $subject = $first_name. " ".$last_name;
        $mailto = "email@domain.com";
        $message = 'Please see attachment.';
        $eol = PHP_EOL;

        // a random hash will be necessary to send mixed content
        $uid = md5(uniqid(time()));
 
        // headers
        $headers = "From: " . $from . " <" . $from_mail . ">" .$eol;
        $headers .= "Reply-To: " . $replyto . $eol;
        $headers .= "MIME-Version: 1.0" .$eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"" .$eol.$eol;

        //body  
        $body = "This is a multi-part message in MIME format.".$eol;
        $body .= "--" . $uid . $eol;
        $body .= "Content-type:text/plain; charset=iso-8859-1".$eol;
        $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
        $body .= $message .$eol.$eol;
        $body .= "--" . $uid .$eol;
        $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"".$eol; 
        $body .= "Content-Transfer-Encoding: base64".$eol;
        $body .= "Content-Disposition: attachment; filename=\"" . $filename . "\"".$eol.$eol;
        $body .= $content .$eol.$eol;
        $body .= "--" . $uid . "--";

        if (mail($mailto, $subject, $body, $headers)) {
            header( "Location: $thankyou_page" );
        } else {
            echo "The form did not send correctly. Please try again.";
        } 
Edited by syrup890
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.