Skip to content

Instantly share code, notes, and snippets.

@Mosharush
Created September 1, 2017 11:19
Show Gist options
  • Save Mosharush/a325f67b1eb8ea7b5c024351396ae255 to your computer and use it in GitHub Desktop.
Save Mosharush/a325f67b1eb8ea7b5c024351396ae255 to your computer and use it in GitHub Desktop.
Read emails from SMTP - PHP
<?php
class Email_reader {
// imap server connection
private $conn;
// inbox storage and inbox message count
public $inbox;
public $msg_cnt;
// email login credentials
private $server;
private $user;
private $pass;
private $port;
// connect to the server and get the inbox emails
function __construct( $user, $pass, $server = '127.0.0.1', $port = 25 ) {
$this -> server = $server;
$this -> port = $port;
$this -> pass = $pass;
$this -> user = $user;
$this->connect();
$this->inbox();
}
// close the server connection
function close() {
$this->inbox = array();
$this->msg_cnt = 0;
$error = imap_errors();
imap_alerts();
if (count($error) > 1 || $error[0] != 'SECURITY PROBLEM: insecure server advertised AUTH=PLAIN') {
// More than 1 error or not the expected error
var_dump($error);
throw new Exception('IMAP error detected');
}
imap_close($this->conn);
}
// open the server connection
// the imap_open function parameters will need to be changed for the particular server
// these are laid out to connect to a Dreamhost IMAP server
function connect() {
$this->conn = @imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
}
// move the message to a new folder
function move($msg_index, $folder='INBOX.Processed') {
// move on server
imap_mail_move($this->conn, $msg_index, $folder);
imap_expunge($this->conn);
// re-read the inbox
$this->inbox();
}
// get a specific message (1 = first email, 2 = second email, etc.)
function get($msg_index=NULL) {
if (count($this->inbox) <= 0) {
return array();
}
elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
return $this->inbox[$msg_index];
}
return $this->inbox[0];
}
// read the inbox
function inbox() {
$this->msg_cnt = imap_num_msg($this->conn);
$in = array();
for($i = 1; $i <= $this->msg_cnt; $i++) {
$in[] = array(
'index' => $i,
'header' => imap_headerinfo($this->conn, $i),
'body' => imap_body($this->conn, $i),
'structure' => imap_fetchstructure($this->conn, $i)
);
}
$this->inbox = $in;
}
function get_attachments( $email ) {
$attachments = array();
// check for attachments
if ( isset( $email['structure']->parts ) && count( $email['structure']->parts ) ) {
// loop through all attachments
for ($i = 0; $i < count($email['structure']->parts); $i++) {
// set up an empty attachment
$attachments[$i] = array(
'is_attachment' => FALSE,
'filename' => '',
'name' => '',
'attachment' => ''
);
// if this attachment has idfparameters, then proceed
if ($email['structure']->parts[$i]->ifdparameters) {
foreach ($email['structure']->parts[$i]->dparameters as $object) {
// if this attachment is a file, mark the attachment and filename
if (strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = TRUE;
$attachments[$i]['filename'] = $object->value;
}
}
}
// if this attachment has ifparameters, then proceed as above
if ($email['structure']->parts[$i]->ifparameters) {
foreach ($email['structure']->parts[$i]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = TRUE;
$attachments[$i]['name'] = $object->value;
}
}
}
// if we found a valid attachment for this 'part' of the email, process the attachment
if ($attachments[$i]['is_attachment']) {
// get the content of the attachment
$attachments[$i]['attachment'] = imap_fetchbody($this->conn, $email['index'], $i+1);
// check if this is base64 encoding
if ($email['structure']->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
// otherwise, check if this is "quoted-printable" format
elseif ($email['structure']->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
if( empty( implode( '', $attachments[$i] ) ) ){
unset( $attachments[$i] );
}
}
}
if( !empty( $attachments ) ){
return $attachments;
}
return false;
}
}
<?php
ini_set( 'display_errors', true );
error_reporting( -1 );
include 'email.reader.class.php';
$reader = new Email_reader( 'automail@domain.co.il', 'PA$$' );
imap_errors();
imap_alerts();
foreach( $reader->inbox as $email ){
$attachments = $reader -> get_attachments( $email );
foreach( $attachments as $attachment ){
$filename = $attachment['filename'];
if( strpos( $filename, 'UTF-8' ) !== false ){
$filename = explode( '?', $attachment['filename'] );
$filename = base64_decode( $filename[3] );
}
header( 'Content-type: application/octet-stream' );
header( 'Expires: 0' );
header( 'cache-Control: no-cache, no-store, must-revalidate' );
header( "Pragma: no-cache" );
header( 'content-disposition: attachment; filename="' . $filename . '"' );
header( 'content-type: application/octet-stream' );
header( 'content-length: ' . strlen( $attachment['attachment'] ) );
echo $attachment['attachment'];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment