uploading pictures by email - php

I was looking for uploading photos by email documents (in php) but couldn't find good one.
Is there any easy ways to upload pictures/photos by email?
ie. steps,
1) on mobile devices, email a picture to private-email#domain.com
2) copy the picture into our server like facebook or flickr
ps. I run apache servers, mysql and php

There are a number of different approaches to receiving email with PHP. The main options are:
Retrieve the emails via background job (cron etc) and IMAP/POP3
Run a mail server and make that send the content into the app directly (pipe it into php)
Use a third party service
I wrote a blog post that was for Rails here, although it's for Rails pretty much all the principals still apply to PHP and translate directly.

Related

Programmatically inserting messages to an email account with CPanel API

I'm looking for a way to programmatically insert messages into a specific email account - created and maintained with CPanel.
I have a website that provides a webmail interface for a video game's inner messsaging (using its API) and I would like to take this service one step further and make the messages available on a POP3 server.
I have looked for a solution multiple ways so far:
Insert mails using CPanel's API: I could not find an api call for this, only for creating new accounts (with https://documentation.cpanel.net/display/SDK/UAPI+Functions+-+Email%3A%3Aadd_pop) This is the preferred method for me as I'd rather leave alone the filesystem.
Manually insert the mails to the filesystem: I found out, that Cpanel stores email data in root/mail/domain/user/ but I have no knowledge of the exact file structure. I recognize the maildirsize file and the other directories containing the mails, but I have no knowledge of the maildirsize file's structure (seems to contain 2 integers divided by a space per line) and also the mail file's filenames are as well not obvious. This is an example of a filename:
1422094110.H186037P182351.hosting-server-domain.com,S=15645
I'd rather use this method as a backup method, because there is very little (or I didn't find any useful) documentation available.
Simply E-mail all messages with custom headers: This would be far the easiest method, however the hosting provider has a very strict anti-spam policy and the outgoing message number is limited.
Implement an own POP3 server: I played around with #cleong 's PHP implementation (https://stackoverflow.com/a/11973533/1030464) and while it worked on localhost, I am not sure it'd be a joyride on the live page. I found a Perl implementation as well which might be worth a try, but I have never ever worked with Perl and I'd rather look at another solution before learning how to implement and integrate that module.
Thank you for reading all this,
Bálint
It sounds like the email is being stored using the Maildir format. This makes inserting mails fairly straightforward. For full details see the Maildir specification, but in summary:
The hardest part: create a guaranteed-unique name for the mail, using the techniques suggested on that page (e.g. local hostname plus high-resolution timestamp plus high-quality random number).
Create a file with this name under .../Maildir/tmp/
Rename the file to be under .../Maildir/new. This renaming step ensures that only fully-formed mails are seen by the mail software - i.e. no other process will attempt to look at the file when you're half way through writing it.
That's it!

How does a php script on a contact form access the user's email?

This tutorial teaches you how to create a contact form for a website using php script: http://www.boutell.com/newfaq/creating/email.html
But how does this work? How does the script access the user's email account to send an email? How does such a form work without a server? (For example if I put my website on github pages which only handles static pages, how does this php script work?)
there's no way to make the server do something if the server does only throw HTML pages out.
You need some programming language (PHP in your tutorial) to process your code, it has to be installed in the server and accessible to you. It will run your code, decide what to do and do it (by the way to actually send the mail you will need the proper PHP modules available). In this case the script will not use the user's email account to send the mail: it is the server who sends the mail, not the user's client. You will receive an email from your server, so watch out to ask the user for his email, otherwise you will not be able to get it in any way.
If you have no serverside intelligence capacity, just use your user's tools. Which means stuck with the mailto scheme (as the tutorial's first examples show). Then the user's computer will send the email (with the drawbacks the tutorial points out)
It uses the mailto: URI scheme which is opened by your mail client just like skype: scheme is bounded to your Skype client.

Wordpress php sending data to local server

If I have a wordpress website, and a user on the website enters some survey information, is it possible to send the results to a local server inside a company (assuming the website is hosted on some other companies server). From looking around I see people using the JSON formats and GET, PUT etc.. but I havent seen this demonstrated with wordpress. Is there a standard way to do this? I can see it is possible to send via emails, but I was hoping for something more like TCP/IP communications
If it must run through the front-ends WordPress installation, then the easiest way to be a simple HTTP POST request to a server you control. PHP has several different ways you can accomplish this with minimal effort.
The other way you can do this is just to set up a form that will send an AJAX response to your server. Just make sure your receiving server is configured to allow the originating domain.

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

Send photo to email bot, upload photo to website

I am constantly taking the following steps, and I know there's a way to automate this:
emailing photos from my phone to myself
saving those photos to my computer
uploading the saved photos to a website
Is there a way to write a script (perhaps in PHP), that does the following:
listens to any emails sent with
attachments to a bot email (ex:
bot#site.com)
automatically uploads any attachments sent to that email to the site? (ex: upload to www.site.com/photos)
If you must have it on your server, there are a couple approaches.
PHP can check a mail account via POP3 or IMAP. You could have a cron job running that fires off a PHP script to check a secret e-mail account every few minutes and process there.
Incoming e-mail can also be piped directly to a PHP script
I just use Flickr, and they give you a random e-mail address #flickr.com that you can send attachments to and have them automatically processed. That's the fastest and easiest option if you're okay with Flickr hosting your photos.
If your phone can go online, why email them and not directly upload photos to your site? Many mobile platforms have clients for adding entries and images to blogs and hosting services like Flickr. Maybe there is such a client right for your phone?
Script on its own is a passive thing. I'm not familiar with PHP though but... If you have a page open 24h/day, you could put there JavaScript calling some server method in loop once in 10 minutes to check for emails and if there are attachments just initiate asynchronous form post.
Would be great if you could elaborate and whatever you have at your disposal. Is it your site and you have administrative access or you just a user? Is there maybe an API to use in addition to just HTTP POST?
I believe, unless it's your own site, you will need somewhere a PC running all the time with either a browser window or a desktop program open.

Categories