【问题标题】:PHP email script with attachment带附件的 PHP 电子邮件脚本
【发布时间】: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 将清除一些路径误用。考虑不要覆盖变量。
  • 它本身不相关,但添加 htmlbody 标签以查看是否至少先加载消息
  • 这只是一个带有单个附件的简单电子邮件表单,我的目标不是安装框架。我喜欢远离那种东西,坚持原来的结构。
  • 试试看这个问题可能会帮助你stackoverflow.com/questions/11004377/…
  • 你的 HTML 表单是什么?也可以贴一下吗?

标签: php email attachment


【解决方案1】:

正如 mario 提到的,我肯定会开始使用 PHPMailer 之类的东西。

https://github.com/PHPMailer/PHPMailer

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

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

【讨论】:

  • 我知道你在说什么,因为它可能是一个简单的框架修复,但我正在尝试学习更多 PHP,因为我通常会学习我需要知道的内容,然后在新领域进行试验直到我得到对的。老实说,我不是 javascript 中的 Jquery 的粉丝,而且我不知道 PHP 邮件程序之类的 PHP 库的编码效果如何,无论哪种方式,对于我所需要的东西来说,这似乎都是多余的。无意冒犯,我只是喜欢学习:)
  • 我明白了。您可以看到该库是如何实现的,因为它是完全开源的。但是,如果您确实需要将电子邮件作为大型项目的一部分发送,并且需要使用远程 SMTP,PHPMailer 将帮助您完成此操作,而 PHP 的 mail() 只会让您失望。这只是一个经过 10 多年开发并被可能成千上万的用户使用的知名库将为您带来的东西之一。但不会阻止您学习。
  • 但是从长远来看,重新发明轮子可能会伤害到你,并且不会带来任何真正的价值......向他人学习,从他们的成功和失败中学习,即使不是方式也一样好比用低于标准的解决方案解决已经解决的问题更好。无意冒犯,因为我曾经站在你的立场上;)
【解决方案2】:

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

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

【讨论】:

  • 不,这不相关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
相关资源
最近更新 更多