您现在的位置是:网站首页> 编程资料编程资料
PHP laravel使用自定义邮件类实现发送邮件_php技巧_
2023-05-24
371人已围观
简介 PHP laravel使用自定义邮件类实现发送邮件_php技巧_
当登录邮箱为腾讯企业邮箱的时候。
Phpmailer发送邮件就不好用了,具体哪里不好用,我没真没找到。
但是,邮件得发啊,怎么办呢?
我这里搞了一个自定义的发送邮件类,腾讯企业邮箱也可用。
但是,邮件发送失败,不会返回报错信息,这个可能是有点坑。
源码如下:
_debug) { echo $msg, '
', "\n"; } } public function setDebug($val = true) { $this->_debug = $val; } /** * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行 * @access public * @param string $server 代理服务器的ip或者域名 * @param string $username 认证账号 * @param string $password 认证密码 * @param int $port 代理服务器的端口,smtp默认25号端口 * @param boolean $isSecurity 到服务器的连接是否为安全连接,默认false * @return boolean */ public function setServer($server, $username = "", $password = "", $port = 25, $isSecurity = false) { $this->_sendServer = $server; $this->_port = $port; $this->_isSecurity = $isSecurity; $this->_userName = empty($username) ? "" : base64_encode($username); $this->_password = empty($password) ? "" : base64_encode($password); return true; } /** * 设置发件人 * @access public * @param string $from 发件人地址 * @return boolean */ public function setFrom($from) { $this->_from = $from; return true; } /** * 设置收件人,多个收件人,调用多次. * @access public * @param string $to 收件人地址 * @return boolean */ public function setReceiver($to) { if (isset($this->_to)) { if (is_string($this->_to)) { $this->_to = array($this->_to); $this->_to[] = $to; return true; } elseif (is_array($this->_to)) { $this->_to[] = $to; return true; } else { return false; } } else { $this->_to = $to; return true; } } /** * 设置抄送,多个抄送,调用多次. * @access public * @param string $cc 抄送地址 * @return boolean */ public function setCc($cc) { if (isset($this->_cc)) { if (is_string($this->_cc)) { $this->_cc = array($this->_cc); $this->_cc[] = $cc; return true; } elseif (is_array($this->_cc)) { $this->_cc[] = $cc; return true; } else { return false; } } else { $this->_cc = $cc; return true; } } /** * 设置秘密抄送,多个秘密抄送,调用多次 * @access public * @param string $bcc 秘密抄送地址 * @return boolean */ public function setBcc($bcc) { if (isset($this->_bcc)) { if (is_string($this->_bcc)) { $this->_bcc = array($this->_bcc); $this->_bcc[] = $bcc; return true; } elseif (is_array($this->_bcc)) { $this->_bcc[] = $bcc; return true; } else { return false; } } else { $this->_bcc = $bcc; return true; } } /** * 设置邮件附件,多个附件,调用多次 * @access public * @param string $file 文件地址 * @return boolean */ public function addAttachment($file) { if (!file_exists($file)) { $this->_errorMessage = "file " . $file . " does not exist."; return false; } if (isset($this->_attachment)) { if (is_string($this->_attachment)) { $this->_attachment = array($this->_attachment); $this->_attachment[] = $file; return true; } elseif (is_array($this->_attachment)) { $this->_attachment[] = $file; return true; } else { return false; } } else { $this->_attachment = $file; return true; } } /** * 设置邮件信息 * @access public * @param string $body 邮件主题 * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本 * @return boolean */ public function setMail($subject, $body) { $this->_subject = $subject; $this->_body = base64_encode($body); return true; } /** * 发送邮件 * @access public * @return boolean */ public function send() { $command = $this->getCommand(); $this->_isSecurity ? $this->socketSecurity() : $this->socket(); foreach ($command as $value) { $result = $this->_isSecurity ? $this->sendCommandSecurity($value[0], $value[1]) : $this->sendCommand($value[0], $value[1]); if ($result) { continue; } else { return false; } } //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放 $this->_isSecurity ? $this->closeSecutity() : $this->close(); return true; } /** * 返回错误信息 * @return string */ public function error() { if (!isset($this->_errorMessage)) { $this->_errorMessage = ""; } return $this->_errorMessage; } /** * 返回mail命令 * @access protected * @return array */ protected function getCommand() { $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符 $command = array( array("HELO sendmail\r\n", 250) ); if (!empty($this->_userName)) { $command[] = array("AUTH LOGIN\r\n", 334); $command[] = array($this->_userName . "\r\n", 334); $command[] = array($this->_password . "\r\n", 235); } //设置发件人 $command[] = array("MAIL FROM: <" . $this->_from . ">\r\n", 250); $header = "FROM: <" . $this->_from . ">\r\n"; //设置收件人 if (is_array($this->_to)) { $count = count($this->_to); for ($i = 0; $i < $count; $i++) { $command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250); if ($i == 0) { $header .= "TO: <" . $this->_to[$i] . ">"; } elseif ($i + 1 == $count) { $header .= ",<" . $this->_to[$i] . ">\r\n"; } else { $header .= ",<" . $this->_to[$i] . ">"; } } } else { $command[] = array("RCPT TO: <" . $this->_to . ">\r\n", 250); $header .= "TO: <" . $this->_to . ">\r\n"; } //设置抄送 if (isset($this->_cc)) { if (is_array($this->_cc)) { $count = count($this->_cc); for ($i = 0; $i < $count; $i++) { $command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250); if ($i == 0) { $header .= "CC: <" . $this->_cc[$i] . ">"; } elseif ($i + 1 == $count) { $header .= ",<" . $this->_cc[$i] . ">\r\n"; } else { $header .= ",<" . $this->_cc[$i] . ">"; } } } else { $command[] = array("RCPT TO: <" . $this->_cc . ">\r\n", 250); $header .= "CC: <" . $this->_cc . ">\r\n"; } } //设置秘密抄送 if (isset($this->_bcc)) { if (is_array($this->_bcc)) { $count = count($this->_bcc); for ($i = 0; $i < $count; $i++) { $command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250); if ($i == 0) { $header .= "BCC: <" . $this->_bcc[$i] . ">"; } elseif ($i + 1 == $count) { $header .= ",<" . $this->_bcc[$i] . ">\r\n"; } else { $header .= ",<" . $this->_bcc[$i] . ">"; } } } else { $command[] = array("RCPT TO: <" . $this->_bcc . ">\r\n", 250); $header .= "BCC: <" . $this->_bcc . ">\r\n"; } } //主题 $header .= "Subject: " . $this->_subject . "\r\n"; if (isset($this->_attachment)) { //含有附件的邮件头需要声明成这个 $header .= "Content-Type: multipart/mixed;\r\n"; } elseif (false) { //邮件体含有图片资源的需要声明成这个 $header .= "Content-Type: multipart/related;\r\n";
相关内容
- PHP反序列化漏洞实例深入解析_php技巧_
- PHP开发技巧之PHAR反序列化详解_php技巧_
- PHP create_function()函数应用实例详解_php技巧_
- 一文带你搞懂PHP对象注入_php技巧_
- PHP 在 Microsoft Windows 下的命令行方式_php实例_
- PHP Array 数组详细介绍_php技巧_
- PHP接入Apple对access_token/identityToken进行JWT验证流程详解_php技巧_
- PHP数据加密方式梳理介绍_php技巧_
- PHP CURLFile函数模拟实现文件上传示例详解_php技巧_
- php邮件发送功能实现详解_php技巧_
点击排行
本栏推荐
