我遇到一个问题,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;
?>
UPDATE 我将参数'attachment'更改为'file',现在我根据表单中包含的文件获得了正确标记的附件,但附件仍然包含表单信息文本,因此它不是最初附加的真正的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;
?>
答案 0 :(得分:4)
正如马里奥所提到的,我肯定会开始使用像PHPMailer这样的东西。
https://github.com/PHPMailer/PHPMailer
$mail->addAttachment('/var/tmp/file.tar.gz');
从长远来看,它将证明可以简化您使用PHP处理的电子邮件。
答案 1 :(得分:0)
$_POST
['telephone'];
之间的空格您需要删除这些空格,例如
$_POST['telephone'];
....
$_FILES['file'];