Jump to content

Receiving Email Via PHP


ShogunWarrior

Recommended Posts

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
Link to comment
Share on other sites

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 [url=http://sourceforge.net/projects/phpsmtpd/]this[/url] though, should you insist on using emails. Did look into it though.
Link to comment
Share on other sites

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 [b]notify@site.com[/b] 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.
Link to comment
Share on other sites

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

[code]<a href='http://www.mydomain.com/the_form.php?id=12345'>click here</a>[/code]
Link to comment
Share on other sites

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 [b]feedback@site.com[/b] 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.
Link to comment
Share on other sites

[quote author=AndyB link=topic=104888.msg418939#msg418939 date=1156089711]
.. 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' ....

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

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?
Link to comment
Share on other sites

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.

[url=http://www.phpclasses.org/browse/package/1120.html]Here's[/url] 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/pecl/tutorials/sockets.php
Link to comment
Share on other sites

Yeah, I see where you're coming from 448191, using option1 I could have an email address like [b]feedback@notify.site.com[/b] and notify.site.com would actually be my PHP socket server which would receive the SMTP connection and know that [b]feedback[/b] 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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=ShogunWarrior link=topic=104888.msg419062#msg419062 date=1156105376]
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.
[/quote]
"Se[b]tt[/b]le, 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.

[quote author=ShogunWarrior link=topic=104888.msg419062#msg419062 date=1156105376]
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?
[/quote]

This is exactly what I meant when I said this:
[quote]That [b]might [/b]not be a problem at all, as long as imcoming connections use a higher port. [b]How to set that up?[/b] I don't have a clue.  :-\[/quote]

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?
Link to comment
Share on other sites

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:
[b]nlo@feedback.site.com[/b] would be filed under NLO and [b]jbl@feedback.site.com[/b] 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.
Link to comment
Share on other sites

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 [b]K[/b]eep [b]I[/b]t [b]S[/b]imple [b]S[/b]tupid is what is often needed.....
Link to comment
Share on other sites

[quote author=ShogunWarrior link=topic=104888.msg419107#msg419107 date=1156113739]
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.
[/quote]

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.
Link to comment
Share on other sites

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......
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

OK. You go to a site and see those options....

The latter requires you reading /cut n pasting an email address.  You HAVE ABSOLUTLEY NO idea how the information is going to come through. They think 'god I have to type this myself'.

Bottom line is there has to be some interaction between you better to have a link saying register that links to a form or recieve registration form via e-mail....

the reason that registartion forms are on-line is that you have control and the visitor doesn't have to do too much.  What you think they will like is IMO what they don't want. People are lazy they want to do as little work to get the maximum benefit - to get what you want you have to make sure its easy for them to do AND that you can use what they give. Just sending an e-mail will not give you that unless you have scripted A.I. in php.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.