Php mail with attachement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
girish24041984
Forum Newbie
Posts: 6
Joined: Sat Sep 25, 2010 2:58 am

Php mail with attachement

Post by girish24041984 »

Here is the code

-------------Php Code ---------------------

<?php


$strname=ucfirst($_REQUEST["strname"]);
$straddress=ucfirst($_REQUEST["straddress"]);
$strcity=ucfirst($_REQUEST["strcity"]);
$strstate=ucfirst($_REQUEST["strstate"]);




$phone=$_REQUEST["strno"];
if($phone != ""){ $strno=$phone; } else { $strno="-"; }

$stremail=$_REQUEST["stremail"];
$strcomments=ucfirst($_REQUEST["strcomments"]);


$strresume_name=$_FILES["strresume"]["name"];
$strresume_type=$_FILES["strresume"]["type"];
$strresume_size=$_FILES["strresume"]["size"];
$strresume_temp=$_FILES["strresume"]["tmp_name"];



if( $strresume_type=="image/jpeg" or $strresume_type=="application/pdf" or $strresume_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $strresume_type=="application/octet-stream" or $strresume_type=="text/plain" or $strresume_type=="application/msword")
{


$message= '


<table cellspacing="0" cellpadding="8" border="0" width="400">
<tr>
<td colspan="2"></td>
</tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strname.'</td>
</tr>

<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$straddress.'</td>
</tr>

<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>City</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strcity.'</td>
</tr>

<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>State</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strstate.'</td>
</tr>

<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Contact No.</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strno.'</td>
</tr>

<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Email</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$stremail.'</td>
</tr>


<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Comments</strong></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strcomments.'</td>
</tr>


</table>




';

// MAIL SUBJECT


$subject = "Mail with doc file attachment";

// TO MAIL ADDRESS


$to="mail id";

/*
// MAIL HEADERS

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: Name <name@name.com>\n";

*/



// MAIL HEADERS with attachment

$fp = fopen($strresume_temp, "rb");
$file = fread($fp, $strresume_size);

$file = chunk_split(base64_encode($file));
$num = md5(time());

//Normal headers


$headers = "From: ".$stremail;
$headers = "\r\n";

$headers .= " MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";

// This two steps to help avoid spam

$headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

// With message

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";

// Attachment headers

$headers .= "Content-Type:".$strresume_type." ";
$headers .= "name=\"".$strresume_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$strresume_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";



// SEND MAIL

@mail($to, $subject, $message, $headers);


fclose($fp);

echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>';
}
else
{
echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>';
//echo "<script>window.location.href='careers.html';</script>";
}

?>

although i am able to send txt file through this code but following are the problems .....

1. No html message body is displaying in the mail at receiving end
2. when i try to send docx file. it says it is corrupted when trying to open it at receiving end.

Please help me out in this.
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Php mail with attachement

Post by robnet »

I'm not sure the built in mail function can handle html.

Either way there are much better ways of sending email through php:

http://swiftmailer.org
http://phpmailer.sourceforge.net
girish24041984
Forum Newbie
Posts: 6
Joined: Sat Sep 25, 2010 2:58 am

Re: Php mail with attachement

Post by girish24041984 »

Hi,

Thanks for your reply.

you are right. and now i am using phpmailer and it works fine for me. just one thing i need to clarify on phpmailer is that do we always need to upload the files on server before sending it to as an attachments .

Thanks,
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Php mail with attachement

Post by robnet »

Well, the file needs to exist on the server otherwise phpmailer won't be able to access it.

You don't have to do this in two steps though - you can upload the file in the same form as the rest of the email.
girish24041984
Forum Newbie
Posts: 6
Joined: Sat Sep 25, 2010 2:58 am

Re: Php mail with attachement

Post by girish24041984 »

Yes, i uploaded the file and send the mail in one step.

anyways thanks for your help :) :) :)

I am new to this forum, i am wondering, is there any "mark as answers "option is here or something like that.
Post Reply