1

我在这里有一个 HTML 表单,我可以在其中选择上传文件,然后通过电子邮件将其发送给我。我已经有一个下面给出的 php 脚本,它通过电子邮件发送文本框值和文本区域值等。

现在我想要的是,我希望表格中上传的文件通过电子邮件作为附件接收给我,我将在其中收到表格的其余部分。

contactform.html

<form id="contact form" name="form1" enctype="multipart/form-data" method="post" action="contactformprocess.php">
  <p>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" />
  </p>
  <p>
    <label for="email">Email:</label>
    <input type="text" name="email" id="email" />
  </p>
  <p>
    <label for="message">Message:</label>
    <textarea name="message" id="message" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="fileupload">Upload your file:</label>
    <input type="file" name="fileupload" id="fileupload" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>

和php脚本

contactformprocess.php

<?php

/* Subject and Email Variables */

    $emailSubject = 'Contact Form';
    $webMaster = 'me@myemail.com';

/* Gathering Data Variables */

    $name = $_POST['name'];
    $email = $_POST['email'];
    $question = $_POST['message'];

    $body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Message: $message <br>
EOD;

    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

    $theResults = <<<EOD
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Confirmation Page</title>
<style type="text/css">
.header {
    font-family: Trebuchet MS, Arial, Helvetica, sans-serif;
}
</style>
</head>

<body>
<table width="100%" border="0">
  <tr>
    <td bgcolor="#adbe69" class="header"><strong>Confirmation Page</strong></td>
  </tr>
</table>
<table width="100%" border="0" align="left">
  <tr>
    <td><p align="center">Thank You!</p>
    <p align="center">Your Form is Submitted Succesfully,</p>
    <p align="center">You will recieve an email within 24 to 48 hours regarding your message,</p>
    </td>
  </tr>
</table>
</body>
</html>
EOD;
echo "$theResults";
?>
4

1 回答 1

2

并不是说我会推荐使用 php 脚本来发送带有附件的邮件(而是提供一个下载链接并将文档存储在服务器上以便检索邮件目标)

看看这里:http ://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment

检索附件的部分如下所示:

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 

您将不得不使用服务器(php-)上传目录中的临时文件而不是附件.zip

于 2012-09-17T05:48:15.377 回答