带有附件的PHP电子邮件脚本


PHP email script with attachment

我遇到了一个问题,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;
?>

更新我将参数"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;
?>

正如mario所说,我肯定会开始使用类似PHPMailer的东西。

https://github.com/PHPMailer/PHPMailer

$mail->addAttachment('/var/tmp/file.tar.gz');

从长远来看,它将证明使用PHP可以简化您的电子邮件处理。

$_POST['telephone'];之间的空间您需要删除这些空间,如

$_POST['telephone'];
  ....
$_FILES['file'];