Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Cannot understand why I am getting this error - both to & from email addresses are valid (I use them daily) so cannot figure out how this is happening - any help would be appreciated.

NOTE: This is working in production but is throwing errors in dev. I have stricter config in dev. NOTE: I am testing locally on PC using smtp4dev

$to = 'recipient <myemail@mydomain.com.au>';
$cc = 'copy <myemail@mydomain.com.au>';
$from = 'sender <myemail@mydomain.com.au>';
$filename = 'Invoice#'.$order_id.'.pdf';
$message = file_get_contents(ROOT_DIR.'admin/include/email-body.html');
$content = chunk_split(base64_encode($pdf_file));
$uid = md5(uniqid(time()));

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'To: '. $to . "\r\n";
$headers .= 'Cc: '. $cc . "\r\n";
$headers .= 'From: '. $from . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $message."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--";

if (mail($to, $subject, $message, $headers)) {
  print "SUCCESS";
} else {
  print "FALIED";
}

Here is the result if I print the variables on the mail() line:

mail(recipient <myemail@mydomain.com.au>, Company - Invoice#12451, "",
MIME-Version: 1.0 
To: recipient <myemail@mydomain.com.au> 
Cc: copy <myemail@mydomain.com.au> 
From: sender <myemail@mydomain.com.au> 
Content-Type: multipart/mixed;
boundary="2c88ff549e67c83e7a6e3df0bffe9dc9"

This is a multi-part message in MIME format.
--2c88ff549e67c83e7a6e3df0bffe9dc9 Content-type:text/html;
charset=iso-8859-1 Content-Transfer-Encoding: 7bit

---> html message body stripped <---


--2c88ff549e67c83e7a6e3df0bffe9dc9 Content-Type: application/pdf;
name="Invoice#12451.pdf" Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Invoice#12451.pdf"

--> pdf attachment stripped <--


--2c88ff549e67c83e7a6e3df0bffe9dc9--)
share|improve this question
    
This might get better answers on StackOverflow, but shouldn't $message contain everything from This is a multi-part message in MIME format. onwards? You've got the whole message, including the body, in the headers. –  SmallClanger Aug 30 '11 at 8:52
    
thanks - I've got it on stack overflow but because the message was SMTP related I thought someone in here might know common causes –  php-b-grader Aug 30 '11 at 10:23

1 Answer 1

Your $to must be only the mail address. The SMTP dialog expects a mail address in angle brackets and you are feeding it the contents of the To: header. The same for $from too.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.