Jump to content


Photo

Receiving Email Via PHP


  • Please log in to reply
21 replies to this topic

#1 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 19 August 2006 - 08:20 PM

Hi,

I thought I should ask here and get some ideas as opposed to just trauling the internet, so here's the question:

________________________
I would like to be able to send an email to an email address (or multiple) and have PHP receive it and do some processing.
I'll give an example:
I did some work at a web design company where they used a Java system that they could send an email to,  e.g (notify@system.org) with a tracking ID and comment and it would add a comment to the project "ID".

I'm wondering what the best way to approach this is. Ideally I'd like to be able to implement it on a shared host environment, but if anyone knows if there's a service like this etc. then I could also look into it.

Thanks in advance,
David
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 19 August 2006 - 10:54 PM

I thought about having errors and messages for clients' domains emailed to my homepage application, wich is similar to what you're trying to do if I understand correctly.

I thought maybe I could use a script that mimics an smtp server. But then I thought it's probably easier and better NOT to mail them, but to just use custom sockets. You wouldn't need all the specifications the smtp protocol uses. You could even write a tiny PHP-GTK utilty that notifies you when new messages are recieved.

I did find this though, should you insist on using emails. Did look into it though.

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 20 August 2006 - 03:55 PM

Thanks for the reply man, what I want to do is allow a client to do the emailing, someone who doesn't need to log in, or know any other services, just use email.
They would just send a message to notify@site.com or whatever and it would do something for their account.

The specifics aren't that important, security etc., the main thing is whether this can be done, and if so, then the best way.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 20 August 2006 - 03:57 PM

your best option is to send them a html formatted e-mail with e a form in it that they must complete and have teh action of that form point to a script n your site to process the info.
follow me on twitter @PHPsycho

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 20 August 2006 - 04:01 PM

.. or send them an email with a clickable link containing a unique useful id to a php form on your site which then abstracts their 'id' ....

<a href='http://www.mydomain.com/the_form.php?id=12345'>click here</a>

Legend has it that reading the manual never killed anyone.
My site

#6 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 20 August 2006 - 04:10 PM

But they need to be able to send it anytime, without prompting.

I'm hopefully going to use this for a feedback system which also has a web form, but it is alot more attractive service if you can send an email since people are quite used to email and are alot more likely to fire up the email editor and send off an email to feedback@site.com as opposed to filling out a web form.

Maybe this isn't easy to do, but I'm sure it's possible, and would be a great boost.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#7 CircularStopSign

CircularStopSign
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 20 August 2006 - 04:51 PM

.. or send them an email with a clickable link containing a unique useful id to a php form on your site which then abstracts their 'id' ....

<a href='http://www.mydomain.com/the_form.php?id=12345'>click here</a>


what if you had the start page produce the link instead of it being sent to their email, that way there would be no prompting?

#8 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 06:24 PM

You want to know if it can be done, the answer is yes.

With php's low level socket functions, you create many different protocol servers including smtp and pop3.

This means you have two options:

1) Use sockets to create an smtp server. Client's email clients can connect to the server by a subdomain.
2) Use sockets to create a pop3 client. You use an existing smtp server, and have your script fetch the emails the clients send from that.

Option 1 is the best option, since you won't have to check for emails all the time, and processing of the content can begin immidately after the transfer is complete.

I wouldn't recommend actually creating the server scripts yourself unless you don't have a life and don't require sleep.

Here's a pop3 client script.

Can't find you a smtp server script though.

Here's a tut from zend about socket servers: http://www.zend.com/...als/sockets.php

#9 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 20 August 2006 - 06:40 PM

Yeah, I see where you're coming from 448191, using option1 I could have an email address like feedback@notify.site.com and notify.site.com would actually be my PHP socket server which would receive the SMTP connection and know that feedback was the "user" and then process from there.

Even if I did manage to program that, would I need full server access to the script to have it run as a daemon or could it be done on a shared server?

Thanks again.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#10 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 06:52 PM

Well, I didn't know this either, but I just read in the Zend tut I posted that you need root access to be able to use 
any port lower than 1024... That might not be a problem at all, as long as imcoming connections use a higher port. How to set that up? I don't have a clue.  :-\ Other than that, I don't see/know any limitations.

