search
Homephp教程php手册php mail邮件发送带附件功能

<html>
 <head>
  <title>带附件的邮件发送</title>
 </head>
 <body> 
  <form method="post" name="form1" action="sendmail.php" ENCTYPE="multipart/form-data"> 
   <table> 
    <tr>
     <td>发送人:</td> 
     <td><input type="text" name="from"></td> 
    </tr> 
    <tr>
     <td>收件人:</td> 
     <td><input type="text" name="to"></td> 
    </tr> 
    <tr>
     <td>邮件主题:</td> 
     <td><input type="text" name="subject"></td> 
    </tr> 
    <tr>
     <td>邮件内容:</td> 
     <td><textarea name="body"></textarea></td> 
    </tr> 
    <tr>
     <td>附件上传:</td> 
     <td><input type="file" name="upload_file"></td> 
    </tr> 
    <tr> 
     <td span=2>
      <input type="submit" value="提交"> 
      <input type="reset" value="重置"> 
     </td> 
    </tr> 
   </table> 
  </form> 
 </body> 
</html>

sendmail.php文件代码:

<?php 
//获得表单信息
$from = $_POST[&#39;from&#39;];
$to = $_POST[&#39;to&#39;]; 
$subject = $_POST[&#39;subject&#39;]; 
$body = $_POST[&#39;body&#39;]; 
// 定义分界线 
$boundary = "345894369383"; //分界线是一串无规律的字符
//设置header
$header = "Content-type: multipart/mixed; boundary= $boundary/r/n"; 
$header .= "From:$from/r/n"; 
//获得上传文件的文件内容
$file = $_FILES[&#39;upload_file&#39;][&#39;tmp_name&#39;]; 
//确定上传文件的MIME类型 
$mimeType = $_FILES[&#39;upload_file&#39;][&#39;type&#39;]; 
//获得上传文件的文件名 
$fileName = $_FILES[&#39;upload_file&#39;][&#39;name&#39;]; 
//读取上传文件 
$fp = fopen($file, "r"); //打开文件
$read = fread($fp, filesize($file)); //读入文件
$read = base64_encode($read); //base64编码 
$read = chunk_split($read); //切割字符串
//建立邮件的主体,分为邮件内容和附件内容两部分
$body = "--$boundary 
Content-type: text/plain; charset=iso-8859-1 
Content-transfer-encoding: 8bit 
$body 
--$boundary 
Content-type: $mimeType; name=$fileName 
Content-disposition: attachment; filename=$fileName 
Content-transfer-encoding: base64 
$read 
--$boundary--"; 
//发送邮件 并输出是否发送成功的信息
if(mail($to, $subject,$body,$header)) 
{
echo "信件发送成功"; 
}
else 
{
echo "信件发送失败"; 
}
?>


文章地址:

转载随意^^请带上本文地址!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor