How to send a xml file in to an email using PHP?



I have a XML created and I want to send it as attachment to an email. How to do that using PHP?


I have this



<?php
$mail_to = "";
$from_mail = "";
$from_name = "";
$reply_to = "";
$subject = "";
$message = "";


/* Attachment File */ // Attachment location



$file_name = "only1.php";
$path = "http://ift.tt/1Gjxhvg;


// Read the file content



$file = $path.$file_name;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));


/* Set the email header */ // Generate a boundary



$boundary = md5(uniqid(time()));


// Email header



$header = "From: ".$from_name." \r\n";
$header .= "Reply-To: ".$reply_to."\r\n";
$header .= "MIME-Version: 1.0\r\n";


// Multipart wraps the Email Content and Attachment



$header .= "Content-Type: multipart/mixed;\r\n";
$header .= " boundary=\"".$boundary."\"";

$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$boundary."\r\n";


// Email content // Content-type can be text/plain or text/html



$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$message_body\r\n";
$message .= "--".$boundary."\r\n";


// Attachment // Edit content type for different file extensions



$message .= "Content-Type: application/php;\r\n";
$message .= " name=\"".$file_name."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"".$file_name."\"\r\n";
$message .= "\r\n".$content."\r\n";
$message .= "--".$boundary."--\r\n";


// Send email



if (mail($mail_to, $subject, $message, $header)) {
echo "Sent";
} else {
echo "Error";
}
?>

No comments:

Post a Comment