You really need a working script, go through every line, modify and strip where required. Maybe you should try PHPMailer @ phpclasses.org... I can't be sure it's an actual smtp server (I can't remember my phpclasses registration info), but it does look that way.

#11 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 07:20 PM

If you can't find a working smtp server script, I suggest you seddle for a pop3 client script. Configuration would probably be a lot easier.

#12 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 20 August 2006 - 08:22 PM

I would settle, but most hosts, namely the one I plan to use set a limit on cron jobs on this host's limit is every 15 minutes, too infrequent for my purpose.
0.5->1 minutes is more like the time I need.

As a note, do you think if someone was sending an email to the PHP server, how would it know to connect to the PHP SMTP server on a port higher than 25?
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#13 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 20 August 2006 - 08:50 PM

the one thing you are missing here is the data......

how are you going to ensure that the correct information has been sent? how are you going to ensure that it will be in the correct format? how are you going to ensure that something I haven't thought of here nd is important is not entered by the sender?????

You can of course write a few regex's to pull data out provided that the sender has put stuff in like

name: Bill
company: Microsoft

but what if they put (by accident)

name: Microsoft
company: Bill

For this reason you would be best advised to use the methods that I or Andy suggest.
follow me on twitter @PHPsycho

#14 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 09:15 PM

I would settle, but most hosts, namely the one I plan to use set a limit on cron jobs on this host's limit is every 15 minutes, too infrequent for my purpose.
0.5->1 minutes is more like the time I need.

"Settle, that's what I meant.. :P I'm sorry, I'm just a stupid Dutch guy.

Anyway, I didn't mean you should use cron jobs. Just a continuously looping script that checks for new email every minute or so.

As a note, do you think if someone was sending an email to the PHP server, how would it know to connect to the PHP SMTP server on a port higher than 25?


This is exactly what I meant when I said this:

That might not be a problem at all, as long as imcoming connections use a higher port. How to set that up? I don't have a clue.  :-\


BTW, you need a port higher than 1024.

I thought of something obvious ::)... If sockets can't use ports lower than 1024 without root access, the POP3 script is pretty useless right now as well.

On the other hand, there are smtp servers (for example mail.gmx.net) wich use different ports. I wonder if they only use the 'odd' port for receiving emails from clients to send... I really don't see how one smtp server knows on wich port the other smtp server expects connections... I'm hardly an expert on that subject.

ToonMariner makes a valid point. How were you planning on imposing formatting on clients' emails?

#15 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 20 August 2006 - 10:42 PM

Hm.. the thing about the constantly looping script is I don't know how you'd do that on a shared host.
If I had shell access and could run a PHP script as a command line script then it could do whatever, and check every minute etc, no problem.
However I'm not going to run it on a box I have access to, it will be shared hosted.

The formatting was simply going to be the body content.
I was going to use separate user parts for the different clients, so:
nlo@feedback.site.com would be filed under NLO and jbl@feedback.site.com would be filed under JBL.
Of course, injection testing, length checking etc. would all be done so it would be safe.

However, I'm thinking this mightn't be very practical or easy.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#16 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 01:07 AM

Shogun, sorry for banging on but the html form as the email or e-mail the link is truely the option for you....

Why?

You are relying on the user replying or even worse typing in an e-mail address....

Because you can control how the user submits data (simply having 2 e-mail addresses does in no-way ensure that you can extract all the data you need - the user WILL do something you don't expect THEY WILL!!!!!!!!!!! the body may contain your information but in what order? and how do you know everyone will stick to that order/format?)

AND

The information is processed instantly - don't have to worry about a cron job or shell scripts.

not calling anyone stupid but the addage Keep It Simple Stupid is what is often needed.....
follow me on twitter @PHPsycho

#17 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 21 August 2006 - 04:58 AM

Hm.. the thing about the constantly looping script is I don't know how you'd do that on a shared host.
If I had shell access and could run a PHP script as a command line script then it could do whatever, and check every minute etc, no problem.
However I'm not going to run it on a box I have access to, it will be shared hosted.


I see what you mean. Only thing I can think of (but it's hardly a solution) is to have a client on a machine you do have full access to request the script up to maximum execution time and then re-request it.

But then again, it may not need to run continiously. If a client requests access to notify.site.com:25 (assuming you can somehow magicly overcome the port problem) Apache will execute it's index page, or not?

Again, ToonMariner makes valid points.

#18 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 09:30 AM

I think the fact that what AndyB and I suggest (variations on a theme - but Andy's is easier to police) doesn't need any further discussion nor faces any issues with which ports you have available and already provides the alternative to infinitely looping scripts etc etc. should speak volumes......
follow me on twitter @PHPsycho

#19 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 21 August 2006 - 02:45 PM

You see I'm implementing a form-based interface anyway by default, this thing with the email is to add an extra level of easiness to it.
I think you get a group of people who won't be bothered going to a URL by allowing emails to an email address.

Anyway it's not essential but would be great.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#20 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 04:25 PM

But putting that form in an email could help - following a link can be boring for some but a form there waiting for them can seem 'convinient'
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users