Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get email and their attachments from PHP

Tags:

php

email

I'm writing a photo gallery webapp for a friend's wedding and they want a photo gallery for guests to submit the digital photos they take on the day.

After evaluating all the options, I've decided the easiest thing for users would be to let them use a familiar interface (their email) and just have them send in the pictures as attachments.

I've created an mailbox but now I need to connect and retrieve these attachments for automated processing for adding to the gallery system. But how? Are there any tutorials or prefab classes you've seen for doing this?

like image 413
Oli Avatar asked Sep 22 '08 13:09

Oli


People also ask

How to send an attachment along with the email?

To send an attachment along with the email, we need to set the Content-type as mixed/multipart and we have to define the text and attachment sections within a Boundary. echo "File Sent Successfully."; die("Sorry but the email could not be sent. Please go back and try again!");

How to get the plain text part of an email in PHP?

The imap_fetchbody () function fetches a particular section of the email body. So to get the plain text part of the email, we can use “1.1” option as the third parameter. /* loop through each email id mails are available.

How does mail () function work in PHP?

When the mail () function is called PHP will attempt to send the mail immediately to the recipient then it will return true upon successful delivery of the mail and false if an error occurs. Here is the description of each parameter. This contains the subject of the email.

What is IMAP extension in PHP?

IMAP is an internet standard protocol used by clients to get emails from a mail server over a TCP/IP connection with SSL security. The IMAP extension available in PHP libraries provides efficient processing of its email structure and access to email messages via communicating with the email servers.


4 Answers

I used to do a lot of this before, but I can't find the code, here's a scaled down version I found. It should put you on the correct path. I used to run this type of script from a cronjob. Sorry I can't find the final version. ;(

// Open pop mailbox
if (!$mbox = imap_open ("{localhost:110/pop3/notls}INBOX", "user", "tester")) {
  die ('Cannot connect/check pop mail! Exiting');
}

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
} else {
  echo "Failed to get mail";
  exit;
}

$MN=$msgCount;
$overview=imap_fetch_overview($mbox,"1:$MN",0);

for ($X = 1; $X <= $MN; $X++) {

  $file = imap_fetchbody($mbox, $X, 1);

  imap_delete($mbox, $X);
}

imap_expunge($mbox);
imap_close($mbox);

Good luck!

like image 77
DreamWerx Avatar answered Oct 14 '22 01:10

DreamWerx


Have you considered using Google's Picasa Web Albums? You can set up an email address to send photos to and share them online. You can then get an RSS feed of these photos, which most programmers are more familiar with than MTAs.

like image 37
Liam Avatar answered Oct 13 '22 23:10

Liam


If you're creating a dedicated mailbox for this purpose, using a filtering mechanism is almost definitely not what you want. Instead, you want to have the mailbox be a pipe to the application, and have the application simply read in the message from stdin, parse out the body, and MIME parse the body to get the attachments.

Having a mailbox be a pipe is supported by all the popular unix-based MTAs that I know of, such as sendmail, postfix, and qmail. Generally you define it in your aliases file, like so:


# sendmail or postfix syntax
msgsubmit: "| /usr/bin/php ~path/to/example.php"

Then mails to msgsubmit@ get routed to a php program for delivery.

This has the advantage of not relying on an IMAP server or any other server beyond the MTA being alive, and it works fine as long as you have control over the MTA of the destination host. Filtering is what you'd want if you wanted all messages on a system to be inspected by the script, which I'm guessing is not the case.

If you want a copy kept in a mailbox somewhere (not a bad idea) simply define the alias to go to multiple addresses, like so:


msgsubmit: "| /usr/bin/php ~path/to/example.php", msgsubmit-box

Or postfix virtual format:


msgsubmit
    "| /usr/bin/php ~path/to/example.php"
    msgsubmit-box

like image 39
Daniel Papasian Avatar answered Oct 14 '22 01:10

Daniel Papasian


What MTA are you using? If you use postfix + maildrop you can create a filtering rule that pipes certain messages through a PHP script that then handles the incoming mails. (google for maildrop and xfilter).

like image 40
Armin Ronacher Avatar answered Oct 14 '22 00:10

Armin Ronacher