Monday, February 7, 2011

Sending Email With Multiple Attachments in PHP

This script is really helpful for those who want to send email with multiple attachments in PHP. There are scripts out there but most of them have some errors in them but i assure you that this script is error free.


If you guys face any error please let me know and i will try my best to assist you.


<?php
function SendAttachMail($to,$from,$sub,$msg,$i)
{
$flag_attach = true;
  // Who the email is from
$email_from = $from;
// The Subject of the email  
$email_subject = $sub;
// Message that the email has in it 
$email_txt = $msg;
// Add the headers for a file attachment 
$headers = "From: $email_from";

// Create a boundary string. It must be unique 
// So we use the MD5 algorithm to generate a random hash
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
              "boundary=\"{$mime_boundary}\"";

$email_message .= "This is a multi-partmessage in MIME
                     format.\n\n" . "--{$mime_boundary}\n" .
  "Content-Type:text/html; " . 
                     "charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
                     $email_txt . "\n\n";


$fileatt_type = "application/octet-stream";
$fileatt = $_FILES['fileatt'.$i]['tmp_name'];
$fileatt_type = $_FILES['fileatt'.$i]['type'];
$fileatt_name = $_FILES['fileatt'.$i]['name'];

$file = fopen($fileatt,"rb");
  $data = fread($file,filesize($fileatt));
fclose($file);


 //Read the contents of atachment file into a string and then encode it 
 with MIME base64 and split it into smaller chunks
$data = chunk_split(base64_encode($data));

 //Define the body of the email message.
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

$email_message = $email_message.$email_txt;
@mail($to, $email_subject, $email_message, $headers);
}


if(isset($_POST['cmdsend']))

 // Now i will write the code to show how the attachments will be 
 passed to the  SendAttachMail() function.

 // The email to whom the message will be send
$to="to@gmail.com";

// Who the email is from
$from="from@gmail.com";

// Subject of the email
$sub=$_POST['em_sub'];

// Email body message
$msg=$_POST['em_msg'];

// Code for multiple attachments. I am doiing this for 3 
 attachments but you can attach as many you want just increasing 
 the number in the code

for($i=1; $i<=3; $i++)
{
$file_name=$_FILES['fileatt'.$i]['name'];

if(trim($file_name)!='')
{
SendAttachMail($to,$from,$sub,$msg,$i);
}
}
}
?>


The html part for this code is:
<!-- Forms action is set to the same page -->
<form name="frmemail" enctype="multipart/form-data" method="post" action="">
Enter Subject: <input type="textbox" name="em_sub"><br>
Enter Email Message: <input type="textbox" name="em_msg"><br>
Attach File1: <input type="file" name="fileatt1"><br>
Attach File2: <input type="file" name="fileatt2"><br>
Attach File3: <input type="file" name="fileatt3"><br>
<input type="submit" name="cmdsend" value="Send Email">
</form>

Saturday, February 5, 2011

EARN EXTRA CASH WITHOUT INVESTING MONEY

Hi,

I am going to introduce a PTC program as I have to admit that I am extremely impressed with Big Extra Cash.

I love Big Extra Cash because you get paid $0.05 just by placing ad on your website or on your blog and to send traffic to Big Extra Cash.

What you get as a member:

Earn up to $0.05 (5 cents) per click.

Earn up to 40% for every sale generated by your referrals click.

Access detailed statistics of your clicks, earnings and even of your referral's clicks and
activities.

Payment via Moneybookers, Alertpay and PayPal!!!

Big Extra Cash is the best PTC site out there with more than 600000 members and paid almsot $500,000.00 and growing.

Just remember this: patience is a virtue.

Please visit the link below to register and start earning today.

http://www.bux4ad.com/aft/f9d37d9a/a10a452b.html

Thanks,

Zeeshan

Web Development | Low Cost Web Development | Quality Web Development

FlairSol Logo
FlairSol Inc. is a low cost web development company. We provide quality web development solutions to website design and website development and are providing website design and development services to both local and offshore web development clients since November 2003.

Presently in the market, an effective IT solution is more than simply just a matter of knowing the technology. At our outsourcing services company, we understand it's a matter of understanding the client's business goals. 

This means that we always think about:

The end-users: interface has never been so important - that's why we believe that end-users need to have an intuitive and visually appealing way to use any kind of software or web-site.

The quality: internal quality policies ensure that we deliver a stable and robust code and provide ongoing technical support after the project completion.

Your business: we will never start a project without understanding the nature of your business and the issues which need to be addressed by completing the project, moreover we will always suggest you several alternatives to choose the best one matching your business needs and budget!

  • Web Application Development
  • Web Development
  • Web Design
  • E-commerce solutions
  • IT Consulting
  • Domain Registration
  • Web Hosting