Hey all! So I've got my PDF creation working, and I'm now trying to attach it to my MIME mail.

I've looked up the mime->addAttachment documentation here:

http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

The part I'm confused about is the: string $c_type = 'application/octet-stream'

In the documentation it says "string $c_type - The content type of the image or file."
In my books it says to add an attachment ot the message, such as a graphic or an archive, call mail_mime::addAttachment()

The specific example is:
$file = '/path/to/file.png';
$mime->addAttachment($file, 'image/png')

So in my case, I want to attach a PDF. However the documentation does not say what the potential keywords are for the second argument. So I tried,
"$mime->addAttachment($file, 'document/pdf')" This does not seem to have worked.

Does anyone have any suggestions? As always, your help is much appreciated!

    I right clicked the PDF to be attached in particular and the answer appears to be 'application/pdf'.

    However I'm still not able to get the bloody thing to attach.

    <?php
    //Attachment php
    require 'Mail.php';
    require 'Mail/mime.php';
    require_once('../config/lang/eng.php');
    require_once('../tcpdf.php');
    
    $mime = new Mail_mime;$text = 'Your Email Client is not configured to receive HTML.';$mime->setHTMLbody($text);
    
    $to = 'xxx@gmail.com';
    $headers['From']='stockinfo@xxx.net';$headers['Subject'] = '3rd Quarter 2010 Detailed Stock Report';
    
    $html = 'Test attachment!';
    
    $mime->setHTMLBody($html);$body= $mime->get();$headers = $mime->headers($headers);
    
    $fileAttach = '/var/www/zzTest PDF Local';
    $host = 'mail.xxx.net';$username = 'stockinfo@xxx.com';$password = 'xxx';
    $mime->addAttachment($fileAttach, 'application/pdf');
    $smtp =& Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username,'password' => $password));
    $smtp->send($to,$headers,$body);
    ?>
    

    I'm not sure where I've gone wrong here but the file refuses to attach. However the cause of the problem may be that the owner and group of the PDF I'm trying to attach are not...me. Sorry my Ubuntu terminology is poor haha. The PDF belongs to www-data as opposed to owner = 'John Doe' group= 'John Doe' where 'John Doe' is my log in.

      Turn on error reporting to know for sure... And look at display errors as well (and log errors below it).

      You can "chown www-data filename" to change the owner of the file. If you generate your pdf files using php scripts, the file will have the same owner as the user running the script. If you log in as john doe and execute the script, it will be john doe. If the script is run by the web server, and the web server user is "www-data", then this will be the owner. As such, there should be no problems in the future as long as the script is always run by www-data.
      And if you are running script manually, I suggest doing so as user www-data, to avoid screwing up ownership.

      sudo su - www-data

      ought to let you change to user www-data, using the same password as you logged on with, i.e. john doe's password.

        As always, thanks for the suggestions johan. I could have been a bit more detailed in my post:

        The script compiles fine. It sends the email, but the pdf does not attach. I turned on error reporting, error_reporting(E_ALL) but I've not used display_errors before. What's the syntax for that? I tried " display_errors(stderr); " but that didn't work.

        I don't really care about changing permissions and all that, I'm mostly worried about getting this attachment to work properly and I don't know if the ownership of the PDF would affect the ability of the script to attach it to an email.

        However, as far as the permissions go, when I try to chgrp or chown on the pdf it says the Operation is not permitted. I "sudo su www-data" but I wasn't sure how to call the script from the terminal once I changed the user. But again I don't know if its necessary for me to change group or owner to in order to get the attachment to work. Any suggestions would be greatly appreciated!

        -slight

          I believe you need to use ini_set() to change the value for display_errors. While your script is not live, it's convenient to display errors, but for things in production you want them to be logged instead.
          And the main reason was that rather than guess that your script doesn't have read rights for the PDF, you would know from the error output.

          The operation not permitted means your user doesn't have privileges to run chown. Since you seem to have access to sudo (which lets you run commands as root), you could try

          sudo chown ...
          

          And if that doesn't work, try becoming root for real first

          sudo su -
          chown ...
          exit
          

            Update:

            I changed to the superuser or whatever the proper term is and used chown and chgrp to change ownership to "john doe". I also added "display errors" properly. The script still runs and I still don't get any error messages or warnings at the end. I'm still getting the email but the attachment doesn't seem to want to work. Any ideas?

            -slight

              Shouldn't the owner be www-data?

              Either way, I'd remove whitespaces from the filename as well since they can cause problems. Not sure if they will or not, but I always avoid them

              # possibly bad
              $fileAttach = '/var/www/zzTest PDF Local';
              
              # no problems
              $fileAttach = '/var/www/zzTest_PDF_Local';
              

              If problems remain, you could also create a script that does nothing but

              header('content-type: application/pdf');
              echo file_get_contents('/var/www/zzTest_PDF_Local');
              

              If this gives you the pdf, then the problem is not file access.

              edit: And you should also check for errors when adding the attachment (and possibly elsewhere when using pear classes)

              # true on success, PEAR_error on failure
              $result = $mime->addAttachment($fileAttach, 'application/pdf');
              
              # error adding attachment
              if (PEAR::isError($result)) {
                #don't echo in production environment
                echo $result->getCode() . ': ' . $result->getMessage();
              }
              else {
                # success
              }
              

                Thanks for the new suggestion Johan!

                After renaming the file with the '_' , I made the new script you suggested.

                <?php
                header('content-type: application/pdf'); 
                echo file_get_contents('/var/www/zzTest_PDF_Local');
                ?>
                

                This did in fact produce the desired PDF. The ownership of the file is still set to 'john doe', I did not change it back to 'www-data'.

                As a sidenote, when I run the script and it forces the "Open/Save File" dialog box, the box says "You have chosen to open 'testAttachment.php (the name of the script) which is a: PDF document from http://localhost"
                I found this interesting because the PDF file that's actually being opened is of course the 'zzTest_PDF_Local'.

                However I did figure out the problem 🙂. It appears that if you place the mime->get() or mime->headers() after the mime->addAttachments the attachment will not add itself. Thanks again for your help.

                -slight

                  Write a Reply...