php简单实现发送带附件的邮件,php发送附件邮件
本文实例讲述了php简单实现发送带附件的邮件。分享给大家供大家参考。具体如下:
下面是静态html代码:
<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['from']; $to = $_POST['to']; $subject = $_POST['subject']; $body = $_POST['body']; // 定义分界线 $boundary = "345894369383"; //分界线是一串无规律的字符 //设置header $header = "Content-type: multipart/mixed; boundary= $boundary/r/n"; $header .= "From:$from/r/n"; //获得上传文件的文件内容 $file = $_FILES['upload_file']['tmp_name']; //确定上传文件的MIME类型 $mimeType = $_FILES['upload_file']['type']; //获得上传文件的文件名 $fileName = $_FILES['upload_file']['name']; //读取上传文件 $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 "信件发送失败"; } ?>
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
