Trying to attach and attachement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Trying to attach and attachement

Post by Addos »

I can’t seem to figure out how to get an attachment to send from this script below. The mail will send ok and I have a folder called ‘files’ where I upload the PDF’s, word docs etc to have attached to the script but I cannot work out how to pass these to this script. I think I should be writing a path to $file_name = "files/somefile.pdf"; but can’t be sure.
Any help would be great
Thanks

Code: Select all

include('Mail.php');
    include('Mail/mime.php');
 
$sender = "Joe soap <info@me.com>";  
 $recipient = "Crazy<info@you.com>"; 
 $subject = "Test Email";
 $text = 'This is a test message.';  
 $html = '<html><body><p>This is a html message</p></body></html>';          $crlf = "\n";
        $headers = array(
                        'From'          => $sender,
                        'Return-Path'   => $sender,
                        'Subject'       => $subject 
                        );
 
        // Creating the Mime message
        $mime = new Mail_mime($crlf);
 
        // Setting the body of the email
        $mime->setTXTBody($text);
        $mime->setHTMLBody($html);
 
        // Add an attachment
   $file = "Hello World!"; 
   $file_name = "files/somefile.pdf";
   $content_type = "Application/pdf"; 
   $mime->addAttachment ($file, $content_type, $file_name, 0);  
   $body = $mime->get();
   $headers = $mime->headers($headers);
 
   // Sending the email
   $mail =& Mail::factory('mail');
   $mail->send($recipient, $headers, $body)
Post Reply