0

0

php socket通过smtp发送邮件(可带附件)

php中文网

php中文网

发布时间:2016-06-20 13:02:40

|

1805人浏览过

|

来源于php中文网

原创

php socket通过smtp发送邮件(可带附件)

//define("SOL", "\n");
define("EOL", "\r\n");
define("SMTP_HOST", "smtp.163.com");//SMTP服务器
define("SMTP_PORT", "25");//SMTP服务器端口
define("SMTP_USER", "");//SMTP服务器的用户帐号
define("SMTP_PASS", "");//SMTP服务器的用户密码


$from = "";//SMTP服务器的用户邮箱
$to = "";//发送给谁 可用逗号隔开多个邮箱
$cc = "";
$bcc = "";

$subject="这是一个由PHP发送的带附件的邮件";//邮件主题 很多客户端会有乱码,所以转一下码
$body = "这个是一个带附件的邮件发送程序
看到没,这里显示了HTM标签哦;请点开链接 ".date('Y-m-d H:i:s');//邮件内容 $smtp = new smtp(SMTP_HOST,SMTP_PORT,SMTP_USER,SMTP_PASS, true);//这里面的一个true是表示使用身份验证,否则不使用身份验证. $smtp->addAttachment("mail.zip"); $smtp->sendmail($to, $from, $subject, $body, $cc, $bcc); class smtp { /* Public Variables */ public $attachments = array(); /* Private Variables */ private $smtp_host; private $smtp_port; private $time_out; private $host_name; private $auth; private $user; private $pass; private $sock; /* Constractor */ public function smtp($smtp_host = null, $smtp_port = null, $user = null, $pass = null, $auth = true) { $this->smtp_host = (!empty($smtp_host)) ? $smtp_host : SMTP_HOST; $this->smtp_port = (!empty($smtp_port)) ? $smtp_port : SMTP_PORT; $this->user = (!empty($user)) ? $user : SMTP_PORT; $this->pass = (!empty($pass)) ? $pass : SMTP_PORT; $this->auth = $auth; $this->time_out = 15; # $this->host_name = "localhost"; $this->sock = FALSE; } /* Main Function */ public function sendmail($to, $from, $subject = "", $body = "", $cc = "", $bcc = "") { $bndp = md5(uniqid("")) . rand(1000, 9999); $bnd = md5(uniqid("")) . rand(1000, 9999); list ($msec, $sec) = explode(" ", microtime()); $mail_from = $this->strip_line_breaks($from); $mail_to = explode(",", $to); $body = preg_replace("/(^|(\r\n))(\\.)/", "", $body); if ($cc != "") $mail_to = array_merge($mail_to, explode(",", $cc)); if ($bcc != "") $mail_to = array_merge($mail_to, explode(",", $bcc)); $headers = "MIME-Version:1.0" . EOL; $headers .= "To: " . $to . EOL; if ($cc != "") { $headers .= "Cc: " . $cc . EOL; } $headers .= "From: $from<" . $from . ">" . EOL; $headers .= "Subject: " . $subject . EOL; $headers .= "Date: " . date("r") . EOL; $headers .= "X-Mailer: Webmail ver 1.0 (PHP Version/" . phpversion() . ")" . EOL; $headers .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $from . ">" . EOL; if (count($this->attachments) > 0) { $headers .= "Content-Type: multipart/mixed;" . EOL . chr(9) . " boundary=\"" . $bndp . "\"" . EOL . EOL; $headers .= '--'.$bndp . EOL; $headers .= 'Content-Type : multipart/alternative; boundary="' . $bnd . '"' . EOL . EOL; $headers .= '--' . $bnd . EOL; $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--' . $bnd . EOL; $headers .= 'Content-type: text/html; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--' . $bnd . '--' . EOL; foreach ($this->attachments as $att) { $headers .= "--" . $bndp . EOL . $att; } $headers .= '--' . $bndp . '--' . EOL; $this->clear_attachments(); } else { $headers .= 'Content-Type : multipart/alternative;boundary="'.$bnd.'"' . EOL . EOL; $headers .= '--'.$bnd . EOL; $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--'.$bnd . EOL; $headers .= 'Content-type: text/html; charset=utf-8' . EOL; $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL; $headers .= $body . EOL; $headers .= '--'.$bnd.'--' . EOL; } $sent = TRUE; foreach ($mail_to as $rcpt_to) { $rcpt_to = $this->strip_line_breaks($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) { $this->log_write("Error: Cannot send email to " . $rcpt_to); $sent = FALSE; continue; } if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headers, $body)) { $this->log_write("E-mail has been sent to <" . $rcpt_to . ">"); } else { $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">"); $sent = FALSE; } fclose($this->sock); } $this->log_write("{$mail_to} send over;"); return $sent; } public function addAttachment($file, $dispo = "attachment") { $file_data = (file_exists($file)) ? file_get_contents($file) : ""; if ($file_data != "") { $filename = basename($file); $ext = pathinfo($filename, PATHINFO_EXTENSION); $chunks = chunk_split(base64_encode($file_data)); $parts = "Content-Type: application/$ext; name=\"" . $filename . "\"" . EOL; $parts .= "Content-Transfer-Encoding: base64" . EOL; $parts .= "Content-Disposition: " . $dispo . "; filename=\"" . $filename . "\"" . EOL . EOL; $parts .= $chunks . EOL . EOL; $this->attachments[] = $parts; } } private function clear_attachments() { unset($this->attachments); $this->attachments = array(); } /* Private Functions */ private function smtp_send($helo, $from, $to, $header, $body = "") { if (!$this->smtp_putcmd("HELO", $helo)) { //$this->log_write("Error: Error occurred while sending HELO command."); return FALSE; } #auth if ($this->auth) { if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { //$this->log_write("Error: Error occurred while sending HELO command."); return FALSE; } if (!$this->smtp_putcmd("", base64_encode($this->pass))) { //$this->log_write("Error: Error occurred while sending HELO command."); return FALSE; } } if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) { //$this->log_write("Error: Error occurred while sending MAIL FROM command."); return FALSE; } if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) { //$this->log_write("Error: Error occurred while sending RCPT TO command."); return FALSE; } if (!$this->smtp_putcmd("DATA")) { //$this->log_write("Error: Error occurred while sending DATA command."); return FALSE; } if (!$this->smtp_message($header, $body)) { //$this->log_write("Error: Error occurred while sending message."); return FALSE; } if (!$this->smtp_eom()) { //$this->log_write("Error: Error occurred while sending . [EOM]."); return FALSE; } if (!$this->smtp_putcmd("QUIT")) { //$this->log_write("Error: Error occurred while sending QUIT command."); return FALSE; } return TRUE; } private function smtp_sockopen($address) { if ($this->smtp_host == "") { return $this->smtp_sockopen_mx($address); } else { return $this->smtp_sockopen_relay(); } } private function smtp_sockopen_relay() { $this->log_write("Trying to Connect " . $this->smtp_host . ":" . $this->smtp_port . "..."); $this->sock = @fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Error: connenct error" . $errstr . " (" . $errno . ")"); return FALSE; } $this->log_write("Connected Ok"); return TRUE; } private function smtp_sockopen_mx($address) { $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address); if (!@getmxrr($domain, $MXHOSTS)) { $this->log_write("Error: Cannot resolve MX \"" . $domain . "\""); return FALSE; } foreach ($MXHOSTS as $host) { $this->log_write("Trying to " . $host . ":" . $this->smtp_port); $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Connect Error ," . $errstr . " (" . $errno . ")"); continue; } $this->log_write("Connected to mx host " . $host); return TRUE; } $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")"); return FALSE; } private function smtp_message($header, $body) { fputs($this->sock, $header . "\r\n" . $body); return TRUE; } private function smtp_eom() { fputs($this->sock, "\r\n.\r\n"); return $this->smtp_ok(); } private function smtp_ok() { $response = str_replace("\r\n", "", fgets($this->sock, 512)); if (!preg_match("/^[23]/", $response)) { fputs($this->sock, "QUIT\r\n"); fgets($this->sock, 512); $this->log_write("Error: Remote host returned \"" . $response . "\""); return FALSE; } return TRUE; } private function smtp_putcmd($cmd, $arg = "") { if ($arg != "") $cmd = ($cmd == "") ? $arg : ($cmd . " " . $arg); fputs($this->sock, $cmd . "\r\n"); return $this->smtp_ok(); } private function strip_line_breaks($address) { $address = preg_replace("/([\t\r\n])+/", "", $address); $address = preg_replace("/^.*<(.+)>.*$/", "", $address); return $address; } public function log_write($message) { $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message; file_put_contents(dirname(__FILE__) . '/mail.log', $message . PHP_EOL, FILE_APPEND | LOCK_EX); } }

 

PHPMailer
PHPMailer

PHPMailer - 一个功能齐全的用于PHP的电子邮件创建和传输类。支持UTF-8内容以及8位、base64、二进制和quoted-printable编码。通过SMTPS和SMTP+STARTTLS传输提供LOGIN、PLAIN、CRAM-MD5和XOAUTH2机制的SMTP身份验证。自动验证电子邮件地址。许多PHP开发人员需要从他们的代码中发送电子邮件。唯一直接支持此功能的PHP函数是mail()。然而,它不提供任何帮助来使用流行的功能,如加密、身份验证、HTML消息和附件。正确格式化电子邮件是令人

下载


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

14

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

12

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

4

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

18

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

golang socket 编程
golang socket 编程

共2课时 | 0.1万人学习

swoole入门物联网开发与实战
swoole入门物联网开发与实战

共15课时 | 1.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号