PHP – Send Email with Attachments

Question: How PHP send email with attachments?

Answer: You do not need to create or write a file for a text file, or html file, or php file, or even image file. All you need to do is think of a name for the file, make a link or button and point them to this script below:

<?php

$from_name = “My Name”;
$from_mail = “sender@email.com”;
$replyto = “sender@email.com”;
$subject = “This is the Subject Line”;
$message = “This is the message “;
$message .= “\r\n\r\nThis is the following message, in new line”;
$message .= “\r\n\tThis one is new line with tab”;
$mailto = “recipient@email.com;
$content = “the-content-to-be-attached–in-string”;
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = “From: “.$from_name.” <“.$from_mail.”>\r\n”;
$header .= “Reply-To: “.$replyto.”\r\n”;
$header .= “MIME-Version: 1.0\r\n”;
$header .= “Content-Type: multipart/mixed; boundary=\””.$uid.”\”\r\n\r\n”;
$header .= “This is a multi-part message in MIME format.\r\n”;
$header .= “–“.$uid.”\r\n”;
$header .= “Content-type:text/plain; charset=iso-8859-1\r\n”;
$header .= “Content-Transfer-Encoding: 7bit\r\n\r\n”;
$header .= $message.”\r\n\r\n”;
$header .= “–“.$uid.”\r\n”;
$header .= “Content-Type: application/octet-stream; name=\”the-file-name.txt\”\r\n”;
$header .= “Content-Transfer-Encoding: base64\r\n”;
$header .= “Content-Disposition: attachment; filename=\”the-file-name.txt\”\r\n\r\n”;
$header .= $content.”\r\n\r\n”;
$header .= “–“.$uid.”–“;
if (mail($mailto, $subject, “”, $header)) {
echo “<script>alert(\”Email has been sent.\”);</script>”;
} else {
echo “<script>alert(\”Email not sent.\”);</script>”;
}

?>

That’s all folks…

This entry was posted in PHP. Bookmark the permalink.

Tinggalkan Jawapan

Please log in using one of these methods to post your comment:

WordPress.com Logo

Anda sedang menulis komen melalui akaun WordPress.com anda. Log Out / Tukar )

Twitter picture

Anda sedang menulis komen melalui akaun Twitter anda. Log Out / Tukar )

Facebook photo

Anda sedang menulis komen melalui akaun Facebook anda. Log Out / Tukar )

Google+ photo

Anda sedang menulis komen melalui akaun Google+ anda. Log Out / Tukar )

Connecting to %s