php脚本发送带文件附件的邮件

时间:2012-09-17 05:43:59

标签: php javascript html forms email

我在这里有一个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";
?>

1 个答案:

答案 0 :(得分: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