email attachments to server - php

I want someone to be able to email something (image, video, text) and then have that data be saved on my server in a special directory (the one representing the email address) and if possible take the text with the email and put the data into a database (tags, title, etc).
What is the best way to do this?

The best way to do this is to run a mail server on the server, and have it output email to a particular address to a command. This command will then parse the email and handle saving the attachment and writing to the database.
Try the Postfix mail server and a | foo.sh alias. The script can be written in whatever language you like. Choose something which will make decoding the email easy.

What is the best way to do this?
If you've no idea where to start then I'd recommend posting a question somewhere like Stack Overflow, providing the relevant information needed for people to suggest a solution, including:
the OS where the mail server resides
whether php can be run on this box or if it needs to be located elsewhere
the software in use for the email service - and whether this could be changed easily
the relevance of timing between delivering an email and content being available
Peering into my crystal ball, the best solution would be a php script triggered via procmail
C.

Related

How to get content from email sent to your lamp server

Say I have my own server, and I want to send an email that's reads "hello World" to my site, submit#example.com
Is there a way to automatically extract the text "hello world" and post the content to my website?
I believe I can read a file with PHP, and the file can be created by apache for every incoming email... but I'm missing some knowledge here
can anybody help me?
You need to have PHP act as an email client. Take a look at PHP IMAP (which actually works for POP too) http://php.net/manual/en/intro.imap.php
While emails are almost always stored on disk as files, accessing those files directly would not be advisable unless you were writing a new email server (I've contemplated that actually, but it is definitely NOT a trivial task) and your web server (e.g., PHP running in Apache) wouldn't have that level of access. The basic steps the right way are:
Add the email account, set the password, etc. Decide whether you want to access it as IMAP (emails stick around unless you delete and then empty the trash, which lets you archive on the server and/or access from other devices) or POP (typically "read the emails and delete from the server immediately").
Write PHP code to read the mail account. The specifics will vary slightly depending on the email server/hosting configuration. You may need to work at it a bit until you can successfully read a list of messages.
Once you are able to read the messages, you will likely need to handle multiple formats. In particular, messages may come in as plain text or as mime parts. There can be quite a bit of variation depending on the software used to write the original message, so to test you may want to send in messages using Outlook, Thunderbird and other programs.
You will also likely need to filter out HTML tags and other extraneous stuff from legitimate messages AND you will inevitably have to come up with a way to block spam. If you only accept messages from registered users then you can verify based on the From: address (at least as a start, because that can be spoofed too...) but if the support email address is "open" then filtering the junk can be quite a challenge.
This is a non-trivial project, but I have successfully set up a few systems of this type, though typically with controls to help block spam (e.g., required codes in the Subject line; limited "From" addresses).

Reading File and Mailing with PHP

I would like to read a data file (such as csv or txt) and mail it using PHP. I would like to be able to have at 5 minute pause in between each send of data. I would also like to use the email field as the senders email. Any help would be appreciated.
Each data chunk will contain:
name
email
address
city
state
zip code
phone
comments
I'm guessing you want to write a newsletter application, so proceeding under that assumption:
You are probably not going to find this answer very helpful, but you should really consider using a newsletter service instead of writing your own code. The reason for this is that sending newsletters requires a LOT more effort and different skills than writing a simple script.
For example, but not exclusively:
Setting up a mailserver correctly.
Taking care about the reverse DNS.
Managing digital signatures (DKIM) for your mail.
Subscribing and tracking sender feedback loops.
Tracking and avoiding DNSBL blocking lists for your server.
etc.
If you really want to write your own code, you have been warned. Take a look at str_getcsv() and SwiftMailer and don't be surprised if you get headaches from this project.

how can I fetch a message using a php script?

I want to make it so that I can email a url and a php script or something at that url will take the contents I emailed such as message body and write them into a DB. I know how to write something into a DB but I dont know what to do in order to EMAIL to a url or person and have it pull out the message.
The reason I am thinking of emailing to a url if possible is because this saves me the massive blob of trouble of setting up IMAP and using all that mess. I know this can easily be done with php I just have no idea how.
You cannot send email to a URL, you must use a valid email address.
If you want to read your email from within php to import into a DB, you'll need to communicate to your IMAP/SMTP/POP3 server via a socket connection with the fsockopen() function.
I suggest you download any open source web based mail client such as RoundCube, Squirrel Mail, etc... and examine the source code.
There are quite a lot of options when it comes to receiving email. You'll still need to use an email address to send to but you can forward that email on via an HTTP POST using a couple of options such as cURL scripts or using a third party like CloudMailin.
I wrote a blog post about this in Rails (although the principles apply directly to PHP too without much modification. This is a dup of a couple of other questions but it was asked in a fairly different way hence the answer.

PHP Save outgoing php mail messages

What is the best way to save a record of outgoing mail through the PHP mail function?
Basically, I want to archive the messages like the Sent folder in Thunderbird/Outlook.
The only 2 options I can think of is create a DB field, or save them at txt files on the server.
I was wondering if anybody had any suggestions, or methods I might be over looking
Either make a copy of the mail and store it yourself in your mailing script, or... and this one's quite handy: BCC a copy of the email to whatever account you want to be the archive of the sent messages. You can set up a mail folder rule to redirect those BCC'd copies into a specific folder if need be.
I suggest saving them in database since it makes searching them a lot easier than plain text files
If there is a massive amount of mail look into MongoDB. It is a NoSQL DB and many sites use it for archival purposes.
I would save them in a database. When the emailer runs to send out messages, a copy is added to the database. You can also keep track when the email was sent and all that fun information. It will make searching the database easier later. Text files would be a pain, imo.

Are there any way to know whether user open mail?

we build newsletter module,
and send email to members.
The environment is LAMP.
Are there any way to know whether member open the mail ?
i hear about put image if 'php' source ,
what is the best way?
Ultimately there is obviously no fool-proof way to get notifications, because there is no guaranteed way of getting the email client to respond back in some fashion; the email client can be set up to just interpret the incoming email as ASCII text and nothing more, and there is nothing you can do about that.
However; in most cases if you are emailing to users that are expecting an email, odds are that HTML rendering and inline images are probably turned on for your source address, so using an inline IMG tag and monitoring access to the referenced file (obviously using some per-user unique ID in the reference) should give you the best you are going to get.
Regardless you'll never be able to make the assumption that if you do not get a notification back that that means the user has not seen the email.
There's no foolproof way to do it since you're not the one in control of the email client. Many people take their privacy seriously enough to prevent read-receipts, web beacons and all the other tricks which can be used to detect the reading (people can turn off read receipts, block images, prevent unsolicited outgoing connections and so on).
This is my opinion of course but I believe you're approaching the problem the wrong way. Instead of trying to force the user to let you know if they've read the email, just make it worth their while. It's obviously of some benefit to you to have this information (otherwise why do it?) so you share that benefit around and make sure it's the user's decision.
That way, you turn the relationship from a battleground into a partnership (win/win).
Yes, there is a standard mechanism (RFC 3798) called read receipts. It is implemented by all modern mail clients, and the user can choose to send (or not) the reciept as they choose.
There are also various non-standard subterfuges for doing this without the user's consent, which I won't detail.
EDIT:
It should be like the below (using built-in PHP mail function):
mail("foo#foo.com", "Let me know if you get this", "Important message", "Disposition-Notification-To: sender#sender.com\r\n");
A common way to check if an email has been read is a web beacon, which is usually a small 1x1px invisible image that is loaded from your server, which can track when the image has been loaded and therefore the email has been read.
This is not guaranteed to work, however, since many email clients block images in their emails or your readers could be using text-only email clients.
Each email has a uniquely named image in it corresponding to the users account (or db row), when that image is loaded or accessed, you can see which user has opened the email. This relies on the user receiving HTML emails though.

Categories