Solved

Receiving emails with PHP

Posted on 2011-02-20
13
605 Views
Last Modified: 2012-05-11
Hi. I would like to learn how to receive emails with PHP. I tried looking at some articles and found words like POP, IMAP, and such. I don't understand these. Can anyone point me at the right direction.

What I want to happen are these...
1.) set up an address like "sendhere@mydomain.com" that will receive the emails.
2.) read/process the emails...

My primary concern is no.1. How do I do that? Do I need to setup a mailbox? Help me please. Thanks! :)
0
Comment
Question by:rationalboss
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34936912
Before you start worrying about receiving emails with PHP you had better get a good understanding of how email works. It varies massively from system to system and there is no one way to do this.

For a start - are you talking about interfacing externally into someone else's email systems or are you talking about interfacing into a local system's mail system? The external system may provide API access or it may not. The internal system could be Windows or Linux. It could be in mbox format, Maildir format, Qmail format, etc..... Your PHP may or may not have direct access to the mailboxes

You would have to describe you existing system in detail before anyone can give you advice. Eve something as simple as creating a mailbox might require creating an alias entry in Exim or Postfix's configuration, creating a system account with the correct file/directory structure, etc.

Sorry to be a wet blanket, but this isn't something trivial.
0
 
LVL 8

Author Comment

by:rationalboss
ID: 34936941
I don't really care whether I interface into someone else's system or not as long as I'm able to retrieve the emails. Can you tell me what specific descriptions of my system do I need to give? Thanks! :)
0
 
LVL 11

Accepted Solution

by:
Ovid Burke earned 250 total points
ID: 34936971
This article would help if you are on Linux with Cpanel:
http://harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script/
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34937469
1) If you already purchased yourdomain.com and also a hosting package for the domain, usually it comes with email accounts where you can simply add email like you@yourdomain.com from the hosting panel. You dont need to bother thinking of how PHP works to receive emails because easily you can access it (sent/receive mails) from your webmail account that comes from the hosting package.

2) POP3, SMTP, will be used if you want to configure your email client software (e.g. Outlook) so you can sent/receive emails from your own PC.

3) Explanation above should be working fine if what you're trying to make is you@yourdomain.com.
If you ever want to make email like you@someoneelsedomain.com then forget about it.
Perhaps you would let know where you order your hosting so we can check what comes with the hosting package .. ?
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34937652
Basically you need to somehow set up a forwarding account to a php script, how that works depends on largely your mail server software - what are you using, qmail, sendmail? Linux/windows? Any server management software?

What you need is called a mail pipe. Ultimitely you set an email account to forward to the script like this:

|/var/www/vhosts/mydomain.com/incomingmail/myscript.php

But most control panel software won't allow this you have to go in with root access and manually alter settings. I got it working on linux/plesk with qmail quite easily though once I'd found what I needed to edit. Let us know your os and mail server. If you don't have root access I doubt you'll have to get in touch with your host but I somehow doubt normal hosts will allow this kind of thing..





0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 34937735
The thing you need is called an Email Pipe.  Here is my teaching example with some comments.  I use this design pattern successfully to receive and parse email messages and to send the parsed data to other scripts for asynchronous processing.  I have a LAMP stack with cPanel.

HTH, ~Ray
#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = '';


// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Author Comment

by:rationalboss
ID: 34937751
How do I do this in CPanel?
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34937919
Rays code above will read the email into php, you can read it into a variable and do what you want with it from there. I think he may have answered my original question on this a while ago actually.

Perhaps Ray can help you with this too as I don't have cpanel, however I can tell you what I did under plesk which may help.

- Went to my domain in the control panel, set up a forwarding account for the email address I wanted to forward to the script.

- Tried to enter this as the email address to forward to |/var/www/vhosts/mydomain.com/incomingmail/myscript.php - note the | at the beginning of the address - means to pipe the email to the script. Technically this is all you need to do, however Plesk wasn't having any of this it kept saying it wasn't a valid email address - of course it isn't! So  I set it to forward to "nonexistentaddress@mydomain.com"

- Then I logged into the server as root on the command line and did a search for "nonexistentaddress@mydomain.com" from the base directorylike this:
grep -lir "nonexistentaddress@mydomain.com" *

- I found this string in a file called /var/mailnames/mydomain/[email-address]/.qmail

- I then overwrote nonexistentemailaddress@mydomain.com with |/var/www/vhosts/mydomain.com/incomingmail/myscript.php in this file and saved it out.

- may have rebooted the web server (I can't remember) and it worked!
0
 
LVL 5

Expert Comment

by:onemadeye
ID: 34938041
@rationalboss:
Here's a video tutorial How to setup email accounts on Cpanel
http://www.cpanel.net/media/tutorials/emailaccounts.htm

Or in text version (I just retrieved from google) :
http://www.techiecorner.com/86/how-to-create-email-account-in-cpanel-hosting/
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34938819
See lines 10-15 in the code snippet above.  cPanel -> Email, etc.  It's been a while since I did this, but the setup was not that difficult.  It took me a while to realize that the script had to be marked executable!
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 34939600
Explaining the things....

First of all, let me tell you that IMAP, POP3 and all those weird words are the name of the protocols used to read e-mail boxes, each of them are represent a whole protocol, and which of them will be the best to use depends of the e-mail client that your server has.

Most of the e-mail servers nowaday support both protocols IMAP and POP3, but some of them only support one (most of the time POP3), so be sure to check which protocol will work for your server...

After that, I will suggest you to use a client class like:

http://www.phpclasses.org/package/2-PHP-Access-to-e-mail-mailboxes-using-the-POP3-protocol.html
http://www.phpclasses.org/package/2-PHP-Access-to-e-mail-mailboxes-using-the-POP3-protocol.html

Why to use a class?...

Well, talking about protocols, the phrase "Do not reinvent the wheel" got a great application, even more if you are not familiar with how the protocol works by itself. I have develop a couple of class to implement some e-mail protocols, and I can guarantee you this... you have to deal with status codes, authentication methods, and a bunch of thinks that are not "basic php"...

So, do not complicate your life, use a open source class, and in this way you will be able to focus on the really important parts of the project, like what to do after getting the e-mails...

Also if you would like to get more familiar with POP3 protocol, you could always take a look on:

http://en.wikipedia.org/wiki/Post_Office_Protocol

Or directly over the RFC for POP3 (in case that you really want get to know the inner-workings fo the protocol):

http://www.faqs.org/rfcs/rfc1939.html

Hope this helps!,
All the best,
VahHackman.
0
 
LVL 8

Author Closing Comment

by:rationalboss
ID: 34957694
"The Expert(s) provided an acceptable solution, or a link to an acceptable solution. May assign if you needed a bit more information to complete the task."
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34957982
What was wrong with this answer?  What questions remain unanswered?  Why did you mark the answer down a whole letter grade?  Work with us here, please!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now