Jump to content


Photo

Attaching PDF files using mail()?


  • Please log in to reply
3 replies to this topic

#1 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 19 October 2006 - 08:13 AM

Hello,

I am fairly familiar with the mail function used in PHP and from what I can tell its possible to attach files to emails sent using this method but I am little unclear on how or what code is to be used?

I think that its a mime type header but I am not certain.

If this is my mailer code,

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$to = "my@email.com";
  
  $message = "my email";

  //Additional Headers
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers .= 'Return-Path: <my@email.com>' . "\r\n";

  $headers .= "To: You <your@email.com>"."\r\n";
  
  $headers .= "From: Me <my@email.com>"."\r\n";

  // Mail it
  $mail = mail($to, $subject, $message, $headers);


How do I attach a PDF? Or in fact any file as an attachment?

Thanks Cep

#2 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 19 October 2006 - 08:30 AM

You can create the entire message in the header.

<?php

$message = chunk_split(base64_encode($message))
$encoding = "base64";

$from = "me@gmail.com";
$to = "you@gmail.com";

$mime = "From: ". $from ."\n";
// Add any other types of headers here
$mime .= "MIME-Version: 1.0\n".
$mime .= "Content-Type: text/plain"
              ."\nContent-Transfer-Encoding: $encoding\n\n$message\n";

// Creates a boundary between the main message and the attachment
$boundary = "b".md5(uniqid(time()));
$mime .= "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";


$pdf_attachment = chunk_split(base64_encode(fread(fopen($filename), filesize($filename))));
$file = "Content-Type: text/pdf; name = ". basename($filename) ."\n"
        ."Content-Transfer-Encoding: $encoding\n\n$pdf_attachment\n";

$mime .= "\n". $file . "--$boundary";
$mime .=  "--\n";

mail($to, $subject, "", $mime);

?>

SA PHP Archives - www.phparchives.za.org

#3 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 19 October 2006 - 10:41 AM

I'll try this out thanks :)
Thanks Cep

#4 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 08 November 2006 - 04:47 PM

I have a few more questions about this subject.

Firstly do you have to send attachments in base64 encoding?

Secondly do you have to create a new unique boundary for each attachment you add

Thirdly if you have to use base64 what would prevent you from reading the email in the correct format (I tried the above in my mail script and I just got a load of base64 giberrish in my Outlook.)
Thanks Cep




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users