Extract attachments when piping mails to PHP
Asked Answered
R

2

9

I know with my cPanel hosting I can pipe an email inbox to a script, but what I want to do is:

  1. Send to [email protected]
  2. Pipe to mail.php
  3. mail.php reads the subject, and a .txt attachment
  4. The contents of the subject and .txt attachment are stored in the database

Is there a way to do this with straight-forward PHP?

Rattle answered 26/10, 2009 at 10:40 Comment(0)
Y
5

You'll probably need do the following:

  1. Write a PHP script that's executable at the CLI (by adding a #! declaration at the top of the script that points to the PHP binary, then settings its executable permissions).

  2. Get that script to read the raw email from php://stdin (file_get_contents is easiest)

  3. Get that script to decode the mail in to parts, using something like PEAR::Mail::Mime::Decode or I think there's a handy Zend Framework component).

  4. Read the attachment and subject from the decoded message, and store as normal

  5. exit(0) at the end to tell the CLI that it was a clean exit - any other exit() status could cause a bounced email.

Youngling answered 26/10, 2009 at 11:29 Comment(1)
Thanks! I am new to executable files, so will need to look into it, do you know any links that might be useful to me :)Rattle
H
6

There exists a PHP library, called php-mime-mail-parser which itself depends on the PECL mailparse library. When you have those installed, the code to achieve what you want is quite straight forward:

<?php
require_once 'MimeMailParser.class.php';

$parser = new MimeMailParser();
$parser->setStream(STDIN);

$subject = $parser->getHeader('subject');

$attachment_content = false;
foreach ($parser->getAttachments() as $attachment) {
    $extension = pathinfo($attachment->filename, PATHINFO_EXTENSION);
    if ($extension == "txt") {
        $attachment_content = $attachment->content;
        break;
    }
}

// adapt to what ever database you are using
$sth = $mysqli->prepare("INSERT INTO mails (subject, attachment) VALUES (:subject, :attachment)");
$sth->bindParam(':subject', $subject, PDO::PARAM_STR);
$sth->bindParam(':attachment', $attachment_content, PDO::PARAM_STR);
$sth->execute();

You can pipe the mail into the script, as it reads from STDIN. You can also read from a file by changing setStream to setPath. See the documentation of the library.

Horatius answered 3/6, 2015 at 19:24 Comment(3)
What sort of changes are required to save and store a pdf file? Is there any sort of encryption or encoding that needs to be dealt with or just change the txt to pdf?Synergetic
Storing the contents of a PDF file in a database field is not something I'd recommend. Better store it to the filesystem and just store the path.Horatius
That is what I mean, I would want to save the attachment as a file and store the location. However I am uncertain on how to handle it as other 'tutorials' or answers I've read refer to some sort of base64 decoding.Synergetic
Y
5

You'll probably need do the following:

  1. Write a PHP script that's executable at the CLI (by adding a #! declaration at the top of the script that points to the PHP binary, then settings its executable permissions).

  2. Get that script to read the raw email from php://stdin (file_get_contents is easiest)

  3. Get that script to decode the mail in to parts, using something like PEAR::Mail::Mime::Decode or I think there's a handy Zend Framework component).

  4. Read the attachment and subject from the decoded message, and store as normal

  5. exit(0) at the end to tell the CLI that it was a clean exit - any other exit() status could cause a bounced email.

Youngling answered 26/10, 2009 at 11:29 Comment(1)
Thanks! I am new to executable files, so will need to look into it, do you know any links that might be useful to me :)Rattle

© 2022 - 2024 — McMap. All rights reserved.