2014-11-04 75 views
0

我创建了一个XML,我想将它作为附件发送给一封电子邮件。如何使用PHP来做到这一点?我的代码无法正常工作如何使用PHP将xml文件发送到电子邮件?

我有这个

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

/*附件文件*/ //固定部位

$file_name = "only1.php"; 
$path = "http://66.147.244.92/~homecre1/public_html/Test/only1.php; 

//读取文件内容

$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)); 

/*设置电子邮件标题*/ //生成边界

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

//电子邮件报头

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

//多部分包装了电子邮件内容和附件

$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"; 

//电子邮件内容 //内容类型可以是文本/无格式或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"; 

//附件 //编辑内容不同的文件扩展名

$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"; 

类型//如果你正在使用的PHPMailer库,然后用下面的函数添加的文件发送电子邮件

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

可能重复http://stackoverflow.com/questions/12301358/send-attachments-with-php-邮件) – 2014-11-04 10:04:11

回答

0

$mail->AddAttachment("filename"); 

PHPMailer的库 链接:https://github.com/PHPMailer/PHPMailer

的[?发送带有附件的邮件PHP()](
+0

有没有PHPMailer的任何选项? – user4212650 2014-11-04 10:13:05

+0

请通过邮件发送功能解释如何发送邮件。 http://php.net/manual/en/function.mail.php 你可以为它编写自己的函数。 一个人写了名字为genico功能 公共静态函数prepareAttachment($ path){ – Vijay 2014-11-04 11:12:58

相关问题