How to implement a feature: Post to a forum via email? - php

I'm investigating the development of a new forum and found via user feedback that posting to the forum via their email client would be a very useful feature.
I'm wondering: is it possible? Can php check for an inbox folder and turn each email into a forum post? And if so, how to do that in a LAMP environment?

Setting up a script triggered by receipt of email to a particular address would be the simplest. That way you don't need to write a new cron script. You would want to create a special email address dedicated to receiving such emails. The thread could be identified by something in the subject that would be required. I'm not sure of your use case so that's the best I can suggest. And you could also require the email to contain something to identify the sender as one of your authenticated forum members. You might need to expose a secret token to them or something. That way if any spammy stuff gets in you can block the account of whoever is sending it.
I believe some opensource forum software has this built in, or has an add on you can install to do it. Even though you're doing your own, you might like to get these modules to see how they do it.

You can create a sceduled (crontab, windows scheduler) script which does the job (reads all the incoming files from SMTP drop folder and posts the messages). However, two questions arise:
how will you know which thread to post to?
how will you filter spam messages?

PHP has IMAP functions that can read POP3 mailboxes. You would want to combine those with a cron job for frequent polling.
Some mail servers also offer triggers to run when mail comes in - you could create a trigger that calls a PHP script, passing on all the details of the incoming message. If you have access to your server and can implement this, it might be easier to work with because you don't have to connect to the POP mailbox yourself in your script.
If you have a POP3 mail box with good spam checking, this might work - although you would, as Naivists says, of course have to find a method how to identify the target thread reliably.

Related

How do I send an email from a contact form on a website, on a server that uses cPanel?

I’m making new a static website for a client, and they are wanting a contact form that a user could fill out and it will send the enquiry to the clients email.
The client is currently using cPanel as their web host manager, on what I believe is an Apache server. I don’t actually know what server side language that it would be using as I don’t have access to their cPanel account.
I was initially thinking I could create a PHP script that would use the mail() function, and I would put that into the public_html folder with the rest of the static site, and so when the user submits a POST request, the forms action will call that PHP script.
But from what I’ve read, that isn’t the best way to go about it and instead I should be using an Simple Mail Transfer Protocol (SMTP). I have a vague idea of what this is from googling, but have no idea how to implement such a thing.
I’m typically used to creating sites with React.js/ Gatsby.js, Node.js, Netlify, and so I’ve had no experience with cPanel and the like.
Has anyone done something similar or has any advice? Any thing will be much appreciated!
I think the first step is to get many information as you could about the available server structure. It is very hard to elaborate on any good solution without having control and/or knowledge about the server-side: maybe a tool or resource you use for implementing an SMTP-based solution could not be available to you when deploying it.
You could try to argue with your client on the importance of knowing the server structure. Use arguments on quality of service, security, among others.
If you get the server information, consider use PHPMailer if the server provides you with the required resources and dependencies.
If you could not have that information, the solution you think first ( use mail() ) will probably work. Great discussions about the topic occurred here.
However, firstly ensure that the server offers email sending service. It would be hard if it does not offer that service, but it is a possibility.

Upload/update Website by E-Mail

I tried to search for this problem, but there are alway things like "i don't recomment to use E-Mail for that", and then there are other solutions. So, I'm here and ask u:
I need a way to upload/generate files(.html/.php) on my Website via E-Mail. And it realy need to be E-Mail, because E-Mail will be the only possible way for me to communicate(google: winlink) with the internet in the next month/year.
But I have no idea how i could do this. I'm looking here for some ideas/ways/tipps for what i should look, or better a good and ready solution.
It should be possible to send a E-Mail with a text, and this needs to be placed in a .html or .php file on my webspace. And good would be, if I'm able to give the name of the file ... and then replace or generate it.
I hope there is a solution in php ... but if sql is necessary it ok too(I'm going to learn it next, too).
MfG Torben
If you have the opportunity to setup a mail server, you can handle stuff like this with some effort with sieve (depending on subject, content or from address) and dovecot. Here's a tutorial in german for setting up a mail server: Mail-Server
Once setup, you can write a program which is called by sieve and is running the code required for what you desire. Please note carefully that this will never be
a) save
b) failure prove
If you can live with those side effects, feel free to go with it!
SIDENOTE:
Better solution might be: Wherever you have access to the internet, you can access the internet and your site/server to.
For the WinLink and Sailmail community, SailBlog offers a blog web space, which is editable via E-Mail. This feature is integrated to the GUI of Airmail (WinLink client for PACTOR, Telnet, Packet and also WINMOR using BPQ32 TCP/IP virtual serial port emulation). Sadly, the Post By Email feature requires an Premium Basic account.
But what about setting up a new E-Mail account, looking for a Perl or PHP library that can handle E-Mail and MIME attachments and running a script on your server, which checks for new E-Mails (new Upload files, directory listing request, Upload files etc.)

