Solved

Mail attachment problem in php

Posted on 2010-09-01
15
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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…

734 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