Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read out /var/mail/username using php?

Tags:

php

email

parsing

I'm sending local mail using the php mail() function like this:

mail('kramer65@localhost', 'THE SUBJECT', 'THE BODY', $headers);

When I do cat /var/mail/kramer65 I get the following:

Received: by php0 (Postfix, from userid 1007)
    id 1A0A028DB7; Tue, 29 Apr 2014 11:00:48 +0200 (CEST)
To: kramer@localhost
Subject: THE SUBJECT
X-PHP-Originating-Script: 0:MyControllerTest.php
From: [email protected]
Reply-To: [email protected]
X-Mailer: PHP/5.5.11-1~dotdeb.1
Message-Id: <20140429090048.1A0A028DB7@php0>
Date: Tue, 29 Apr 2014 11:00:48 +0200 (CEST)

THE BODY

To read that out from php I run the mail() function again and then I try to use the php mailparse extension and syslog the result like this

$output = mailparse_msg_parse_file('/var/mail/kramer65');
syslog(LOG_ERR, 'mailparse RESULT: ' . $output);

which results in: Apr 29 11:00:48 php0 php: mailparse RESULT: Resource id #635

Does anybody know what I'm doing wrong here? How can I read out the results from /var/mail/kramer65 from within php? All tips are welcome!

like image 359
kramer65 Avatar asked Mar 23 '26 05:03

kramer65


1 Answers

mailparse_msg_parse_file() returns a MIME Resource, so that's why you're getting Resource id #635. You need to do something more to get the values you want out of the MIME Resource.

For example:

mailparse_msg_get_part_data ( resource $mimemail ) 

which gives you an array with the values you're looking for.

like image 178
commesan Avatar answered Mar 24 '26 19:03

commesan