Asked  7 Months ago    Answers:  5   Viewed   30 times

Can someone help me figure out why this keeps returning false?

$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$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."--";

// send message
if (mail($to, $subject, "", $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}

 Answers

32
$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary="".$separator.""";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset="iso-8859-1"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$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;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}

What I've done:

  • Separated the body from the headers
  • removed extra EOLs put before separators
Wednesday, March 31, 2021
 
radmen
answered 7 Months ago
83

I would suggest looking at phpmailer if you want to do complex email.

Wednesday, March 31, 2021
 
RahulG
answered 7 Months ago
32

Your code looks fairly straightforward and syntactically correct. Is the script returning any error messages?

If you're receiving the message without any issues, then the problem doesn't look to be in your code.

A few things to check:

  • Make sure that the file "matt.txt" is both readable by your webserver and that the path is correct. The path to the file needs to be included in the $mail->AddAttachment() method call and should be relative to the script's location.
  • Verify that your mail server isn't stripping any attachments out due to restrictions and/or try sending a different attachment file type (try a .zip or a .jpg file)
  • If you're running a newer version of phpMailer, you can try catching any exceptions that are thrown (perhaps one that isn't preventing the message from going out, but just preventing the attachment from being included) using the following syntax: (taken from phpMailer Example Code)

    require 'PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer(true);
    try {
    
      $mail->IsSMTP();    // set mailer to use SMTP
      $mail->Host = "SMTP.SErver.com";    
    
      $mail->From = "From@email.com";    
      $mail->FromName = "HCSC";  
      $mail->AddAddress("To@email.com", "Example"); 
      $mail->AddReplyTo("Reply@email.com", "Hcsc"); 
    
      $mail->WordWrap = 50;    
      $mail->IsHTML(false);    
    
      $mail->Subject = "AuthSMTP Test";
      $mail->Body    = "AuthSMTP Test Message!";
      $mail->AddAttachment("matt.txt");
      echo "Message Sent OK<p></p>n";
    
    } catch (phpmailerException $e) {
    
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    
    } catch (Exception $e) {
    
      echo $e->getMessage(); //Boring error messages from anything else!
    
    }
    
Saturday, May 29, 2021
 
edsk
answered 5 Months ago
21

Temporary files are created safely with tempnam. How you are going to call them while attaching them to a multi-part mail does not need to have anything to do with the temporary name you give the actual file. If it does in your 'mail' implementation, show that specific code, 'cause it doesn't need to be like that.

Actually, to mail it, you don't even need to create a file, just add it as a specific part to the body of the mail with proper content-encoding & boundaries, and you're done.

Saturday, May 29, 2021
 
TheFrack
answered 5 Months ago
97

Change this:

$fp = fopen($strresume, "rb"); 
$file = fread($fp, $strresume_size); 

to this:

$fp = fopen($filetemp, "rb"); 
$file = fread($fp, $strresume_size); 
Saturday, May 29, 2021
 
xrock
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :