By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

problem in sending email with attached file

P: 1
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given below.


function SendMail123($From,$FromName,$To,$ToName,$Subject,$ Text,$Html,$AttmFiles){
$OB="----=_OuterBoundary_000";
$IB="----=_InnerBoundery_001";

$Html=$Html?$Html:preg_replace("/\n/","{br}",$Text)
or die("neither text nor html part present.");
$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
$From or die("sender address missing");
$To or die("recipient address missing");
$Html=stripslashes($Html);
$headers ="MIME-Version: 1.0\r\n";
$headers.="From: ".$FromName." <".$From.">\n";
$headers.="To: ".$ToName." <".$To.">\n";
$headers.="Reply-To: ".$FromName." <".$From.">\n";
$headers.="X-Priority: 1\n";
$headers.="X-MSMail-Priority: High\n";
$headers.="X-Mailer: My PHP Mailer\n";
$headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";
//Messages start with text/html alternatives in OB
$Msg ="This is a multi-part message in MIME format.\n";
$Msg.="\n--".$OB."\n";
$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";
//plaintext section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here
$Msg.=$Text."\n\n";
// html section
$Msg.="\n--".$IB."\n";
$Msg.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n";
$Msg.="Content-Transfer-Encoding: base64\n\n";
// html goes here
$Msg.=chunk_split(base64_encode($Html))."\n\n";
// end of IB
$Msg.="\n--".$IB."--\n";
// attachments
if(count($AttmFiles) != 0){

foreach($AttmFiles as $AttmFile){
$patharray = explode ("/", $AttmFile);

$FileName=$patharray[count($patharray)-1];
$Msg.= "\n--".$OB."\n";
$Msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n";
$Msg.="Content-Transfer-Encoding: base64\n";
$Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n";

//file goes here
$fd= @fopen ($AttmFile, "r");
$FileContent= @fread($fd,filesize($AttmFile));
@fclose ($fd);
$FileContent=chunk_split(base64_encode($FileConten t));
$Msg.=$FileContent;
$Msg.="\n\n";
}
}
//message ends
$Msg.="\n--".$OB."--\n";
mail($To,$Subject,$Msg,$headers);

//syslog(LOG_INFO,"Mail: Message sent to $ToName <$To>");
}

if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
//die("hhhhhhhh");
$ATTM=array();
@chmod('uploads/',777);

$target_path = 'uploads/'.$_FILES['textDoc']['name'];

//system("chmod -R 777 ".$target_path);
//@chmod($target_path,777);
if(move_uploaded_file($_FILES['textDoc']['tmp_name'], $target_path)) {

$ATTM[]=$target_path;

}
$message = $_POST["Comments"];
$TEXT=$message;
$HTML=$message ;
$sender_email=$_POST['txtEmail'];
$ret = SendMail123(
$sender_email,$sender_emails, //sender
"aaaaa@gmail.com","Recipients Name", //recipient
"PC - Free Assessment Form", //subject
$TEXT,$HTML,$ATTM); //body and attachment(s)

}




below is the email that i got....

From: <TEST123@TEST123.COM>
To: Recipients Name <aaaaa@gmail.com>
Reply-To: <TEST123@TEST123.COM>
X-Priority: 1
X-MSMail-Priority: High
X-Mailer: My PHP Mailer
Content-Type: multipart/mixed;
boundary="----=_OuterBoundary_000"


This is a multi-part message in MIME format.

------=_OuterBoundary_000
Content-Type: multipart/alternative;
boundary="----=_InnerBoundery_001"


------=_InnerBoundery_001
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

test123


------=_InnerBoundery_001
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: base64

dGVzdDEyMw==




------=_InnerBoundery_001--

------=_OuterBoundary_000
Content-Type: application/octetstream;
name="val1.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="val1.txt"

MTE2MDY3Mzg1NQ==




------=_OuterBoundary_000--




please help to get the actual file with attachment.
Apr 26 '07 #1
Share this Question
Share on Google+
1 Reply


mwasif
Expert 100+
P: 801
I always used PHPMailer to send email attachment. See if this can help you.
Apr 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.