Attachment Email with PHP

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 = ";";
$arr_to = explode(";",$all_to);
foreach($arr_to as $to)
	$subject = "Attachment Email";
	$random = md5(date('r', time()));
	$headers = "From:";
	$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-$random\"";
	$attachment = chunk_split(base64_encode(file_get_contents("$database_$current.sql.gz")));

	$content = "
	Content-Type: application/gzip; name=$database_$current.sql.gz
	Content-Transfer-Encoding: base64
	Content-Disposition: attachment


	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.


One response to “Attachment Email with PHP

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: