【问题标题】:Upload and Send Attachment from PHP Form [duplicate]从 PHP 表单上传和发送附件 [重复]
【发布时间】:2014-02-10 05:31:07
【问题描述】:

尝试在表单上使用最简单的文件上传方法并将文件作为附件发送到我的电子邮件。照片只需要这个。这正在成为一场噩梦,我一直在网上无休止地搜索,并且有太多广泛的 php 编码让我感到厌烦。非常感谢任何帮助或建议。表格提交成功,我收到了电子邮件,但没有附件!请指出我在这里做错了什么?

这是简单的 HTML。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="sendit.php" method="post" name="mainform" enctype="multipart/form-data">
Name
<br />
<input name="name" type="text">

<br />

Email
<br />
<input name="email" type="text">

<br />
Attach File
<input name="photofile" type="file">

<br />
<br />
<input type="submit" name="Submit" value="Send">

</form>
</body>
</html>

这是可爱的 PHP。

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Variables
$name    = $_POST['name'];
$email    = $_POST['email'];

$to  = 'info@thebrlab.com' . ', ';
$to .= $Email; 
$subject = 'This Is Becoming A Headache';

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

// Additional Headers
$headers = "From: $name <$email>\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: ".$name." <".$email.">\r\n";
$headers .= "Reply-to: ".$name." <".$email.">\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>

【问题讨论】:

  • 不,抱歉。那就是射出一个现有的附件。我需要用户能够将文件上传和 PHP 作为附件发送到我的电子邮件。

标签: php


【解决方案1】:

您将在下面这一行中覆盖您之前的@​​987654321@

$headers = "From: $name <$email>\r\n";
      //^------- You need to add the concatenate operator as shown in the below code.

应该是

$headers .= "From: $name <$email>\r\n";

【讨论】:

  • 谢谢,修复了标题。但仍然没有附件。
  • 检查你的 $message 变量。你已经评论了这两行。
  • 我将输入名称作为照片文件。我应该在我的 PHP 中的任何地方调用它吗?
【解决方案2】:

这可能会有所帮助... Send attachments with PHP Mail()?

如果我真的正确理解你在寻找什么,那就更是如此...... http://www.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-using-php/

【讨论】:

  • 我已经尝试过excellentwebworld,但它对我不起作用。我回来了“发送电子邮件时出错。”
猜你喜欢
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 2022-01-12
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多