I have a feature in my app that handles and parses incoming emails.
The mails come in through the usual method with exim as a .forward file:
| /path/to/php /path/to/mail/handler.php
This sends RFC822 formatted text to my handler, which uses a parser to break it down and hand over to the rest of my app.
New feature needs to be added to forward any mails to foo#mysystem.com along to bar#othersystem.com
I'm trying to figure out a way to do this the most simple way. The long way is to take the parsed message, modify the To: address and send on as a new mail.
So my question is: What's the simple way to "forward" an RFC822 formatted message? Is there a way of telling exim to forward the mail instead?
You should have Exim do this, not PHP. Mail servers are all about forwarding email. There should be a way to add multiple addresses in your .forward file. If you can't figure out how, https://serverfault.com/ is the place to ask.
Related
I have special e-mail accounts on my web server (#mydomain.com). I want to run a php script automatically when one of these accounts get a new e-mail. For example: when info#mydomain.com address receives a mail, I want to run "receivedMail.php" file and read this new e-mail. I don't know where I will start or how can I do this.
You're wanting to pipe email received for a specific email address to a PHP script.
If your webhost has cPanel, this makes it very easy to setup. See:
http://kb.siteground.com/how_to_pipe_an_email_to_a_php_script/
Alternatively, if you don't have cPanel but have Exim mailserver, this will guide you on how to do this:
http://www.phpshare.org/articles/Piping-Incoming-Mail-with-PHP
Hope this helps!
You have to create a cron job calling your php file every 1 minute for exemple.
The script have to the derver and read the email.
This has nothing to do with web servers directly, Since web servers do not receive emails. So no .htaccess style files come into play. The email is received by a mail server, so that is where you have to get active. Two strategies are possible:
you use the possibilities to trigger an action as offered by your
mail server (typically the smtp server you operate). That obviously
depends on which mail server you operate, different software offers
different features.
you poll those email accounts on a regular base, using a standard protocol like POP3 or IMAP4. You can do this using any suitable client. When a new message is found whilst polling the account, then you trigger the action you wish.
Option 2. is probably easier to start with. So give it a try: create a php file which polls your email server. You can use the imap php extion for this, it supports all important email protocols. The extensions allows to easily detect and retrieve new messages. For each such message you can implement whatever action you wish. All that is left is to run this script on a regular base, say every 3 minutes, which is typically done using a cron system. There are many examples for this out there on google...
Have fun!
I am developing a dynamic mail forwarding component for a website.
The concept is that
- a user will send an email with arbitrary content to a special email address on the server
- based on the "to" and "subject" line content, the system will forward the email to a group of (external) email addresses that are pulled from a mysql database
I'm running on a shared webhost (Bluehost), so I don't think I have any advanced access to the mail infrastructure to do this for me (no .forward files, for example). The target email list is dynamic, so I can't use cPanel to set up static forwarding.
I have the incoming mail coming to a PHP script, but the processing looks challenging, especially for the MIME parts - multipart, attachments, etc. I have looked at the PEAR Mail / MIME stuff, but it seems overkill to completely decode and re-encode the msg...
Any tips?
I use phpMailer for sending email from PHP.
I implemented exactly the same concept in mod_perl a few years back. Be aware that much of your outgoing email will likely be blocked if you send a significant volume. And if your system can be hacked (and that sounds at least plausible), spammers may find it and use your system, which would then lead to all your outgoing email being blocked.
Sending email is more difficult than it should be (if considering only the tech) thanks to spam and spam filters. These days I often find it easier to write code which uses a third-party service to actually send the emails, feeding the addresses over an API. I've had good luck with MailChimp and Contactology APIs recently.
EDIT: as for the incoming email: If you weren't in a shared hosting situation, it wouldn't be that hard - simply capture it, strip the headers, and pipe it back out to a new address. I'd have to agree with Conrad I guess on that point -- a shared host PHP script seems like a rather sub-optimal environment to try and solve this problem. If you can't use system() to pipe it back out directly the the mail infrastructure, I think you do have to completely parse the incoming email and reassemble it.
I know you can send emails with PHP but, can you receive emails through PHP?
You can pipe incoming mails into a PHP script so you can process it directly. Evolt has an article on how to setup something like that. This can be useful if you want to activate scripts programmatically by sending Emails to it (like responses to a newsletter mail that unsubscribes the user).
If you just want to read mails using PHP, PHP has native functions to talk to IMAP, NNTP and POP mailboxes.
Take a look at http://cloudmailin.com it takes away a lot of the hassle involved with receiving the email and will send it directly to your app via an HTTP Post. We have quite a few php users using the system to receive email.
You could write a mail server in PHP that binds to a port and listens for incoming email.
But PHP is not the language I would recommend for tasks like this, and it would also be a hugely complex undertaking.
You can hook into an existing mail server as callback script, or periodically query a mail server via POP or IMAP. The latter option is the most common: run a PHP script that processes an email account via a cron task in intervals. See http://php.net/imap.
There is a great library that is based on IMAP extension: http://code.google.com/p/php-imap
Only a mail server can receive e-mails. You could read mail box formats (such as mbox or Maildir) to read e-mail using PHP.
PHP scripts functioning as IMAP/POP3 servers can receive e-mail sent to them.
I would like to have a server that is able to receive emails. Then I want to use PHP to program the way the emails are shown to the users. Can I do it purely with PHP? I mean, it is not a problem to send emails from PHP but I do not know if I can receive emails by PHP? (In a way PHP receives POST requests).
ADDED
As a response to the first answer, I would like to specify that it looks like I need an SMTP server. I want to be able to communicate with the SMTP server in a programmatic way. For example, I want to have a possibility to "tell" to the SMTP server to create a new e-mail address. I also need to know where incoming emails stored and in what format. For example, how I can extract the "sender", "cc", "bcc" from the file corresponding to the received mail.
would like to have a sever that is able to receive e-mails.
If you are writing it from scratch then you'll need the specification for SMTP. I would advise very strongly against this. SMTP servers are hard to write, and there are several really good open source solutions out there.
My understanding of PHP is that it does very poorly when it comes to multithreading, so it probably isn't a good solution for this problem.
Than I want to use PHP to program the way the mails are shown to the users
Servers that receive mails do not typically show them to users. They usually store them in a standard way (such as Maildir or mbox) which other software (such as a local email client or an IMAP server) accesses.
The job of showing email to a user is belongs to email clients. Web based PHP web mail software includes SquirrelMail and RoundCube. AFAIK they both act as IMAP clients. See the IMAP specification.
As a response to the first answer, I would like to specify that it looks like I need a SMTP servers. I want to be able to communicate with the SMTP server in a programmatic way. For example, I want to have a possibility to "tell" to the SMTP server to create a new e-mail address.
Pick an SMTP server that runs on your OS. Read the instructions to find out how to configure delivery and accepted addresses. It usually comes down to manipulating text files.
I also need to know where incoming mails stored and in what format. For example, how I can extract the "sender", "cc", "bcc" from the file corresponding to the received mail.
Again. See the manual for the mailserver. Most will give you options about where to store the data and in what format.
Then you just need to decide if you are going to get PHP to dig into those directly, or use an IMAP server in between.
No, that is not easily possible. PHP is made for (stateless) http protocol, while a mail is sent in a conversation that is built up from various requests and responses.
It is possible to parse and process mails using PHP, but I would recommend installing a mailbox that you can read from PHP using POP3. Then, your PHP application can show and process mails from that mailbox.
i am searching for a way to read mail messages from a PHP application, including access to attachments etc. imap functions are not acceptable as a solution, as this application will handle mails with heavy attachments.
i have full access to the server's mail folder from php via filesystem. any thoughts?
I think you could use a combo of the Pear packages Mail_Mbox and Mail_mimeDecode. Use Mail_Mbox to read new mail from the inbox, one message at a time, and use Mail_mimeDecode to extract the attachements. All this will be done w/o IMAP. You can then save the read messages to a different mbox to keep the inbox clean.
Pear - Mail_Mbox
Pear - Mail_MimeDecode
I had a question like this a while back. See if any of the answers help you:
How to get email and their attachments from PHP
postfix + maildrop was the solution I ended up taking, It routes the emails through to a PHP script when it arrives and in my case, the PHP does something with the attachment. But I only needed to read each email once. If you need to be able to browse all the emails, you either need to store the results of maildrop or find another solution.
If you need to full access to all emails, POP and IMAP are popular choices because they do work. I'm not sure why you're against them.