Hi. I'm not sure if this is possible:

I found out that Blogger allows you to email text, images, etc to a unique email address, and then it'll post the email as a blog post. I'm trying to create a system like that for my client. Is it possible to 'grab' the email body (and possibly the subject) in PHP?

Dani AI

Generated

Yes — you can capture the subject and body in PHP. There are two reliable patterns: have your mail server pipe incoming messages for a dedicated address straight to a PHP script, or have a PHP process poll a mailbox via IMAP/POP3. is correct that libraries exist to help, but here are practical, hands‑on options and pitfalls to watch for.

A minimal pipe script (set your MTA alias/.forward to run it) — this reads raw mail from STDIN and extracts headers/body (naive split shown; production needs a MIME parser):

#!/usr/bin/php
<?php
$raw = stream_get_contents(STDIN);
$parts = preg_split("/\r?\n\r?\n/", $raw, 2);
$headers = $parts[0] ?? '';
$body = $parts[1] ?? '';
$subject = '';
if (preg_match('/^Subject:\s*(.*)$/mi', $headers, $m)) $subject = trim($m[1]);
file_put_contents('/tmp/emaillog.txt', "Subject: $subject\n\n$body\n\n", FILE_APPEND);
?>

Or poll with IMAP (use PHP's imap extension to fetch parts and then decode/multipart-parse):

<?php
$mbox = imap_open('{imap.example.com:993/imap/ssl}INBOX','user','pass');
$ids = imap_search($mbox, 'UNSEEN');
foreach ($ids ?: [] as $num) {
  $hdr = imap_headerinfo($mbox, $num);
  $subject = isset($hdr->subject) ? imap_utf8($hdr->subject) : '';
  $body = imap_fetchbody($mbox, $num, 1);
  // decode or fetch other parts as needed
}
imap_close($mbox);
?>

Notes and gotchas: multipart emails, inline images and attachments require a MIME-aware parser (mailparse, php-mime-mail-parser or similar) and decoding (base64/quoted-printable). Protect against spam and abuse by using unique recipient tokens, verifying senders, limiting sizes, and sanitizing HTML. Test with real .eml samples, ensure the script is executable, handle concurrent deliveries (locking/queue), and store attachments outside the webroot. If you prefer not to run mail infrastructure, inbound email webhooks from mail providers are a simpler alternative.

Recommended Answers

All 2 Replies

I guess not :)

Yes this is absolutely possible.

The only library that is coming to mind is Zend_Mail http://framework.zend.com/manual/en/zend.mail.read.html

Other examples exists such as:

Googling will yield a lot of different examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.