PHP mail attachment script

This custom function or PHP mail attachment script is able to send a plain text email message together with a single attachment file. The attachment file has to be uploaded first or you can use a file which already exists on your web server. There are much better and more advanced PHP scripts on the Internet, but I hope this example will help you to understand, how it’s possible to send an email message plus attachment by using some PHP code.

About PHP’s native mail() function

PHP mail attachmentThe mail() function doesn’t support attachment or HTML mail by default. You need to use different headers and MIME mail parts to make this possible. Many shared hosting providers doesn’t allow the usage of this function and it might be disabled.

Normally you will pass three values to the mail() function plus some headers. In the example below I skip the value for the message value, because the message is defined as a MIME part together with the attachment.

Below is an example on how I use this function to send an email message with one attached .zip file:

Are you looking for a script to send multiple attachments? Try my PHP email attachment class script which is based on the same method and is another example how mail in PHP code works.

Other ways to send email with attachments using PHP

For websites or applications were an email message is an essential part for your business you should use a professional transaction email service provider and a PHP class which works for the SMTP standard. In my opinion is the PHPmailer class a very stable and flexible email script. If you like check my PHPMailer tutorial, too.

Subscribe

Don't miss a future blog post!

Subscribe now and get future blog posts right in your email inbox.

20 Comments

praveenkrg

Hi Olaf,

I tried with your example. Mail sends with an attachment. But the size of attach zip file become 0 byte. So the zip file could not work.

I don’t know whats the problem

Any help will be appreciated.

Thanks in advance,
Praveen

Hi,
maybe the path to the file is not right?
check this row first:

$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";

maybe you need to use a static path?

praveenkrg

Thanks for immediate reply

I checked the this row
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";

My directory structure is like this:
www.domain.com/foldername/zipfilename.zip

When I try with static link it gives warnings:

Warning: filesize() [function.filesize]: stat failed for www.domain.com/foldername/zipfilename.zip on line 191
Warning: fopen(www.domain.com/foldername/zipfilename.zip) [function.fopen]: failed to open stream: No such file or directory
Warning: fread(): supplied argument is not a valid stream resource
Warning: fclose(): supplied argument is not a valid stream resource

Any idea?

That’s what I thought :)

A document root is something else, create a new file and check the doc root using:

echo $_SERVER['DOCUMENT_ROOT']

praveenkrg

Sorry didn’t understood. Can you elaborate, what should I change and where?

You need to know what your document root is, this path is required to open a file with PHP.

Create a new PHP file and add this code:

Run this small script in your browser and the output is the document root.

Next you need to check if this is the same location where the attachment is located.

praveenkrg

When I run the above script on local system it prints “C:/xampp/htdocs”

And when I run the script on server it prints: “D:\Hosting\4836046\html”

My server directory structure is like this:
www.domain.com/foldername/zipfilename.zip

Windows servers and the document root are always a problem.

I never used PHP on windows, check this PHP function (check also the comments)
http://php.net/manual/en/function.getcwd.php

Maybe your hosting provider can help you on how to identify the document root from your hosting account?

praveenkrg

Ok I understood the problem. I will ask my service provider.

But the mail I am getting this time is containing the 0 bytes zip file attachment. Is this problem because of doc root?

Right, the script can’t include the file’s data (only the mail header for this attachment is send)

praveenkrg

Hi Olaf,

My hosting provider replied.
Absolute Hosting Path: D:\Hosting\4836046\html

Now what’s next step?

Sorry, I can’t say exactly how you need to do this in Windows :(
(I use windows only to work in Adobe Photoshop)

First you need to find a way to handle those paths in PHP on windows.
I’m sure Google has a lot of information on that.

On Linux I have one file (config.php) in the doc root, creating this constant variable:
define('DOC_ROOT', dirname(__FILE__).'/');

Next I include this file in my PHP scripts, let’s say the script is one level above the root I use
include_once '../config.php'

After including that file I’m able to use the variable DOC_ROOT in my scripts. This way the server variable DOCUMENT_ROOT is not needed anymore.

I hope that helps.

praveenkrg

Thanks for your advice. I will try to find alternate solution for this.

One more thing I just tried your code with my other hosting (linux, php).

mail function is giving warning.

Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. in /home/content/b/e/s/bestjobs1/html/corporate_reg/mail_attach.php on line 26
mail send ... ERROR!

any idea?

I think this is related to the paths as well.
If you can’t get these paths under your fingers, you will not succeed.

Try a simple mail command first:
mail('mail@domain.com', 'mail subject', 'my first mail send via PHP');

praveenkrg

Hi Olaf,

Thanks for your support and help.

I did it my self.

–Removed 3rd party code–

Hi,

great that you found a working solution.
I removed your code because it’s not tested and might not safe for other users.

Hello Olaf, the mail function is not working in my site, actually it is working fine my domain but when i uploaded to this site (appiconexpress.com) it is not working well. It gives mail sending error..! (else part). Please help, thanks in advance

    Hi,
    it’s possible that your host doesn’t allow the use of the mail() function. Check this with your hosting provider. If they don’t allow that that function try PHPMailer.

thanx for the quick reply, i tried this code with without the header part means I use if ( mail( $to, $subject,”” )) then mail is sent but with header content it does not work. Is there is problem with header part or some other issue. Please tell me the solution

    Do you use any attachement? Maybe custom headers are not allowed? If you use mail() for more than just a single message, the script can break on many places. You can try it for another X hours to get it working or you use a full featured mail script like PHPmailer :)

Leave a Comment