【发布时间】:2014-01-02 12:19:57
【问题描述】:
我遇到了一个问题,PHP 脚本将收集的数据字段作为附件 (array.htm) 发送并且主消息为空白。所以没有附件和空白消息。你能帮帮我吗?
<?php
// Email recipient & email subject
$to = "webmaster@webmaster.com";
$subject = 'This is a test';
// Collected parameters
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST ['telephone'];
$address = $_POST ['address'];
$postcode = $_POST ['postcode'];
$type = $_POST ['type'];
$description = $_POST['description'];
$attachments = $_FILES ['file'];
$path = $_SERVER['DOCUMENT_ROOT'];
if (!empty($_FILES['attachment']['tmp_name'])) {
$path = $_FILES['attachment']['name'];
if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}
//PHP variables
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";
//Printed parameters
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Business Name:</b></td><td>' . $name . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Telephone:</b></td><td>' . $telephone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Address:</b></td><td>' . $address . '</td></tr>';
$message .= '<tr><td width="150px"><b>Postcode:</b></td><td>' . $postcode . '</td></tr>';
$message .= '<tr><td width="150px"><b>Business Type:</b></td><td>' . $type . '</td></tr>';
$message .= '<tr><td width="150px"><b>Description:</b></td><td>' . $description . '</td></tr>';
$message .= '</table>';
// Send mail
mail($to, $subject, $message, $headers);
// Redirect success
$theResults = header("Location: \success.html");
exit;
?>
更新 我将参数“附件”更改为“文件”,现在我根据表单中包含的文件获得了一个正确标记的附件,但附件仍然包含表单信息文本,因此它不是最初附加的真正的 jpg,当然消息仍然是空白的。但是现在我将正确的文件附件上传到脚本运行的服务器区域。这有帮助吗?
<?php
// Email recipient & email subject
$to = "webmaster@webmaster.com";
$subject = 'This is a test';
// Collected parameters
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$type = $_POST['type'];
$description = $_POST['description'];
$file = $_FILES['file'];
$path = $_SERVER['DOCUMENT_ROOT'];
if (!empty($_FILES['file']['tmp_name'])) {
$path = $_FILES['file']['name'];
if (copy($_FILES['file']['tmp_name'], $path)) $file = $path;
}
//PHP variables
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $file . "\"\n\n";
//Printed parameters
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Business Name:</b></td><td>' . $name . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Telephone:</b></td><td>' . $telephone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Address:</b></td><td>' . $address . '</td></tr>';
$message .= '<tr><td width="150px"><b>Postcode:</b></td><td>' . $postcode . '</td></tr>';
$message .= '<tr><td width="150px"><b>Business Type:</b></td><td>' . $type . '</td></tr>';
$message .= '<tr><td width="150px"><b>Description:</b></td><td>' . $description . '</td></tr>';
$message .= '</table>';
// Send mail
mail($to, $subject, $message, $headers);
// Redirect success
$theResults = header("Location: \success.html");
exit;
?>
【问题讨论】:
-
如果您对繁琐的手动邮件/mime 附件构造有疑问,那么这就是您的 问题。其他人都在使用 PHPMailer/SwitftMailer 来简化这些事情。启用
error_reporting将清除一些路径误用。考虑不要覆盖变量。 -
它本身不相关,但添加
html和body标签以查看是否至少先加载消息 -
这只是一个带有单个附件的简单电子邮件表单,我的目标不是安装框架。我喜欢远离那种东西,坚持原来的结构。
-
试试看这个问题可能会帮助你stackoverflow.com/questions/11004377/…
-
你的 HTML 表单是什么?也可以贴一下吗?
标签: php email attachment