How to automatically put email content on the webpage

I've been receiving a lot of spam emails and I want to post them on my website to serve as a warning for others not to believe it. Since I'm too lazy to login to my email account and check. I'm thinking to write a script in php (since this is the only one i know)that logs in to the mailbox at regular intervals, retrieves the content of the e-mails and then post it to my website automatically. Any suggestions on what would be the best approach to accomplish this task? Do I have to do PHP socket programming?
Thanks
I suppose your emails are filed under a Spam or Junk folder. You may want to set up a Cron job that runs periodically to read via IMAP or POP.
You may want to check what're the protocols supported by your service provider and where exactly you want to post the information to - a database or HTTP POST.

How do I receive email and process it in a web application

I have set up an email id my PHP web application. Users will send emails to this id.
I want to process these emails in the application. Ho do I go about doing this?
Thanks in advance.
I recently worked on a project that required parsing of email from gmail and updating database with certain values based on the contents of the email. I used the ezcMail (now) Zeta Components library to connect to the mail server and parse the emails.
The strategy I adopted was to filter all interesting incoming mail with a label "unprocessed". Run the PHP script via a crontab every 15 minutes. The script would connect to the mail server and open the IMAP unprocessed folder and parse each email. After inserting the interesting values into the database, the script moves the files to another IMAP folder "Proccessed".
I also found IMAP to be better than POP for this sort of processing.
Recently I wanted to be able to receive emails immediately in something I was making so I did some research (I came looking on this question here too actually) and I ended up finding Google App Engine to be pretty helpful. It has an api you can use to receive and process emails sent to ____#yourapp.appspotmail.com. I know that it doesn't really seem helpful since you probably don't want your app on App Engine and you want to receive emails at yourdomain.tld, but with a little setup you can get what you want.
My basic setup is like this:
User sends email to user_id#mydomain.tld (an email address that doesn't actually exist)
mydomain.tld has a catchall email address that forwards to inbox#GAEapp.appspotmail.com
GAEapp (a tiny app on app engine) receives the email, processes it out, and sends a post request with relevant stuff to mydomain.tld
So basically you can make a little GAE app that works like a go between to grab the emails. Even with the redirect it'll work out ok, the email will be fine.
Also I decided to learn me some django and I made a free app called Emailization that will basically do that for you. You create a recipient like ___#emailization.com and give a URL to POST to. Anything sent to that address gets POSTed to you URL. You can make a catchall on your domain that forwards to that emailization recipient and you'll get email through the catchall too!
or you can see a small GAE app I made that you can setup yourself that does the same thing.
Hope that helps somebody!
Use procmail if it is installed on your system. Put these lines in a .procmailrc file in the home directory of the user who receives the e-mail.
:0
| /path/to/your/script.php
Or you can also use a .forward file containing
"|/path/to/your/script.php"
Procmail has the advantage that it allows you to deal with more complicated filtering if your application ever requires it.
Your script.php file will read the headers and body of the e-mail from stdin.
Check out fMailbox. It does not require any non-standard extensions (such as imap) and has been tested with various servers, attachments, multipart messages, SSL, and more.
I suggest using Zend_Mail component of Zend Framework.
There is a great library: Try this: http://code.google.com/p/php-imap
You need to implement an email client in Php. This is probably going to be a POP client.
This code would query the POP server containing your email, download it, and then you could parse it as needed.
A quick google search of "POP client php" has revealed a vast array of different options. Its hard to tell if there's really "The One True PHP POP Library", otherwise I'd include it here. If you are using a preexisting framework, you may wish to check to see its level of POP support, otherwise check the google results above and take your pick. Or it may just be easiest (and most educational :) ) to roll your own.
There are a number of hosted solutions that will accept email for your domain and then post it a script on your website. Most of these will handle the parsing of the messages for you (separating the attachments, "to" "from" and other addresses, etc).
You just create a script that receives a FORM POST and does whatever you need with it.
Mailgun
CloudMailin
You can also look at Mandrill (by MailChimp), SendGrid, and PostMarkApp.
Hosted solutions as Travis Austin suggested work well.
If you are looking for a self-hosted one, you can have a look at the Mailin module allows you to receive emails, parse them and post them to a webhook of your choice.It also checks the dkim and spf, computes a spamassassin score and determines the message language.
I don't know if it will suit your needs since it is written in node.js, but the more options you have, the better. (Disclaimer: I am the maintainer of Mailin)
There is a great tutorial for this here:
http://www.evolt.org/incoming_mail_and_php
which covers how to have the emails forwarded directly to your script, which your script reads via stdin (fopen, fread, etc.) The tutorial code even does basic parsing of the header/body for you.
If you want to avoid reaching out over POP or IMAP to another server to pull-down the email, you can add a 'hook' into the email receive process on some SMTP server you set up (possibly the same php server). Then just have the destination email handled by this server.
Here is an example with postfix, but similar things are possible with sendmail as well.
http://www.adkap.com/autoresponder.html

