how to extract replied email from original email - php

I am using imap to grap emails and insert them into a mysql database. But I don't want the original email included.
So lets say I send out an email that contains this:
Hello, How is the weather.
And then you respond
The weather is great
The email that php reads from Imap looks like this:
The weather is great
Hello, How is the weather.
Is there away to remove the orginal message, "Hello, How is the weather."?
I am not to savvy on this stuff and will probably need things explained very clearly. Thanks!

Nope. Since there is no standard way to reply (i.e., some people use >, some use |, some top-post, some intermix, some attach) there's no way to accurately and reliably pull out the original. You could fiddle with regexp to catch some of the more common cases, but I think you'll find that route to be far more trouble than it's worth.

To my knowledge, there is no foolproof way to do this. When you reply to an email, the MUA has the right to reformat the original message as it sees fit. For example, Outlook may convert the original text to HTML and add invisible tags.
If you have access to the whole email thread, you may be able to find the original message based on subject or any hints that the headers give you.
This is all AFAIK, so someone please correct me if I am wrong.

You could do what Gmail does when you are sending multiple emails back and forth with the same subject.
Just keep a copy of the email that you send and then when you get a reply, look for similar text or as Gmail refers to it: "quoted text".
Or you can look for certain patterns based on their email domain. However, there is no answer that will work 100% of the time; it is all just based on your best guess.

Related

Catch Email and Its Attachments PHP

What I am trying to do is, lets say, our users send an email to help#supportmysite.com, then as soon as help#supportmysite.com receives an email, it checks the parts of the email and gets the title, email from whom we got the mail, text body, and the attachments in it.
How can i go about making a system like that in PHP? This is my first time developing something like this. I haven't really ever fiddled with PHP mailer and other stuff related to mails, so need a little help :)
Thanks :D
Take a look at the PHP IMAP documentation. It can be used to retrieve various bits of information from emails so I think that'd be a good starting point for you.

'Raw-send' using PHPmailer?

I need to write a PHP script that can insert one (or more) additional header, and re-send it to another email address.
How would I do that using PHPmailer? I can't seem to find how to do a 'raw send' of email message (with additional headers inserted already).
Or, if PHPmailer can't do that, how do you recommend I do what I want?
After spelunking around, I decided to just use 'fsockopen' and adapt this PHP snippet (lots of bugs there).
Thanks for everyone commenting!

Edit an email subject line (IMAP)

I am trying to integrate IMAP email processing with another in house system that bases what it uses off of the subject line / email content.
We need to be able to change the text of the subject line before moving the email to a new folder. What/where would be a good place to start?
I've had a look around and it IS possible in a manual sense, via a thunderbird plugin or using outlook. I just can't seem to find a relevant example in PHP, or any other language for that matter. I also hear the idea is flakey at best as you need to modify the email content and upload it back to the imap server.
The outlook implementation seems to delete the original and save a new one to your IMAP folder on the server.
Side note: Yes I know it is a weird requirement, and although forwarding the email to ourselves then moving it is our fall back plan it is not much liked as it moves original headers useful for things like reply-all.
Any suggestions appreciated.
PS If I'm blind and there is something obvious I'm missing in the manual let me know.
Do you already have any code built to handle the email processing? IMAP subject line information is stored as a header so you would need to utilize the PHP functions of imap_headerinfo() and/or imap_fetchheader() depending on the functionality you're looking for to achieve this. You could have PHP check each message header and if it matches X format, remove the message, and create a new one with the appropriately modified header information.

Does the message part of the PHP mail() function need to be escaped?

I'm creating a feedback webpage. I want to just take a textarea of user input and ship it off to myself with the mail function. Is it safe to do the following:
mail("me#blah.com", "Feedback!", $_POST['feedback'], "From: myself#blah.com");
/* (to) (subject) (message) (headers) */
Or do I need to somehow escape or sanitize $_POST['feedback']?
Yes, you should sanitize it as a general practice, but there's not too much that can go wrong here, just make sure that in the headers you specify:
Content-Type: text/plain
Then no user can send anything "malicious" for you to click on, perform some sort of tracking, etc. (EDIT: without you knowing what it is, of course; what I mean is, no misleading links, 1x1 transparent GIFs or the like)
As stilstanding said - you still need to sanitize any user-supplied data.
I would also recommend using a script like PHPMailer to create and send email messages. This will take care of formatting the message securely and makes tasks like sending multipart html/text messages much easier:
http://sourceforge.net/projects/phpmailer/
Depends on who is receiving the message. E.g. if it's GMail, they have their fair arsenal of escaping anything there, and anyone will have a really hard time injecting anything into it. But say, if someone is going to look at the message from some webmail, and if that webmail is of an outdated version, someone with a bad intention might be able to do something bad.
If you are not going to check for <script> tags and such, you have to do no escaping. But you'd better encode your email in quoted-printable, for deliverability rather than security, a task which is best done by a good mail class/function like Zend_Mail. And you can use it stand-alone (i.e. without the other library components).
"Sanitize" is a kind of vague term. There are different types of encoding for different purposes.
If you're sending plain-text messages, I don't think you need to worry about encoding them. PHP's mail function should take care of any encoding needed.
If you're sending HTML messages, then you do need to HTML-encode all HTML attributes and content, regardless of whether the values come from the user or not.

code for take the link contained in the email

I wrote a code for fetching mail from the inbox in php. It is working well . But actually i need to take the link contained in the message.. I want to take the link address which are came in the message part. Can you help me for doing this..
Cant really help like this , too little information and sample .. but i would say use a regular expression to get the links out of the email .. this would vary ofcourse if the email is HTML or plain text etc.
If you have influence on the content of the emails, make them so that the link is always on a certain line. Then walk through the email until you reach that line, then use it.
If you don't have influence on the email, definitely use a regular expression.

Categories