Blog > detail

Just my tiny mind

Attachment Email with PHP

26-2-2012 Admin General 2 Comments

Attachment Email with PHP

In this article I will try to share how to send an Attachment Email with PHP. You can use this for sending attachment like file backup. In this case I just we already implementing my previous article Mysql Backup with PHP, and have a file with the name $database_$current.sql.gz.


The Attachment Email with PHP code should be like this:


$all_to = "bla@bla.com;ble@ble.com";

$arr_to = explode(";",$all_to);

foreach($arr_to as $to)

{

$subject = "Attachment Email";

$random = md5(date('r', time()));

$headers = "From: mhseptiadi.wordpress.com";

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-$random\"";

$attachment = chunk_split(base64_encode(file_get_contents("$database_$current.sql.gz")));


$content = "

--PHP-mixed-$random

Content-Type: application/gzip; name=$database_$current.sql.gz

Content-Transfer-Encoding: base64

Content-Disposition: attachment


$attachment

--PHP-mixed-$random--";


if(@mail($to, $subject, $content, $headers))

{echo "Mail sent successfully to ".$to."

";}else{echo "Mail sent failed to ".$to."

";}

}


You can change $database and $current with anything that you need, and also you need to change the $all_to variable. You can send multiple emails by give all the contacts with sparator ";" in $all_to variable. At the end, we just made code to send Attachment Email with PHP.


Good Luck.


Attachment Email with PHP Attachment Email with PHP Attachment Email with PHP

Attachment Email with PHP Attachment Email with PHP Attachment Email with PHP

Attachment Email with PHP

word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word

Attachment Email with PHP

Attachment Email with PHP

Attachment Email with PHP