【问题标题】:How can I use PHP to mail my an attachment?如何使用 PHP 邮寄附件?
【发布时间】:2010-07-10 16:43:13
【问题描述】:

我有一个由输入字段和文件字段组成的基本表单。我有几件事情想要表格做。收集信息(显然)。还有一个上传文件的选项(可能是 .doc、.pdf、.docx),所以我想将附件限制在这些扩展名和 2MB 以下。我所知道的是我必须有我的表格“enctype = multipart”,但这就是我所知道的。

我有以下 PHP 代码:

<?

$to = 'my@email.com';
$subject = 'Contact from your website';
$message = 'From: ' . "\n\n" . 'Name: ' . $_REQUEST['name'] . "\n\n" . 'E-mail: ' . $_REQUEST['email'] . "\n\n" . 'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
            'Reply-To: ' . $email . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";    



$str = $_REQUEST['email'];

if( $str == "E-mail address" || $str == "" )
{
 echo "Please use your browser's back button enter a valid E-mail address";

} else {
 mail ($to, $subject, $message, $headers);
 header("Location: thankyou.html");
}  

?>

如何调整它以允许它发送附件?

如果可能,我希望看到示例。请理解,我对 PHP 非常陌生,直到现在还不需要发送带有附件的 PHP Web 表单。所以我对你的要求是,如果你发布一个示例,请尝试阐明适用于该示例的相应 HTML 表单代码。

任何帮助将不胜感激!

非常感谢, 阿米特

【问题讨论】:

    标签: php html forms webforms


    【解决方案1】:

    要邮寄附件,您可以使用框架方法,也可以自己编写方法。一些sample code 可以是good point to start

    要获取从浏览器发送的文件,您必须使用 FILE 变量。 PHP documentation 很好地解释了如何做到这一点。

    要将文件类型限制为 .doc 和 .pdf,您必须阅读文件的开头并与“真实”的 .doc 或 .pdf 进行比较。更简单的方法是比较文件扩展名,但您网站的用户始终可以将 virus.exe 重命名为 document.pdf。

    【讨论】:

      【解决方案2】:

      尚未测试,但理论上应该可行。

      使用这个使用缓冲区的代码。 (代码来源:http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

      <?php
      //define the receiver of the email
      $to = 'youraddress@example.com';
      //define the subject of the email
      $subject = 'Test email with attachment';
      //create a boundary string. It must be unique
      //so we use the MD5 algorithm to generate a random hash
      $random_hash = md5(date('r', time()));
      //define the headers we want passed. Note that they are separated with \r\n
      $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
      //add boundary string and mime type specification
      $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
      //read the atachment file contents into a string,
      //encode it with MIME base64,
      //and split it into smaller chunks
      $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
      //define the body of the message.
      ob_start(); //Turn on output buffering
      ?>
      --PHP-mixed-<?php echo $random_hash; ?> 
      Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
      
      --PHP-alt-<?php echo $random_hash; ?> 
      Content-Type: text/plain; charset="iso-8859-1"
      Content-Transfer-Encoding: 7bit
      
      Hello World!!!
      This is simple text email message.
      
      --PHP-alt-<?php echo $random_hash; ?> 
      Content-Type: text/html; charset="iso-8859-1"
      Content-Transfer-Encoding: 7bit
      
      <h2>Hello World!</h2>
      <p>This is something with <b>HTML</b> formatting.</p>
      
      --PHP-alt-<?php echo $random_hash; ?>--
      
      --PHP-mixed-<?php echo $random_hash; ?> 
      Content-Type: application/zip; name="attachment.zip" 
      Content-Transfer-Encoding: base64 
      Content-Disposition: attachment 
      
      <?php echo $attachment; ?>
      --PHP-mixed-<?php echo $random_hash; ?>--
      
      <?php
      //copy current buffer contents into $message variable and delete current output buffer
      $message = ob_get_clean();
      //send the email
      $mail_sent = @mail( $to, $subject, $message, $headers );
      //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
      echo $mail_sent ? "Mail sent" : "Mail failed";
      ?> 
      

      【讨论】:

      • 代码存在一些缺陷。不管怎样,我试过了,它没有用。不过感谢您的尝试。
      【解决方案3】:

      使用PHPMailer。它是免费的,运行良好,并且可以轻松使用附件。我已经看到Swiftmailer 在 SO 上也提到了很多,但我自己没有使用过,所以不能多说。

      【讨论】:

      • 我不太明白如何使用PHPMailer。是否有任何示例文件显示如何操作?!
      • 在 PHPMailer 站点的菜单栏上,选择 Products -> PHP Mailer -> PHPMailer Examples。那里有很多东西,从基本的东西到特定的 SMTP 服务器配置
      【解决方案4】:

      如果您可以访问 PEAR 库,这将使添加附件变得轻而易举。看看here

      【讨论】:

      • 我在这里有几个问题:1)我如何访问 PEAR 库/将其安装在服务器上(或者甚至取决于我?!) 2)在示例链接中您显示,它说明如何附加预定文件,例如图像。我希望附加的文件是用户从他们的计算机上传的文件(为了让事情更清楚,我正在制作一个简历数据库,所以我希望我的用户通过表单发送他们的简历)。然后我会用 $file-attached 或类似的东西替换它说“img here”的地方吗?!
      • 获得访问权限 - 如果您拥有服务器并自己管理它,那么安装它应该没有问题 - 如果您不这样做,请询问您的托管公司是否可以使用 PEAR(以及是否可以制作)如果没有,您可以使用)。您不限于可以附加的内容 - 任何文件都可以,无论图像与否。
      • 谢谢。但是我该如何做类似 $_REQUEST['file'] 来代替 ...你知道吗?发送给我的文件是他们计算机上的文件,而不是我的服务器上的文件
      • 其中只有一部分是相关的(上传),但请看一下 - 我相信您可以找出所需的部分。 php-mysql-tutorial.com/wikis/mysql-tutorials/…
      猜你喜欢
      • 1970-01-01
      • 2012-04-18
      • 2014-03-19
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2012-05-23
      • 2012-06-08
      相关资源
      最近更新 更多