send email attachments (any format)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shankari
Forum Newbie
Posts: 9
Joined: Mon Oct 03, 2011 9:37 am

send email attachments (any format)

Post by shankari »

This is the code. I don’t get any error. But email is not sent. can anybody help?
Also, I need to send any type of file (mp3/pdf/txt/wma/doc)

Code: Select all

<?php

session_start();

$msg="";

if (isset($_POST['Submit1']))
{

$scname=$_POST['scname'];

$file_name=$_FILES["file"]["name"];
$splt = explode(".",$file_name);
$fpart=$_POST["scname"].".".$splt[1];
if(file_exists("Events/".$fpart))
{

}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"Events/".$fpart);
}

$to="xxx@xxx.com";
$from="xxx@xxx.com";
$subject="Event Registration – For approval";
$message="Event Registration Request…..";

$random_hash = md5(date('r', time()));

$headers = "From: xxx@xx.com\r\nReply-To: xxx@xxx.com";

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = chunk_split(base64_encode(file_get_contents("Events/".$fpart)));
$output = "
–PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
–PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit

Hello World!
This is the simple text version of the email message.

–PHP-alt-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit

Hello World!
This is the HTML version of the email message.

–PHP-alt-$random_hash–

–PHP-mixed-$random_hash
Content-Type: application/octet-stream; \”$fpart\”\n
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
–PHP-mixed-$random_hash–”;

echo @mail($to, $subject, $output, $headers);
} 

?>
User avatar
Salaria
Forum Commoner
Posts: 34
Joined: Fri Feb 13, 2009 2:50 am
Location: India
Contact:

Re: send email attachments (any format)

Post by Salaria »

I think you are missing in email headers, content-size (Attachment Size) and adding all attachments as Content-Type: "application/octet-stream" instead of specific to attachment mime type.

I have not tried to diagnose on your provided script by running it but you can compare your script with
http://www.finalwebsites.com/forums/top ... ent-script
http://www.php.net/manual/en/function.mail.php#105661

If you are looking for more flexible solution for sending emails then I would recommend you to SwiftMailer for this http://swiftmailer.org/

Please reply me if you just want to get this script working. I'll check by running it on local installation tell you exact issue.

Thanks and Regards,
shankari
Forum Newbie
Posts: 9
Joined: Mon Oct 03, 2011 9:37 am

Re: send email attachments (any format)

Post by shankari »

If you could get this script running , that is awesome. I'll sure check other links you have given
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: send email attachments (any format)

Post by twinedev »

Another thing to consider is using something like the phpMailer. Well coded and easy to use (when you download it, the only file you need to include on your site is the main phpmailer.class.php file, and they give good variety of samples in the zip file you download. Eliminates all that.

Only real basic text emails do I hand write anymore.

-Greg
Post Reply