I am trying to work out how to do something. It must be possible - I've seen something similar done in multiple systems (Twitter, facebook etc.)
The basic thing I want to do is - someone leaves a comment on my website. The comment goes to my email, as well as to my MySql database. I then reply directly on my email. The reply is saved to the database, and auto-approves the user's comment.
I can't work out how to speak to my MySql / PHP system directly from an email. Also - I can't work out how to set up the inboxes?
I am guessing that I'd need to set up mailserver rules so that emails sent to xxxx-response#mydomain.com are actually redirected to a php script, where xxxx is an identifier of the original comment?
Thanks for any light that you can shed...
Assuming you're on a unix system, you could set up an account for receiving the mail, e.g. "fred", and use a .forward in fred's home directory to pipe emails to your php script. e.g.
.forward:
|"/usr/bin/php /path/to/your/script.php"
which will invoke your script anytime an email arrives in fred's mailbox.
You can't 'talk to mysql from an email'. An email is a passive piece of text, nothing that can take actions.
The solution is based on something like this:
You have an email account and poll that accounts Inbox by a php script in a periodical manner, by using wget or similar in combinition with a crontab entry. You can also create an event driven approach, but that is more complex and does not really offer advantages. For all messages found inside that inbox you check if it matches any previous entry in the database. You can do that since you have access to the messages headers and body (the text) by php. That is all you need. Typically the connection between two messages is done by evaluating the 'In-reply-to' header that is set by email programs when you press the reply button. It is like a reference.
The access to the accounts inbox is done by standard mail access protocols, typically pop3 or better imap4. When the mail server is not on the same system then abviously you want to add ssl encryption too. php's imap extension is a very good start for implementing such a client, it supports several protocols.
Related
I would like to know how to setup a system in php that I could send emails to and the email content would poblish on my blog as a new post and the email subject would be the new post title.
Am not using word press or any of those blogging platforms am running my own blogging script and a php 5.x version.
I don't even know how to get started on this kind of programming challenge please any one with any ideas is welcomed.
Joe
You would need to create a script that is able to read from your email account. Php has the IMAP library that enables you to read email messages. Check this link to see how you could use the library.
You would need to schedule the script to be run at a periodic interval of time (eg. every 5 minutes). In Linux you would setup a cron job to do it.
So, I mean the script should be invoked periodically. If it finds a new message, it would then update your blog by taking the body of the email.
you need to set up a database that has all the tables and rows necessary to hold all the vital information about the post that people will send via email. then using MYSQL in your PHP file you need to connect to the database (host, username, and password) and then create your own query to enter the desired data to the database.
Example: if I want to upload a video to some video service website (such as Youtube) by email, I can send a video to an email address (e.g 1234567890#upload.youtube.com). The number is an upload code - It's a unique number and changes every time. Then Youtube will process and upload my video automatically.
How did they create the email address 1234567890#upload.youtube.com, and how did they process an email message that a user has submitted?
I'm looking for the basic functionality behind this kind of web-application.
I've never done this, but I have a basic idea of how this is done.
There's a mail server on upload.youtube.com waiting for any email no matter who's the recipient.
Once it gets a message it will pipe it to a script which can parse it and get the recipient address (that's the uploader's unique identifier).
Then it will look in the email for the attachment and get MIME code, base64-decode it and then write it to a file on a disk.
Then it will add an entry in the database with the user's unique identifier and the link to that uploaded content.
if you use cPanel you could use something like http://www.zubrag.com/scripts/cpanel-create-email-account.php to create the email accounts (use your own routines to generate the unique username etc) then you could use a script like http://garrettstjohn.com/entry/extracting-attachments-from-emails-with-php/ to get the attachments from the email.
Finally, I figured out what I want.
All I have to do is handle with catch-all email on my web control panel. In my case, I just create a new subdomain and set a catch-all email option on that subdomain.
You can do all of above in Cpanel easily - just create a subdomain and selected a radio box Pipe to a Program and fill your application path. That's it!
Unfortunately, I'm using DirectAdmin. I have to do more complicated than Cpanel. It can handle with catch-all email as well, but I have to use wildcard dns for my subdomain record first.
In addition, I found this useful article about how to Pipe / Send Email to PHP Script. This article will show you every step including a PHP script for using with email piping.
Thank you for all answer. It's very helpful.
That's nice to know that you have achieved this.
I am here to introduce you to a script that can help you. The script is dealing with the dynamic email addresses with PHP. You can see the script at github and there is a premium version of this software you can see a demo here. You can buy the script from codecanyon.
I was just wondering some hidden potential in a device I own, and after thinking a while, came up with this:
If an email is sent to "name#domain.com" (this would be specific to the device) from a specific sender, with a part of the body of the message containing a link, can I make a PHP script that accesses said account via POP3, detect emails sent to the address via a specific sender, and then download the file (link) in the email with string "http://www.domain.com/redirect.html?_encoding=" to a directory on my server?
Basically this:
specified sender emails "name#domain.com" with an email that contains a link specified above. PHP script (being called by a web browser) uses POP3 to download the link in the email from the specified sender, saves it to "C:\directory\filename.suffix" automatically?
Even better, is it possible without a PHP script, something that would automatically download the link when the email is received and save it to a directory on the local computer?
Thanks, I'm just trying to find if it is even possible and not waste my time.
Have a look at flourish fMailbox. You can easily access your emails via POP3 with that and check them for your criteria.
One disadvantage if you run the script regularly (e.g. with a cronjob): It's based on the email's 'uid', which probably means 'unique id' - but (at least in my case) it's not so unique at all and changes if you delete emails from the server. So in other words, it's not a unique id but describes the nth email, counting from the first one. So if you don't want to start from the beginning each time you check your mails, you a.) have to store your last 'uid' somewhere and start from there and b.) should not delete emails from the server.
For the rest of your question:
Look if there's a specific link in there: http://de.php.net/manual/en/function.strpos.php
Download the contents: http://de.php.net/manual/en/function.file-get-contents.php
Yes, it's possible. Click this link and scroll-down all the way to the bottom where a user left a comment regarding accessing POP3 folders:
http://php.net/manual/en/book.imap.php
I have a simple user signup form with a checkbox that lets users get a daily email notification if there was activity on any of their projects...much like Stack Overflow has a "Notify xxx#example.com daily of any new answers".
My current thinking to implement this in a LAMP environment is as follows:
In the user database, set a boolean value if the user wishes to receive a daily email.
If there is any project activity, the project gets updated with the current timestamp.
Each night (midnight), a PHP file is executed (likely through a cron job) that scans through the project database to identify which projects had activity that day. For projects with activity, the project owner name is selected and the user table is scanned to check if the user wishes to receive a daily email notification. If yes, add to a recipient list; else, ignore.
Questions / concerns I have that would appreciate some guidance on before I start to implement:
I'm in a shared hosting environment. What precautions do I need to take from being misidentified as spam either by the hosting company or the receiving mail servers?
Do I need to "chunk" out the recipient list (50 emails at a time) and email each group? Is this as simple as putting a sleep(30); between each call to mail()?
I'm using the CodeIgniter framework and will have the cron job call the appropriate function in a controller to run this at midnight. How do I limit calls from only the cron job to prevent some unauthorized user from calling this function from the browser?
Thanks.
If you do change the "From" header in php, make sure you change it to the domain that's hosted on that server. It looks suspicious when mail #a.com is being sent by b.com's servers.
I would send the emails individually foreach ($Users as $User)..., since this allows you to personalize the email contents. Even if you don't need to personalize emails now, you might want to later, and the support for it will already be there when you need it.
First, I would store the script outside of the web root. I'm not sure if CodeIgniter will let you do this, but there is no need for the script to ever be served by Apache. Cron doesn't care where the script is stored. Additionally, I've checked the time when the script is executed. If it's not midnight, then don't blast out the emails. Also, you could keep a log around and also check to make sure the emails haven't already been sent that day before sending.
1) Start with an SPF record and a DKIM if you can that lets mail servers know to expect email from your servers
2) First, you need to put the recipients in the BCC field so that it not each user has the email addresses of 49 other users on your system. One step further is to do each email separately, putting only the recipient in the TO field. This approach also allows your to tailor each email to the user (perhaps putting in "Hi [First name]".
3) Have the cron job something like this
wget http://localhost/send-emails
Then in your script, check $_SERVER and make sure you only allow requests from 127.0.0.1
About the third question: You can either use an .htaccess file to prevent access to that specific page or you can call your script in cron with a command line parameter and check for that variable in $argv.
1) The SPF record is the most important thing. Use the email from the domain so whatever#whatever.com, where whatever.com has the SPF records set correctly.
2) It's always good to throttle email, especially when first starting out. You should check your shared servers policies, which are normally 200-500/hour. Calculate how many seconds that comes to. For example 300/hour is 1 every 12 seconds. After a few weeks of sending good emails, you should be ok to send larger amounts.
3) You can have the cron file outside the webroot or limit access via .htaccess or another method.
i'm new in php. I want to send an email to someone and afterwards i need to check if this mail could be received. How do i do that? Hope you guys understand my problem ;).
Thanks in advance. Marc
That's a really sticky question. The only real way is to have PHP monitor an inbox to check for "undeliverable message" notices you might get back. If you're really wanting to go forward with it, look into POP3 connectors for PHP. Like this: http://pecl.php.net/package/POP3
if (strpos(strtolower($subject), 'undeliverable') !== false){
//do whatever you want with the address that couldn't be reached
}
You technically wouldn't need a compiled PHP extension for POP3 (especially if you're new to PHP)... you could connect and read messages by opening a socket and speaking mail server:
http://www.adamsinfo.com/a-rudimentary-php-pop3-example/
Edit (years later):
Definitely check out http://mailgun.net/, http://sendgrid.com/, and http://postmarkapp.com/.
If you're sending HTML mails, you could use a little trick:
generate a unique id for the mail you are sending (based on content and recipient)
include an image that is loaded from your webserver
<img src="http://yourdomain.com/tracker.php?id=1234567" />
in tracker.php, log the id that called the script and send a 1px by 1px image
This won't work though, if the mail client does not download images from the internet when showing an email, as Thunderbird does, for example (IIRC Outlook does so too)
There is no definite solution for this. Web bugs are a o.k. idea but they're dying out, as they are very problematic security wise and are blocked by default in every current E-mail client I know of. I would suggest a combination of checking a bounce inbox like brianreavis suggested, and in addition, requesting a delivery receipt using the following header line:
Disposition-Notification-To:<xxx.xxx#example.com>
That way, you can get most negatives (bounced mails) as well as many positives (receipts). Sending the receipt can be blocked by the sender, but together with parsing error notifications, you should have fairly reliable system.
One option is a Web Bug but these are far from 100% reliable, and are arguably not a nice way to behave. It won't differentiate between emails which are unread and those which are undelivered, for example because of a bad email address, and it is possible to read an email containing a Web Bug without triggering it.
In short you create an HTML email containing an element which has a URL on your site which is unique to that email. So if a client accesses that URL you can be sure that someone has read your email. Wikipedia gives this example:
For example, an e-mail sent to the address somebody#example.org can contain the embedded image of URL http://example.com/bug.gif?somebody#example.org. Whenever the user reads the e-mail, the image at this URL is requested. The part of the URL after the question mark is ignored by the server for the purpose of determining which file to send, but the complete URL is stored in the server's log file. As a result, the file bug.gif is sent and shown in the e-mail reader; at the same time, the server stores the fact that the particular e-mail sent to somebody#example.org has been read.
However, it is possible - probably quite likely - that someone can read your email without connecting to that URL. This may because:
A lot of email readers block such links by default because of privacy concerns precisely because of Web Bugs like this.
They read the email in text only, either because their email client is configured to do so or because it can't display HTML email, for example, a lot of mobile phone clients.
This is a often used option - both by spammers and more responsible marketers - but I probably wouldn't recommend it unless you fully understand its limitations and implications in terms of what people might think of you if you do use it.
There is a perfect solution, in terms of knowing the eMail has been read.
The bad part is that the body of the eMail must be stored on web server.
The trick:
-Send an eMail just just a small text and a URL, so the user must go to that URL to read de content of the eMail.
In other words... on the eMail there is nothing about what you want to send, there is only a link to a unique page you create before sending such eMail.
So to read the content the user must open a web browser and go to such URL.
The trick is to put a little of such text on eMail... something like: bla, bla, bla ... press here to read more.
The concept: Online body, offline URL to access such body.
That way you can be sure of this: if URL has been read, the eMail has been read.
The bad part is that the info you want to send is not on the eMail, must be on a unique webpage.
So you must control no robot can go to such URL... for example with URL like:
https://server/private/?eMail_Body=user
Hope this idea can help someone.