Create a link between an email and my web-app?

I've seen on site like flickr or brightkite, a personnal email is provided to the users.
If the user mail somethin to this adresse, the content is posted on his public profile.
How can I do that on a web application ?
There are two ways to do this, as I see it:
First, you can use an existing SMTP server/email box system and, on an interval, pull the messages from that mail box using POP3 or IMAP to insert stuff into your database/system.
Alternatively, you can write an implementation of SMTP that will accept email messages coming in and perform your custom logic to put data into your database/system instead of into a mailbox. This is, ultimately, a cleaner design that will have much less overhead. In fact, there may be an SMTP server implementation out there somewhere already that will allow you to inject this kind of custom logic (I'll edit if I can find one).
Personally, I'd go with the second option. This will give you much more control over what's going on in your system and it will have an overall cleaner design.
Good luck!
Edit: It's not PHP, but JAMES from Apache is a Java mail server that allows you to inject custom mail processing units (called mailets) to handle mail processing. You could write such a mailet that will process email messages and put the updates in your database instead of a mailbox. There may be other projects that implement this kind of design, so it's worth a look.
Edit again: Ooo... here's an open source php SMTP server on SourceForge. I don't know that you can inject custom logic, but you can always edit the source and make it do what you want! (If you insist on PHP anyway)
You write a lot of code between your app and the php imagp/pop3 functions.
There are several free mail servers available that support using MySQL or any other database as a storage backend and requires only configuration to do so. If you're not comfortable customizing an existing mail server or writing your own, I'd go with that solution. It's several orders of magnitude faster than using POP3 or IMAP to communicate with the mail server.
Flickr has published their methods for doing exactly this in the book Building Scalable Websites. The whole of chapter 6 is dedicated to the topic. You don't need a non-standard MTA, as mentioned above. The default MTAs will work fine (sendmail, qmail, postfix, exim, etc.). All you have to do is edit /etc/aliases. /etc/aliases can be used to set a mailbox to pass all email to a script.
I strongly recommend reading through this chapter, as it goes on to outline a lot of the common issues you'll run into doing exactly this -- parsing attachments, coping with email from mobile devices (which frequently includes bad/quirky headers), doing authorization correctly, etc.

Categories