Edit an email subject line (IMAP) - php

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.

Related

MIME Multipart Parser

The company I work for provides bulk-mailing functionality to our clients [double opt-in, not spam, I promise] and we get a figurative ton of reports back via Feedback Loops from AOL, Comcast, Yahoo, etc. These are generally from people that signed up, don't want it anymore, have been conditioned to not click 'Unsubscribe' links, [because "that's how the spammers get you"] and simply mark all the messages as spam.
Now, these FBL emails follow a specific format where the message is multipart, there are one or two text parts, and then the original message is attached, usually with all recipient information stripped out. This attached email is also multipart and contains the unsubscribe link, but the section in the attached email the link occurs in is quoted-printable encoded and the link is longer than what quoted-printable allows for in a line, so it get munged. Occasionally the section seems to get base64-encoded, I think it happens if the client is using a fancy language like chinese/japanese/etc.
What I need is a mime/multipart data parser that can give me these parts. PHP has oh so helpfully not implemented any form of multipart parser that I can find outside of what's internal to either their horrid IMAP functions, or internal to PHP itself which processes multipart form data.
Does anyone know of something I can use for this short of having to write my own? I had found one script, but it relies on old PECL functionality that relies on a custom-compilation of PHP which is not an option for this server.
TL;DR: PHP's imap_* functions will parse the parts of the message received from the server, but I need to parse the parts of an email attached to the email downloaded from the server.
This guy's script is ugly as sin, but it gets the job done:
http://www.phpclasses.org/package/3169-PHP-Decode-MIME-e-mail-messages.html

Update text on a website by an e-mail

My client is a restaurant that needs to change a paragraph section (<P>) every day for specials.
There are many people that will be handling it so I have to make it as easy a possible.
I don't think teaching the whole staff how to use a CMS is feasible, so I thought it would be a good idea to make something like an email service, that only updates that bit of text.
So in other words the staff would just have to send an email, and the server would somehow change the text on the HTML page for that day.
Can I do this in PHP code maybe?
I am also open to other ideas to something easy, like a simple login system to just change that bit of text.
I wouldn't recommend setting the text by e-mail. E-mail is an ugly, UGLY format to process, especially if it is sent by humans on every type of broken e-mail clients. The half of the e-mails will be invalid HTML, the other half will be tabulated unimaginably, the third half will contain signatures and there are so many more halves :)
And explaining the e-mail format you expect to the staff (utf-8 plain text with no signatures, etc...), and how to set it on their Outlook Expresses, Netscape Mails, and web clients you never even heard of, will be just as difficult as explaining a CMS.
What I would recommend is a simple form instead. If you open the form the current text could show up in a text field, and upon posting back the form you save it's contents on the server.
You would need to store this text somewhere. There is very few servers that host web applications without some form of database backends, so I'm pretty sure you have some kind of database to store your text in.
Also the form would need some kind of password protection. The easiest would be IMHO to password protect the folder where your php is. It's not too hard in Apache.
Check this link: http://www.groovypost.com/howto/htaccess-password-protect-apache-website-security/
I'm not familiar with your experience in PHP, but I hope you can make a form to edit a database record. If not, then please use google, there are tons of tutorials on it.
You could use imap extension http://php.net/manual/en/book.imap.php it allows you to read emails from email box. usualy, programmers create keywords that act like commands to script, for example, if title of email is match pattern UPDATE pageID then it will process email body as content for this page.
This script will be running with crontab, which is scheduler for unix OS. So you can run it every 1 hour for checking new mail.
maybe your client could send an excel sheet and you parse this email attachment on server side with php.
https://code.google.com/p/php-excel/
One option is to use a blogging platform to post the latest specials. You could then use PHP to grab the RSS output (last feed item) and populate the website. This would take care of the form, log in and security part. It also gives the client a running history.
(if you want to go this route I can post an rss reader php script to help you out)
Alternatively, if you decide to go the email route, put the text between something like this:
<!-- PUT PARAGRAPH HERE -->
Here is today's specials.
<!-- /PUT PARAGRAPH HERE -->
Can be anything really, but bookending it with something constant you can search for in the string will help avoid many of the issues mentioned by #SoonDead above. PHP can convert it into something consistent, but you'll need some php knowledge to make it work.

'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!

Is there a PHP script to mail forms for beginners for free?

I have a place where I want users to submit emails for newsletter subs and a place to submit an entire contact form. Zero php knowledge outside of know that it can do what I need.
There's a mail function built-in, take a look at
http://php.net/manual/en/function.mail.php
you will however, need to format the email body with the form data posted, of course.
Check out FormToEmail. From their site:
FormToEmail is a PHP form mail script.
It comes in a free version and a Pro
version. It processes web forms and
sends the contents of the form to you
by email. It will process any form. It
doesn't make forms but it comes with
HTML code for a basic contact form
that you can use on your website. It
is very secure and cannot be hijacked
by spammers. It is very simple to
install, you only need to add your
email address to it. Step-by-step
instructions written in plain English.
Actually, you don't need to do any php programming to create a such a setup. There are plenty of mailing list software apps available and you can grab a formmail script (like the one from Matt's Script Archive) and build the html with the right fields.
Use the mail function.
However be aware that you need to specify the correct headers yourself if you want to use html in your emails!
A common beginner's mistake is assuming you can simply use html tags with mail and it should work automatically.
Figured this was worth mentioning, since you are wanting to do a newsletter. If you try sending the email, and the raw html tags seem to appear in the message, this is your issue :)
As someone who is a PHP know-nothing, I often look for easy solutions to complex problems. Using a super simple PHP form can have some downsides such as getting massive amounts of spam if you don't have any measures to prevent that, setting up those measures is hard to do if you do not have any knowledge of PHP.
Here is what I found, there is a script here: http://www.dagondesign.com/articles/secure-php-form-mailer-script/
that you can download at no charge and the developer seems to have a lot guidance on how to install and use. I was able to set it up and get it to work.. not without having to spend quite of bit of time reading and trouble shooting. Hope that helps you.

Manipulating the content of an email message

I am looking for a solution that will enable me to connect to a mailbox, obtain an email, apply specific modifications to the email body (for example, change the content), and then forward the newly modified email to a new email address.
The trick is that such modification must not destroy the format and headers of the original email and I must not lose any attachments that were in the original email.
The sort of manipulation that will be performed will need to be done by an external process that knows the logic of my application.
The solution I am looking for can be an external software that can invoke some API for processing the content of the emails, or even API by itself that my code will invoke.
Our solution is currently based on PHP, but any other solution is also acceptable.
I started working with the Zend Mail library but I am running into problem having to understand the inner-workings of email formats. I wouldn't want to start messing around with the mime objects in the email format. I only want to alter the textual content of the message and keep the rest untouched.
http://php.net/manual/en/book.imap.php - functions that let you manipulate email systems.
What mail server are you using? In qmail its easy to process any incomming email. You can put any script in any language to process the lines of the email.
If you have IMAP access to your server you can use the php IMAP lib. http://www.php.net/manual/en/book.imap.php
I wrote a library as part of a larger open source app that may help you a bit. Its an object orientated wrapper around the PHP imap functions and can be found at google code.
Unfortunately this doesn't do exactly what you want. What in the message are you trying to change? I may be possible to just grab a raw version and specifically search out what you want to change, ignoring the whole mimetype processing altogether, and then just send the whole message along again.
Resending the email is simple enough, and this (small tutorial)* on sending email with attachments can refresh you on the basics (although most of what is in there you can skip as the attachments and mimetypes will already be built).
* I can't post the link because my reputation isn't high enough for two links in a single post, so I'll add it in a comment.

Categories