Can you receive emails through PHP? - php

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.

Related

How to call php file when we receive a mail

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!

How to handle incoming mails?

We are developing a Client Support System.
So we need to handle every mail once we get it.
How to configure Exim? When the mail is received. It has to be handle (in php). For example it has to be inserted in DB.
You need to configure exim to pipe the mail to a php script.
Here is a tutorial on how to achieve this: http://www.activecampaign.com/support/tt/kb/article/help-desk/version-25x-26x/setting-up-supporttrio/setting-up-your-server-to-forward-email-to-the-pipe-script
The php script you "pipe" the mail to will receive the emails content via stdin, which enables you to insert it into the db.

PHP E-Mail Client - Overcoming slow send/receive

I have developed a CRM that is used with a marketing website similar to manta.com. The CRM has a built-in email client. Basically it matches names and emails to addresses on file, and if the email is from a customer it loads their account information next to the email.
The entire system works, but is in some cases slow. Because each "region" of the country is managed by a different company, the software connects to several different mail servers, all of which behave differently.
In some cases (mostly with godaddy servers) it takes a very long time to send an email. It also takes a long time to load the inbox if they have a lot of messages (500+), even if it is paged.
I am using pear Mail::Factory smtp to send, and the built in php imap functions to receive.
I am thinking for outgoing messages I will just store the emails into a db and then have a cron script send it out, then they can send their email and move on right away.
Does anyone have any ideas/concepts for handling large inboxes via IMAP and php? I will have no control over the mail servers themselves, but have full control over the server that the CRM is on.
I am not looking for code, just concepts if you have them.
It is Linux (ubuntu), apache, mysql, php. I can use Zend if needed.
It looks like a combination of caching the emails and using the imap_fetch_overview function which omits the body of the email. This seems to eliminate any issues with the lag. I can also use it to page the results so I am only loading a small number (25-50) at a time. Thanks for the help.

How can I create a mail server?

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.

To detect spam messages using imap php

I am retriving my mails using php imap function.
I am retriving all messages from server and i need to know how to detect messages which are spam in my received mails.
Question: How to Detect spam in emails received using imap php
Usually that is the job of your mail server admin, if you are him you will need to use a 3rd party software like SpamAssassin. The choise is up to you and your specific implementation of a mail server.

Categories