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().
Related
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;
I have a php file that I use to send newsletters. Recently I moved to a new server and they use PEAR Mail instead of the default PHP mail to send mails and I had to update my script to function. But it's still not working. I get the TXT version not the HTML version.
If I manually enter the html codes inside the setHTMLBody() it works but when I replace it with my ob_start $output_string variable it doesn't work.
Here is my script;
ob_start();
include "URL/To/File.php";
$output_string = ob_get_contents();
ob_end_clean();
$headers['From'] = 'from#email.com';
$headers['Subject'] = 'Newsletter Subject';
require_once('Mail.php');
require_once('Mail/mime.php');
$message = new Mail_mime();
$message->setTXTBody("Your client doesn't support HTML.");
$message->setHTMLBody(''.$output_string.'');
$mail =& Mail::factory('mail');
$result = $mail->send('myemailaddress#gmail.com', $message->headers($headers), $message->get());
if (PEAR::isError($result)) {
echo("<span>" . $result->getMessage() . "</span>");
} else {
echo("<span style='color: #f7941c; font-weight: bold'>Congratulations!
Your mail has been sent successfully</span>");
}
how do I correctly input the line below correctly? It's not working as is right now.
$message->setHTMLBody(''.$output_string.'');
So I'm cold on this subject right now (working on mobile) though let's see if I can help you out. So I looked up the setHTMLBody function. It's a little fuzzy on the type that the expected parameters should be. In PHP you can get the type using gettype($example) (like console.log(typeof example); in JavaScript though PHP is generally more forgiving about types (calculating a number that has a string type will work in PHP, not JavaScript)).
The name of the function implies that it should make this part of the email HTML. Now of all the modules I've built on my web platform email has been the most challenging not because it's inherently complex though because it's very subjective. In example some servers might expect you to serve an <html> element, others a <body> element and others won't care if you omit it (and I'm not sure what if any specifications declare what is "proper" here). I've not intentionally worked with compressing data in emails (just output in web mail though it's technical context is lost at that point). Long story straight here: the client's user agent (browser, email application, etc) should be handling the compression, not you.
PHP ob stuff is a bit convoluted. I dislike the same function/method being used for both compression and being able to capture and do find/replace with the output before sending it to a client. I think you're using it for compression though you could also be using it to replace bits of code for whatever reason. In this case your best bet for troubleshooting (presuming that your ob should work, most likely for replacing bits of code) is to use the string and test it outside of this environment. When I test cron jobs I always test them in normal environments first (though keep in mind cron jobs run in a much more limited environment so for debugging there I just have print_r($_SERVER) send me information via email).
So I think your ob code is messing up the parser setHTMLBody() function. Break your code down until you have working bits and then add your necessary and increasingly complex bits to it until you hit a problem and then because you know exactly what you just added you'll be able to single out the issue much easier.
I'd need further clarification though I can edit this answer later. Let me know where you're at, I always check notifications even if it takes a day.
I have a few dozen tools that I use when I develop. I'm not sure if this tool will validate though it may help you somehow since you are working on email. https://www.mail-tester.com/ helped me address some issues related to email (it's not related to this issue).
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
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.
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