[reading email] How to save the attachments?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sonja.chevre@gmail.com

    [reading email] How to save the attachments?

    Hello !

    I'm looking for a script which allows me to read emails and attachment
    from a pop server (then I'll save the email's text in a db and the
    attachments on the server).

    So I have found this article: http://www.phpit.net/article/read-email-php-pop3/

    ....
    $email = $pop3->get_mail($i) ; // how to get the email
    ....

    But then how do I get the information about my attachments and how can
    I save them?

    Thanks a lot
    Sonja

  • davidkruger@techie.com

    #2
    Re: How to save the attachments?

    On Jul 26, 3:49 am, sonja.che...@gm ail.com wrote:
    Hello !
    >
    I'm looking for a script which allows me to read emails and attachment
    from a pop server (then I'll save the email's text in a db and the
    attachments on the server).
    >
    So I have found this article:http://www.phpit.net/article/read-email-php-pop3/
    >
    ...
    $email = $pop3->get_mail($i) ; // how to get the email
    ...
    >
    But then how do I get the information about my attachments and how can
    I save them?
    >
    Thanks a lot
    Sonja
    Hi, I may not be the best to respond to this, since I am not familiar
    with reading the pop3 e-mail through php at all...but I do know that
    with e-mails, attachments are encoded right in the text of the
    message, if you save a message and view it in a text editor, you will
    likely see the encoded text for your attachement(s).

    In the headers for the e-mail message you will find a section similar
    to:

    ------=_NextPart_000_ 001C_01C7C9E5.5 6D0D990
    Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_ 001D_01C7C9E5.5 6D32380"

    Directly above the encoded attachment will be a section similar to:

    ------=_NextPart_000_ 001C_01C7C9E5.5 6D0D990
    Content-Type: application/x-zip-compressed;
    name="BELL REPLACEMENT FILES.zip"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
    filename="BELL REPLACEMENT FILES.zip"


    ------=_NextPart_000_ 001C_01C7C9E5.5 6D0D990--

    As you notice the content in the mail is encoded as base64.

    Where ever the string after the boundary= portion is, will be either
    the e-mail message itself, content-type info, or the attachment data
    itself. Once you get your text from the e-mail file that is the
    encoded attachment, you could probably use:

    $file_as_str=ba se64_decode($en coded_string);

    which you should then be able to just output the contents of the
    base64_decode results to a file to have the attachment file saved, or
    place the data in a blob in a mysql table or whatever you would choose
    to do with it. Hopefully that makes sense.

    Comment

    • Rik

      #3
      Re: [reading email] How to save the attachments?

      On Thu, 26 Jul 2007 10:49:30 +0200, <sonja.chevre@g mail.comwrote:
      Hello !
      >
      I'm looking for a script which allows me to read emails and attachment
      from a pop server (then I'll save the email's text in a db and the
      attachments on the server).
      >
      So I have found this article:

      >
      ...
      $email = $pop3->get_mail($i) ; // how to get the email
      ...
      >
      But then how do I get the information about my attachments and how can
      I save them?
      Aside from the correct notes from David, there are several packages who
      can do this for you without having to reinvent the wheel. Several come to
      mind:

      1. <http://nl2.php.net/manual/en/ref.imap.php>
      2. <http://nl2.php.net/mailparse>
      3.
      <http://pear.php.net/package/Mail_mimeDecode/docs/latest/Mail_Mime/Mail_mimeDecode .html>

      --
      Rik Wasmus

      Comment

      • sonja.chevre@gmail.com

        #4
        Re: How to save the attachments?

        Great! Thank you to both of you for the useful information

        Comment

        • bill

          #5
          Re: [reading email] How to save the attachments?

          Rik wrote:
          On Thu, 26 Jul 2007 10:49:30 +0200, <sonja.chevre@g mail.comwrote:
          >
          >Hello !
          >>
          >I'm looking for a script which allows me to read emails and attachment
          >from a pop server (then I'll save the email's text in a db and the
          >attachments on the server).
          >>
          >So I have found this article:
          >http://www.phpit.net/article/read-email-php-pop3/
          >>
          >...
          > $email = $pop3->get_mail($i) ; // how to get the email
          >...
          >>
          >But then how do I get the information about my attachments and how can
          >I save them?
          >
          Aside from the correct notes from David, there are several packages who
          can do this for you without having to reinvent the wheel. Several come
          to mind:
          >
          1. <http://nl2.php.net/manual/en/ref.imap.php>
          2. <http://nl2.php.net/mailparse>
          3.
          <http://pear.php.net/package/Mail_mimeDecode/docs/latest/Mail_Mime/Mail_mimeDecode .html>
          >
          >
          http://nl2.php.net/mailparse results in a 404

          http://us2.php.net/manual/en/ref.mailparse.php works

          bill

          Comment

          Working...