Hello!

I am a newbie who's trying to code in my app a way to send email with file attachments. Here's the code I put together (thanks to Google!).

<?php
$fileatt = "/somedir"; 
$fileatt_type = "application/zip";
$fileatt_name = "logos.zip"; 

$email_from = "joe@work_email.com"; 
$email_subject = "attachment test-subject"; 
$email_txt = "attachment test-body"; 

$email_to = "matson@yahoo.com";

$headers = "From: ".$email_from;

$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-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to, $email_subject, $email_message, $headers);

?>

My dev box runs Linux and sendmail. The recipient ("joe@work_email.com") runs MS Windows and Outlook.

The code works and sends an email to the recipient. The problem is that the attached file is corrupted (read, empty). I sent gif and zip files as attachments: MS Outlook `sees` the files as gif or zip but the size is 0/zero bytes.

The weird part is that, using mutt on my dev box, I was able to successfully send GIF & ZIP as attachements.

Is it because the header is mal-formed in my code? What am I doing wrong here?

Thanks for helping.

Al.

I figured out what I was doing wrong.

a) I was passing a directory name ($fileatt) to fopen()
b) I was not trapping any errors
c) I was not using relative paths so the file could not be found. As soon as I moved the file to the directory where my code is, all worked as expected.

Php newbie + new dad + zero sleep for the next 6 months = bad combination. This is the corrected code. Not perfect but gets the job done.

<?php

$fileatt = "upload/";
$fileatt_type =  "image/gif";
$fileatt_name = $fileatt. "dtv.gif";

$email_from = "joe@work_email.com";
$email_subject = "attachment test-subject";
$email_txt = "attachment test-body";

$email_to = "matson@anotheremail.com";

$headers = "From: ".$email_from;

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

$fileatt_name = substr($fileatt_name, strlen($fileatt));

$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-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

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

if ($ok) {
    echo "<p>mail sent!</p>";
} else {
    echo "<p>mail could not be sent!</p>";
}
?>

hi...

I am also struggling for the same...even i m not getting attachment...if u got attachment..plz send the full code...


I figured out what I was doing wrong.

a) I was passing a directory name ($fileatt) to fopen()
b) I was not trapping any errors
c) I was not using relative paths so the file could not be found. As soon as I moved the file to the directory where my code is, all worked as expected.

Php newbie + new dad + zero sleep for the next 6 months = bad combination. This is the corrected code. Not perfect but gets the job done.

<?php

$fileatt = "upload/";
$fileatt_type =  "image/gif";
$fileatt_name = $fileatt. "dtv.gif";

$email_from = "joe@work_email.com";
$email_subject = "attachment test-subject";
$email_txt = "attachment test-body";

$email_to = "matson@anotheremail.com";

$headers = "From: ".$email_from;

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

$fileatt_name = substr($fileatt_name, strlen($fileatt));

$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-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

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

if ($ok) {
    echo "<p>mail sent!</p>";
} else {
    echo "<p>mail could not be sent!</p>";
}
?>

The code I pasted in my *last* post is the complete code (Feel free to use it) I have to email an attachment .

1) first, do you trap errors? Paste this on the first line of your code...before anything else

ini_set('display_errors', 1);
error_reporting(E_ALL);

It will display *all* errors, warnings and notices that most certainly will point out why you are not getting your attachment sent out.

It helped me figure out that I was passing a directory name instead of file name.

2) second, what OS are you running? I am on Linux and I had a permission problem in my upload directory that prevented files to be uploaded, thereby failing the attachment process. Once I changed the permissions to +rwx, the problem went away.

3) make sure your header is correctly formed. That was actually the toughest part for me. If not, you'll received a corrupted attachment (at best). Use the code I pasted in my last post as template.

Enabling error trapping on 1) will help you *a lot*.

Good luck.

Al.

The code I pasted in my *last* post is the complete code (Feel free to use it) I have to email an attachment .

1) first, do you trap errors? Paste this on the first line of your code...before anything else

ini_set('display_errors', 1);
error_reporting(E_ALL);

It will display *all* errors, warnings and notices that most certainly will point out why you are not getting your attachment sent out.

It helped me figure out that I was passing a directory name instead of file name.

2) second, what OS are you running? I am on Linux and I had a permission problem in my upload directory that prevented files to be uploaded, thereby failing the attachment process. Once I changed the permissions to +rwx, the problem went away.

3) make sure your header is correctly formed. That was actually the toughest part for me. If not, you'll received a corrupted attachment (at best). Use the code I pasted in my last post as template.

Enabling error trapping on 1) will help you *a lot*.

Good luck.

Al.

Hi Kristo,

Thanks 4 ur help.....

i have another doubt ....

here my varibles are

$attach_file="$_FILES['resume'['name'];
$tempattach_file="$_FILES['resume'['tmp_name'];

