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
Related
Emails containing requested reminders are sent to users daily. My goal is to create a calendar in each marking days in which they opened the email in green. This requires tracking whether the user opens or does not open their email on a given day.
Mailchimp present their method in a blog post
When you send campaigns through MailChimp, we embed a tiny invisible graphic in the bottom of your HTML email... When someone opens your email and views the images within it, that
graphic is downloaded from our server, and it's recorded as an open on
your campaign report.
To break this process down into steps:
1) Including graphic in each email (sent with chron job) unique to user
2) Writing a script that runs whenever the graphic is downloaded from the server and changes an "email_opened" value in a User History table
So my questions:
·How does #1 work? Furthermore, how can I create a system for automatically generating a unique graphic for each user?
My current idea is to send the image from a table using PHP such as:
<? $img_data = file_get_contents("images/phpimage.png"); header("Content-type: image/png"); echo $img_data; ?>
and then somehow set it up to send a different image depending on the email address.
·Where should script #2 be placed and in which language should it be written? My initial thought was php and within the email, but if it's the moment the graphic is downloaded from the server then surely it should be server-side. How would this work?
To "include" an image in an HTML-format email, what you actually have to do is create an <img> tag where the src attribute contains a URL which points back to an image file on your server. (You can't run PHP "within the email", as you contemplated, btw).
For this purpose though, the image could be invisible (using style properties, or by setting height/width to 0), and the src could actually point to a script on your web server which accepts a user ID and email ID as parameters and renders an empty image. The unique URL could be something like http://www.example.com/logEmail?userID=12345&emailID=6789 for a particular user/email combination. The image content that you return from this script is not important and does not need to be unique - remember, it's invisible anyway. Just return a 1px by 1px white square.
When the script runs, before it renders the image back and completes the request, it simply updates your database at the same time with to indicate that the email with the given ID has been read by the user with the given ID.
As for the language, that's entirely up to you depending on how/where you're planning to host the web application. You can achieve what you want in any of the popular server-side languages.
The MailChimp article you linked to makes an important caveat though - this technique won't work if you send any users emails in plain-text for any reason, and nor will it work if the user's mail client is not set to download images (corporate Outlook installs are often set like this by default, for instance, until the user clicks to allow them to be downloaded, and/or marks the sender as safe). So you can't rely on this data as a 100% accurate guide to whether your users are reading your emails or not. If you get a response for a particular email, you can say with certainty that they opened it (N.B. "opening" and "reading" and "understanding" are absolutely not the same thing!!). But if you don't get a response, you cannot in fact be certain whether they opened the email or not.
Please check following scenario,
First I am sending email to user using php
Whenever user viewed this email, I want to update my user table.
Is it possible?
Usually you'd put some resource to your email (like an image/tracking pixel) that is loaded from your server. The url of that resource is actually an url to your script. Once the client app tries to load the resources your script gets executed. You get the params you set when sending the email and do what you want with them.
Keep in mind that not all email clients will behave this way. For example plain text email viewers will not load any resources.
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.
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'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.