Solved

Mail attachment problem in php

Posted on 2010-09-01
15
482 Views
Last Modified: 2012-05-10
Experts,

I got a script to send mail with attachment, the script was good. It send the attachment but if I download or open the attachment it shows an error message as file corrupted, it shows the file size correctly.

Can you please help me with this. I attachment my coding below.

Is there any possibility to send bulk email in this coding, which i attached???

Thanks in advance..
if(isset($_POST['send']) && $_FILES['file']['size'] >  0)
{
$fileName = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
$fp = fopen($tmpName, 'rd');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
}
//define the receiver of the email
$to = 'srigowsalya@buy2all.in';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode($tmpName));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: <?php echo $fileType;?>; name=<?php echo $fileName;?>

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";

Open in new window

0
Comment
Question by:RajeshKanna
  • 7
  • 2
  • 2
  • +3
15 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33574814
0
 
LVL 6

Expert Comment

by:midhungirish
ID: 33574819
I prefer to use swift mail plugin ( http://swiftmailer.org/ )....... It deals with all the low level code... and allows us to handle the application logic well...
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 33574823
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:RajeshKanna
ID: 33575409
Hi experts,

I need to get the files from my local system and it should be attached to the mail. So please help over it..
0
 
LVL 5

Expert Comment

by:davekok
ID: 33575529
Specify the content transfer encoding in your attachment:

"Content-Transfer-Encoding: base64\r\n"

You may also wish to add a disposition:

"Content-Disposition: attachment; filename=\"$filename\"\r\n"

Full code:

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: <?php echo $fileType;?>
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?php echo $fileName;?>"

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--


I hope this helps
0
 

Author Comment

by:RajeshKanna
ID: 33575637
davekok,

I received the mail but there is no file attached.
0
 

Author Comment

by:RajeshKanna
ID: 33575648
logudotcom,

I used the URL which you have mentioned, I get a error message in put() function and also I need to get the file to be select from my PC and has to attach with mail.
0
 
LVL 6

Expert Comment

by:syedyounus
ID: 33575840
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_25130786.html  

Check the  solution  ID:26534918 by Author:VanHackman
0
 
LVL 7

Expert Comment

by:Swatantra Bhargava
ID: 33575889
0
 
LVL 5

Expert Comment

by:davekok
ID: 33575972
Try adding the "MIME-Version: 1.0\r\n" header to $headers. After adding the content type on line 27.

$headers .= "MIME-Version: 1.0\r\n";
0
 

Author Comment

by:RajeshKanna
ID: 33583860
davekok,

After adding the header too. I can't receive the attachment.

Experts,

I need to get the file from my local PC and has to attach it with the mail. Please help over it.
I got another script and I got error over it ("fopen() failed to open stream: No such file or directory")

I attached the code and error message screen shot bolow.
<?php
require_once('PHPMaile/class.phpmailer.php');
if(isset($_POST['send']))
{ 
$fileatt = $_POST['file']; // Path to the file
$fileatt_type = "application/octet-stream";
$fileatt_name = ""; // Filename that will be used for the file as the attachment

$email_from = ""; // Who the email is from
$email_subject = "Test Mail"; // The Subject of the email
$email_txt = ""; // Message that the email has in it

$email_to = ""; // Who the email is too

$headers = "From: ".$email_from;
echo $fileatt."<br>";
echo str_ireplace('"\"',"",$fileatt);

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

$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-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$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";

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}
?>

Open in new window

error.jpg
0
 

Author Comment

by:RajeshKanna
ID: 33583873
I used "\" for file path and got the following error.
error1.jpg
0
 

Author Comment

by:RajeshKanna
ID: 33583889
Can you please help me with any one of the script??? In both the case the file was attached with mail but the content of the file was not there.

For the first script which I posted got error message "file corrupted" while i open the attachment in the mail.

For the second script I got error message "File empty" while i open the attachment in the mail.
0
 
LVL 7

Accepted Solution

by:
Swatantra Bhargava earned 125 total points
ID: 33583920
Try attached code which is working perfectly with one attachment.
email-blast.zip
0
 

Author Closing Comment

by:RajeshKanna
ID: 33584200
Billion thanks
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Word, Excel, Access icons 4 17
PHP Mail error 3 28
Wordpress Pagination 1 30
JQuery Search Filter 2 39
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question