How To Send Mail with Attachment in PHP
Posted by Raj
How To Send Mail with Attachment in PHP
I this tutorial,I will explain how to send mail with attachments in PHP.
I hope this will help you to send email with attachments in PHP.
I this tutorial,I will explain how to send mail with attachments in PHP.
you can send attachments using a multipart/mixed content header.then convert the attachment into a base64 string.We are creating a very simple script with php.
the script sends an email to that address with a pdf file(attached file)that I specify in the script.
Here's a simple php example of sending html mail with attached file:
Example: How To Send Mail with Attachment in PHP
Example: How To Send Mail with Attachment in PHP
<?php
$file_path="files/test.pdf";//// Path to the file
$file_path_type = "application/pdf";
$subject = "Testing File attachment";
$subject = "Testing File attachment";
$headers = "From: ".$from. "\r\n" .
"CC: php@example.com";
$from ="test@example.com"; //From email.
$message = "Hello!!<br>";
$to ="rajesh@example.com"; //To email
$file_path_name="test.pdf";
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Headers
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$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" .
$message .= "\n\n";
//convert the attachment into a base64 string
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$sent = @mail($to, $subject, $message, $headers);
if($sent) {
echo "Sent Succesfully";
} else {
die("Email not Delivered");
}
?>
I hope this will help you to send email with attachments in PHP.
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
From Mail,
how to,
in php,
PHP,
PHP interview questions and answers,
send email with attachment,
Send Mail in PHP,
To Mail
.You can leave a response, or trackback from your own site.
June 6, 2013 at 4:55 PM
Sir
Very easy, very simple..
Thanks sir