ZF2 reading mail with attachments?? - php

I can't find any documentation about reading email attachments. Can anyone suggest something?
I am using Zend\Mail\Storage\Imap to read my emails text, but I cant find any methods to get attachments.

ifYou can access the attachments using the Mime/Part package. You can firstly test that a message is multipart using $message->isMultipart()
You can then iterate a messages parts or access a specific part using $part = $message->getPart($num);
from there you can access the type of part, determine if its inline/attachements and so on.
Useful links:
see : http://framework.zend.com/manual/2.2/en/modules/zend.mail.attachments.html
see : http://framework.zend.com/manual/2.2/en/modules/zend.mime.part.html

Related

Is there a way to get the body of a Laravel Mail object to store in a variable?

I'm using the Laravel Mail API to generate mailables and send them out.
Everything is working fine with that.
However, I also want to log the email bodies in the DB.
In order to do that though, I need to first get the bodies of the emails I'm sending out.
In the Laravel docs, they have a snippet about how to output an email to the browser here:
https://laravel.com/docs/5.7/mail#previewing-mailables-in-the-browser
However, I don't want to output the email to the browser. I want to store it in a variable.
Is that possible? If so, how? Thanks.
Like the docs suggested, I tried something like the following:
$body = new App\Mail\InvoicePaid($invoice);
However, I got an error saying that I have to actually return the new Mail object in order for it to work (i.e., do what they do in the example code in the docs).
Previous paragraph tells you how to capture the rendered template:
$html = (new App\Mail\InvoicePaid($invoice))->render();
echo $html;

IMAP map_search() for ics files

i wants to fetch email having only .ics files using IMAP in php
$emails = imap_search($inbox, 'ALL');
i am using above code for search i need some condition instead of "All" or imap_search by some extension(.ics) which gives me emails having .ics
you ccan used this function to serach the perticular extation in your email.
<?php $keyword=".ics";
$emails = imap_search($inbox,'BODY "'.$keyword.'"', SE_FREE, "UTF-8");
?>
The search key header content-disposition ".ics" may or may not work. Depends on the server.
IMAP isn't quite well defined in this aspect.
HEADER <field-name> <string>
Messages that have a header with the specified field-name (as
defined in [RFC-2822]) and that contains the specified string
in the text of the header (what comes after the colon). If the
string to search is zero-length, this matches all messages that
have a header line with the specified field-name regardless of
the contents.
So the field name has to be as defined in 2822. But email messages contain three kinds of header (the top-level message header, embedded message/822 headers and finally bodypart headers), and all three use that syntax. I suspect that most servers search either the first kind of header or all three, and that the RFC's author had in mind the first two.
So why don't you just try the search key with the target server, and if it works, it works.
Thanks you friends i got the exact solution of my problem
i am opening connection first by imap_open then searching emails
$imap_search($inbox, 'ALL')
then fetching structure of emails by imap_fetchstructure and from that structure i am getting ics files from attachments and finally form attachments i am getting imap body imap_base64(imap_fetchbody($mbox, $mid, 2));
it will gives me actually .ics calendar content

When to use imap_qprint?

I'm using PHP's IMAP Functions extension to check e-mail from a POP3 account.
When it comes to actually fetching the message, I have the following code:
// Make new raw email message
// With PHP Imap, we need to fetch headers and body separately!
$body = imap_fetchheader($mailbox, $msgno);
$body .= imap_qprint(imap_body($mailbox, $msgno));
All the examples I am able to find mentions that one should use imap_qprint() here, but I've noticed that when I do all the GET parameters of a URI get mangled.
For example,
http://localhost/pronk.php?id=6248&key=c7eb7c5173e1525a47c63abc39d938e1
becomes:
http://localhost/pronk.php?idb48&keyƇeb7c5173e1525a47c63abc39d938e1
If I don't use imap_qprint() everything seems to work just fine. (I'm using imap_body instead of imap_fetch_body because I want the entire e-mail - headers, parts, and everything) But since all examples I can find say to use qprint, I wanted to know why my code seems to need it omitted?
Not sure if you're still curious about this, but if you do want to use imap_qprint(), you need to first check what the encoding of the message is.
You can do this using imap_fetchstructure(). http://php.net/manual/en/function.imap-fetchstructure.php.
Only if the encoding is set to 'QUOTED-PRINTABLE' would you want to use imap_qprint().

What is the best way to mark parsed emails as "parsed" .. just delete them?

I'm writing an email parser for a site and I'm not sure of best practices. Specifically, I am not sure how to mark emails that I have already parsed, so I don't access them each time I access the mailbox.
PS - I've never done any email parsing.
I'm using the Flourish library (along with Codeigniter) so so far I am calling cronjobs/parseMail with a cron job
public function parseMail(){
// Connect to a remote imap server
$mailbox = new fMailbox('imap', 'mysite.com', 'user', 'password');
// Retrieve an overview of all messages
$messages = $mailbox->listMessages();
foreach ( $messages as $message ){
$messageBody = $message['text'];
// parse it
}
}
So once I have "dealt with" an email.. should I just delete it? Or is there a better way to insure that I am not parsing emails I have already done?
BONUS QUESTION > Dont I need to supply a specific email account somewhere? If I have "admin#mysite.com" and "addressForParsing#mysite.com" .. where does that get specified that I am only interested in the latter? Do I just pull the "To:" out of my parsed info or is there a better way?
Flourish: wow... this is even less helpful than the stock PHP functions. You'll have to store message UIDs externally from IMAP to track if something's been processed or not.
PHP/CodeIgniter: CI doesn't seem to have an IMAP library, so you're using PHP functions. imap-setflag-full() will let you set the \Flagged flag on the message which you can use to track if the message has been processed.
Custom Socket Code: you can use something like this code to set/get custom IMAP flags, but you'll probably have to read a handful of IMAP RFCs to get everything else working.

PHP class to read emails with attachments through IMAP

I am looking for PHP class, that helps me read emails and their attachments.
I tried to write it on my own with IMAP class in PHP, but emails exists in many, many formats. And it is complicated to include all of them to get some reasonable text output.
It should look something like this:
$mailbox = Mailbox("{localhost:993/imap/ssl}INBOX", "user_id", "password");
$unreadMessages = $mailbox->unreadMessages(); // just ids
$message = $mailbox->getMessage(5); // headers and body in plain text, ids of attachments
$message->saveAttachment(1, '/path/to/attachments/folder');
$message->seen();
Thanks for help!
I use Mail component from Apache Zeta Components. It's really easy to use. I had a working prototype in under one hour.
Check out Zend_Mail - it has lots of options, you will probably find everything you need. Maybe not as simple as your code above, but as you said yourself, there are a lot of options.
http://framework.zend.com/manual/en/zend.mail.read.html

Categories