php


How to parse raw E-Mail with Zend Framework 2?


I have e-mails to my server being piped to my Zend Framework 2 index (following MVC) and that gets sent to my controller.
public function incomingMailAction()
{
$message ='';
$stdin = fopen('php://stdin', 'r');
while($line = fgets($stdin)) {
$message .= $line;
}
fclose($stdin);
// Parse e-mail here and store in database (including attachments)
}
I can handle the storing in the database part, I just don't know how to take that raw message and then turn it into something useful (To, From, ReplyTo, CC, BCC, Headers, attachments... ect).
Thanks!
You can use Zend\Mail\Message::fromString($rawMessage); It will not decode MIME body though.
I attempted to parse E-Mails with ZF2 too, but I actually found a comment in the source code of the Zend Mail component that decoding the message is on the todo list and not yet implemented. There seems to be no easy way of doing this currently.
Instead, I recommend using php-mime-mail-parser - I ended up using that library instead. It uses the functions of the pecl extension mailparse (which you may need to install) and is suprisingly easy. Some examples which should get you started:
$message = new \PhpMimeMailParser\Parser();
$message->setText($rawMail); // Other functions to set a filename exists too
// All headers are retrieved in lowercase, "To" becomes "to"
// and "X-Mailer" becomes "x-mailer"
$recipient = $message->getHeader('to');
$date = $message->getHeader('date');
$xmailer = $message->getHeader('x-mailer');
// All headers can be retrieved at once as a simple array
$headers = $message->getHeaders();
$recipient = $headers['to'];
// Attachments can be retrieved all at once as "Attachment" objects
$attachments = $message->getAttachments();
foreach($attachments as $attachment) {
$attachment_as_array = array(
'type' => $attachment->getContentType(),
'name' => $attachment->getFilename(),
'content' => (string)$attachment->getContent(),
);
}
Because the library uses an existing extension of PHP and seems to be very efficient in terms of memory management it is probably a better fit to parse emails than ZF will ever be - and it is also very easy to use. The only drawback for me was the additional installation of the mailparse pecl extension on every server.
public function incomingMailAction()
{
$message ='';
$stdin = fopen('php://stdin', 'r');
while($line = fgets($stdin)) {
$email .= $line;
}
fclose($stdin);
$to1 = explode ("\nTo: ", $email);
$to2 = explode ("\n", $to1[1]);
$to = str_replace ('>', '', str_replace('<', '', $to2[0]));
list($toa, $tob) = explode('#', $to);
}
Stolen from: PHP email Piping get 'to' field

Related Links

Get nested documents with a filter on Elasticsearch 5
Add tracking pixel to wordpress for post views
suppress key from an array search and get the next key
Adding image alt attribute field
Codeigniter url redirect
Getting errors when calling functions inside stored procedure
apache stopped working suddenly port changing was useless
Move WordPress Network / Multi site to another directory on the same server
Dubts about PHP Parametized queries and password_hash
Laravel :values placeholder not working?
How to count SESSIONS to display total number of logged in users on the site
Create php fom select multiple taking values from a table bd
shell_exec command in php not working properly
Non-Capturing Group: Remove 3rd value
Unable to connect to php database with pdo
Fastest way to trim first flash?

Categories

HOME
events
magento2
iverilog
tcp
google-webmaster-tools
hyperledger
linker
tweepy
kurento
responsive-design
phpstorm-2017.1
buffer
powermock
webstore
supervisord
temperature
web-deployment
hidden
maven-plugin
jest
angular2-highcharts
gitkraken
sqlplus
stackexchange.redis
multi-upload
react-dnd
raima
high-availability
ruby-daemons
ninject
intune
d3.js-v4
infopath2010
quick-nimble
delayed-job
dreamfactory
google-now
swfupload
suds
grails-spring-security
coverage.py
dagger
gcal
realstudio
sonicmq
asp.net-web-api-routing
connect-direct
dynamics-crm-4
ipywidgets
okio
livecycle
raw-sockets
xcode8-beta4
swiffy
pack
ui4j
arbre
self
gitlab-omnibus
componentart
abstract-factory
prism-4
ssmtp
sql-processor
uno
n-tier-architecture
encryption-symmetric
utf8-decode
dmake
osascript
rpg
neo4jphp
accountmanager
property-injection
meteorite
application-blocks
vlab
chartfx
unicoins
sly-scroller
notin
oscilloscope
asp.net-mvc-3-areas
chefspec
story
telerik-ajax
prefuse
autosize
exponent
mkannotation
memory-pool
nosetests
asp.net-mvc-views
servlet-container
ios-4.2
icicles
j2mepolish
.net-services
getimagesize
yahoo-maps
downcasting
webclient.uploaddata

Resources

Encrypt Message