abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

12/15/2020

How to send attachment email using PHP

1) use basic mail function available in PHP 

2) get file type using application/octet-stream

3) form must  enctype="multipart/form-data"

4) sure to use MD5 encryption & chunk_split(base64_encode)

5) to upload file use move_uploaded_file



<?php

$from = $_REQUEST["from"];

$emaila = $_REQUEST["emaila"];

$filea = $_REQUEST["filea"];


if ($filea)

{

function mail_attachment ($from , $to, $subject, $message, $attachment)

{

$fileattachis = $attachment; // Path to the file

$fileattachis_type = "application/octet-stream"; // File Type 

$start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;

$fileattachis_name = substr($attachment, $start, strlen($attachment));

$email_from = $from; //email from

$subject = "New Attachment Message";

$email_subject =  $subject;

$email_txt = $message;

$email_to = $to;

$headers = "From: ".$email_from;

$file = fopen($fileattachis,'rb'); 

$data = fread($file,filesize($fileattachis)); 

fclose($file); 

$msg_txt="\n\n You have recieved a new attachment email from $from";

$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_txt .= $msg_txt;

$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_txt . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileattachis_type};\n" .

" name = \"{$fileattachis_name}\"\n" . //"Content-Disposition: attachment;\n" . 

//" filename = \"{$fileattachis_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 "File Sent Successfully.";

unlink($attachment); // delete a file after attachment sent.

}

else

{

die("Sorry email not sent. Please try again!");

}

}

move_uploaded_file($_FILES["filea"]["tmp_name"], 'temp/'.basename($_FILES['filea']['name']));


mail_attachment("$from", "youremailaddress@gmail.com", "subject", "message", ("temp/".$_FILES["filea"]["name"]));

}

?>


<html>

<head>


<script type = "text/javascript">

function checkfile() {

with(document.filepost) {

if(filea.value ! = "") {

document.getElementById('textis').innerText = "Attaching File ... Please Wait";

}

}

}

</script>


</head>

<body>


 

<form name= "filepost" method="post" action="file.php" enctype="multipart/form-data" id = "file">

<table width = "300" border = "0" cellspacing = "0" cellpadding = "0">

<tr valign = "bottom">

<td height = "20">Name:</td>

</tr>

<tr>

<td>

<input name = "from" type = "text" id = "from" size = "30">

</td>

</tr>

<tr valign = "bottom">

<td height = "20">Email Address:</td>

</tr>

<tr>

<td class = "frmtxt2">

<input name = "emaila" type = "text" id = "emaila" size = "30">

</td>

</tr>

<tr>

<td height = "20" valign = "bottom">Attachment:</td>

</tr>

<tr valign = "bottom">

<td valign = "bottom">

<input name = "filea" type = "file" id = "filea" size = "16">

</td>

</tr>

<tr>

<td height = "40" valign = "middle">

<input name = "Reset2" type = "reset" id = "Reset2" value = "Reset">

<input name = "Submit2" type = "submit" value = "Submit" onClick = "return checkfile()">

</td>

</tr>

</table>


</form>


<div id="textis"></div>


</body>

</html>


No comments:

Post a Comment

An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...