and also 

$content="<table>...........</table>";

where can i set the code in ur php code....here $content contains some message in table formate....

i am getting confused here...plz do with patience....

Thanks in advance....

Since you are not telling what you want to do with $content, how can I know where this variable goes in my code?

Explain what you want to do with it, then I **may** be able to make a suggestion. No guarantees.

Since you are not telling what you want to do with $content, how can I know where this variable goes in my code?

Explain what you want to do with it, then I **may** be able to make a suggestion. No guarantees.

Hi Kristo,

I sry 4 not explaining...k..

this is my php code....i think now, u can understand clearly....plz do it patience...

<?php 
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$name= $fname.' '.$lname;
$edu=$_POST['edu'];
$email=$_POST['email'];
$gender=$_POST['gender'];
$date=$_POST['date'];
$month=$_POST['month'];
$year=$_POST['year'];
$address=$_POST['address'];
$mobile=$_POST['tel'];
$resume=$_FILES['resume']['name']; //attachment
$tresume=$_FILES['resume']['tmp_name'];
$dob=$date.'-'.$month.'-'.$year.'';
$subject="Career";



$email_message= "<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Intemo Systems - Careers</title>
<style>
<!--
.title{
	font-size:18;
	font-family:Tahoma;
	font-weight:bold;
	color:#F78C02;
}
.label{
	font-size:14;
	font-family:Tahoma;
	font-weight:bold;
	color:#FBFBFB;
}
.text{
	font-size:12;
	font-family:Tahoma;
	color:#FFFFFF;
	line-height:20px;
}
.outline{
	outline: thin solid #FBFBFB;
}
hr{color:#77bc13}
table,tr,td{
	font-size:14;
	font-family:Tahoma;
	color:#000000;
}
-->
</style>
</head>

<body topmargin='0'>
<table width='100%' border='0' cellspacing='1' cellpadding='1' align='center'>
  <tr>
    <td colspan='3'>&nbsp;</td>
  </tr>
  <tr>
    <td  width='15%'>&nbsp;</td>	
    <td align='center' valign='middle'>
<table width='100%' border='0' cellspacing='2' cellpadding='2' bgcolor='#717171' class='outline'>
  <tr>
    <td colspan='3' class='title'>Intemo Systems - Careers</td>
  </tr>
  <tr>
    <td colspan='3'><hr></td>
  </tr>  
  <tr>
    <td align='left' valign='top' width='20%' class='label'>Name</td>
    <td align='left' valign='top' width='1%' class='label'>:</td>
    <td align='left' valign='top' width='*' class='text'>".$name."</td>
  </tr>
  <tr>
    <td align='left' valign='top' width='20%' class='label'>Qualification</td>
    <td align='left' valign='top' width='1%' class='label'>:</td>
    <td align='left' valign='top' width='*' class='text'>".$edu."</td>
  </tr>
  
   <tr>
    <td align='left' valign='top' class='label'>Gender</td>
    <td align='left' valign='top' class='label'>:</td>
    <td align='left' valign='top' class='text'>".$gender."</td>
  </tr><tr>
    <td align='left' valign='top' class='label'>Email</td>
    <td align='left' valign='top' class='label'>:</td>
    <td align='left' valign='top' class='text'>".$email."</td>
  </tr>
   <tr>
    <td align='left' valign='top' class='label'>Date of Birth</td>
    <td align='left' valign='top' class='label'>:</td>
    <td align='left' valign='top' class='text'>".$dob."</td>
  </tr>
  <tr>
    <td align='left' valign='top' class='label'>Address</td>
    <td align='left' valign='top' class='label'>:</td>
    <td align='left' valign='top' class='text'>".nl2br($address)."</td>
  </tr>
    <tr>
    <td align='left' valign='top' class='label'>Mobile Number</td>
    <td align='left' valign='top' class='label'>:</td>
    <td align='left' valign='top' class='text'>".$mobile."</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>

</table>	
	</td>
    <td  width='15%'>&nbsp;</td>
  </tr>
  <tr>
    <td colspan='3'>&nbsp;</td>
  </tr>  
</table>


</body>
</html>";


$fileatt = "upload/";
$fileatt_type =  "application/doc";
$fileatt_name = $fileatt. $resume;

$email_from = $email;
$email_subject = "Resume- Careers";
$email_txt = "attachment test-body";

$email_to = "aaaa@aaaaaaa.com";

$headers = "From: ".$email_from;

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

$fileatt_name = substr($fileatt_name, strlen($fileatt));

$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-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

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

if ($ok) {
    echo "<p>mail sent!</p>";
	header("location:careers.html?msg=Message Successfully sent"); 

} else {
    echo "<p>mail could not be sent!</p>";
	header("location:careers.html?msg=Message not sent!!"); 

}
?>

Thanks in advance....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.