You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “Recieve E-mail and Save Attachments with a PHP script”.
You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “Recieve E-mail and Save Attachments with a PHP script”.
Great solution, exactly what I was searching for!
But there is no instructions on how to send an email to the script in the first place?
And can a cron job be used to check a mailbox? And if a new email is found, execute the script to process it?
Thanks
Pingback: Add an email address that forwards to a script | Stuporglue.org
I can’t get the body and attachment, just empty string, that’s all!
how to get it? any update from your script?
thx
Can you send me a sample email that’s not working (full headers and
everything). Either as an attachment or directly to
stuporglue@gmail.com would be fine. My script has only been tested on
email from a couple of mail clients, so there may be headers that
will break it.
If you’ll send me a sample I can take a look at it.
Just what I am looking for – absolutely brilliant thank you! Just one snag… At present, it appears that the body and any attachments are not being stored in the database / saved as files. Any thoughts? Would very much appreciate some tips.
thanks again!
Hello,
Everything works fine except saving attachments. And I can’t find any errors. Any idea what could it be?
If I had to guess (for both Andy’s and Marko’s problems) I would suggest checking folder permissions. Make sure that the user who runs the script has permissions to write to the designated folder, and that the database connection info and table structure are correct. Note that the script is probably run as the mail user.
Nice! just one quick question, how would the code differ incase there are multiple “boundary=” preg_match(“/boundary=(.*)$/”,$line,$matches))? I believe this only looks for first boundary= but emails with multiple sections i believe comes with multiple boundary=. thanks.
It should handle as many parts as you’ve got.
Here’s the relevant snippet again:
function process_multipart(&$info,&$data){...
$multi_parts = split("--" .$bounds,$data);
for($i = 1;$i < count($multi_parts);$i++){
process_part($multi_parts[$i]);
}
}
We do a split on the data using $bounds as the delimiter , so the data becomes an array ($multi_parts) with each array item being one bounded part of data. That part is then sent off to process_part to discover what it is and save it off if appropriate.
Hi im really new on programming, i follow your instructions, but im getting this mail back.. with this message.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
pipe to |/home/paelladelicatessen/buzon/process_email.php
Any tip any help i would appreciate.
thanks
Hi Alex,
I’m sending you an email.
Just as a hint to anyone, you might want to use an absolute path to your save directory. I tried relative first and it didn’t work for me.
An absolute path is a good practice.
Technically a relative path will work since we’re just using fopen, but it must be relative to the directory in which the script is running (which is always the case with fopen and PHP). Which directory that is will depend on how the mail forwarder invokes the PHP script. Since the mail program is most likely running as a daemon, it’s probably running in / and will most likely start the PHP script in / as well.
Whoooohoo! I love it when a plan comes together.
2 issues i cam acrooss
1- denying my email adress i had to fully add my name in ‘my name ‘ it did not like ‘myname@kula.org.uk’
2-the directory issue- each hosting alsways has some wierd chrooting and realtivlaty like you said- but i looked in the log.file you so kindly gerenated and copied used that as my realtive directory– it had extra /home/myaccname/www_root/ instead of just /www_root/
Excellent script ! Thanks!
It only works for me when i send an email from my gmail account :O He saves the attachements perfectly.
But when i send an email via my email accounts in Thunderbird or my online Hotmail account or iPhone (also gmail through iPhone mail app) he doesn’t save the files! I also tried to send mails from the same accounts via a webmail enviroment.
How can i fix this? :O some setting in my email?
You will probably need to look at the raw email that is being sent and fix the parsing that is being done here to work with the emails you are sending.
A better option (and what I should’ve done originally) might be to use a Perl or PHP email library to do the parsing.
Good luck!
hi I followed your instructions but I’m getting this mail back
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
pipe to |/home/affiza/public_html/email_script.php
Please help. thanks in advance
It looks like you are sending an email to “pipe to |/home/affiza/public_html/email_script.php” as though it were an email address.
Instead, you will need to set up an email account on your server (eg. my_file_upload@example.com) and configure the email server to pipe (http://en.wikipedia.org/wiki/Pipeline_%28Unix%29) the emails that are recieved to your PHP script.
Due to the number of different server setups, you’d need to consult your hosting provider for specific instructions on how to do that step.
I have a setup an email account on my server and allowing it to forward all emails to the provided php sript and in script I have followed all instructions of yours
Sorry, I don’t know. The previous user with the similar problem (Alex) didn’t have the path to his script setup correctly, but that’s a pretty simple error which you should be able to catch.
One possibility is that your web/SSH access is chrooted (or similar), but the email is not, which could lead to a different path being the correct path.
OK I have fixed that problem by adding a file exim.conf in root/etc folder
contents of that file are
# This transport is used for handling pipe deliveries generated by alias
# or .forward files. If the pipe generates any standard output, it is returned
# to the sender of the message as a delivery error. Set return_fail_output
# instead of return_output if you want this to happen only when the pipe fails
# to complete normally. You can set different transports for aliases and
# forwards if you want to – see the references to address_pipe in the directors
# section below.
address_pipe:
driver = pipe
return_fail_output
virtual_address_pipe:
driver = pipe
group = nobody
return_fail_output
user = “${lookup{$domain}lsearch* {/etc/virtual/domainowners}{$value}}”
now the second problem is I’m unable to store any attachment in the specified folder :( where am I going wrong. Please help
Thanks
Make sure that the folder’s permissions are writable by the user the script is running as.
In your case, I believe that the group will be nobody, and the user will be whatever
user = “${lookup{$domain}lsearch* {/etc/virtual/domainowners}{$value}}”
evaluates to.
On my server I think the script gets run by the mail server user.
For testing only (because it’s a security risk) you could change the permissions to 777, see if it gets written, and then you’d know if paths are right.
You could also add
print `whoami`;to the top of the PHP script and change your exim.conf
#...Set return_fail_output# instead of return_output if you want this to happen only when the pipe fails
# to complete normally.
This would send the results of `whoami` back to you (also do “print getcwd();”, etc. to figure out what’s going on.
Don’t forget to fix your permissions when you’re done.
I found that it works fine from gmail.com but when sending attachments from mac mail it does not work. I’ll test in outlook tomorrow.
however the attachments are not saving form what I can see. i get this email back
Thanks! I just uploaded the following files to your storage:
Filename — Size
1326110501_1326110501_Twalk.ly-iPhone.png — 199.28 KB
1326110501_1326110501_tigerwifi.png — 334.51 KB
however the URL that it needs to save to
$save_directory = “/home/telecho/public_html/private/email/attachments”;
seems not to save.
I only ever used it with Gmail. If you want compatibility with everything, you’ll probably need a better email parser, like maybe the PECL Mailparse library, or perhaps something in Perl.
is there away to save the whole email just incase it does not save correctly like in a blob formate or something.
the error I think is that mail (apple and outlook) send some attachments like images as base 64.
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=”Twalk.ly-iPhone.png”
…long base64 content blob cut…
I’d start by checking permissions. The script probably runs with mail server permissions, not with web server permissions.
Yes, PHP has lots of functions and can do pretty much anything you want it to.
I’d probably start with these
or possibly
The problem is that my script is not looking for inline content, only for attached content. With the way you have attached the file, it is inline, hence the
Content-Disposition: inline;
filename=CV-IT.pdf
If you attach it, you would instead see
Content-Disposition: attachment; filename="CV-IT.pdf"The Content-Disposition handling is around line 54-64.