Link to home
Start Free TrialLog in
Avatar of RajeshKanna
RajeshKannaFlag for India

asked on

Mail attachment problem in php

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

Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

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...
Avatar of RajeshKanna

ASKER

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..
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
davekok,

I received the mail but there is no file attached.
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.
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";
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
I used "\" for file path and got the following error.
error1.jpg
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.
ASKER CERTIFIED SOLUTION
Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Billion thanks