Last active
September 13, 2019 09:06
-
-
Save hughshen/2dc3b1e384a3ecc5811fd1a7fe81b01a to your computer and use it in GitHub Desktop.
OpenCart 使用 PHPMailer 替换默认的邮件类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Mail { | |
protected $to; | |
protected $from; | |
protected $sender; | |
protected $subject; | |
protected $text; | |
protected $html; | |
protected $attachments = array(); | |
public $protocol = 'mail'; | |
public $hostname; | |
public $username; | |
public $password; | |
public $port = 25; | |
public $timeout = 5; | |
public $newline = "\n"; | |
public $crlf = "\r\n"; | |
public $verp = false; | |
public $parameter = ''; | |
public function setTo($to) { | |
$this->to = $to; | |
} | |
public function setFrom($from) { | |
$this->from = $from; | |
} | |
public function setSender($sender) { | |
$this->sender = $sender; | |
} | |
public function setSubject($subject) { | |
$this->subject = $subject; | |
} | |
public function setText($text) { | |
$this->text = $text; | |
} | |
public function setHtml($html) { | |
$this->html = $html; | |
} | |
public function addAttachment($filename) { | |
$this->attachments[] = $filename; | |
} | |
public function send() { | |
if (!$this->to) { | |
trigger_error('Error: E-Mail to required!'); | |
exit(); | |
} | |
if (!$this->from) { | |
trigger_error('Error: E-Mail from required!'); | |
exit(); | |
} | |
if (!$this->sender) { | |
trigger_error('Error: E-Mail sender required!'); | |
exit(); | |
} | |
if (!$this->subject) { | |
trigger_error('Error: E-Mail subject required!'); | |
exit(); | |
} | |
if ((!$this->text) && (!$this->html)) { | |
trigger_error('Error: E-Mail message required!'); | |
exit(); | |
} | |
if ($this->protocol == 'mail') { | |
if (is_array($this->to)) { | |
$to = implode(',', $this->to); | |
} else { | |
$to = $this->to; | |
} | |
$boundary = '----=_NextPart_' . md5(time()); | |
$header = ''; | |
$header .= 'MIME-Version: 1.0' . $this->newline; | |
$header .= 'Date: ' . date('D, d M Y H:i:s O') . $this->newline; | |
$header .= 'From: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline; | |
$header .= 'Reply-To: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline; | |
$header .= 'Return-Path: ' . $this->from . $this->newline; | |
$header .= 'X-Mailer: PHP/' . phpversion() . $this->newline; | |
$header .= 'Content-Type: multipart/related; boundary="' . $boundary . '"' . $this->newline . $this->newline; | |
if (!$this->html) { | |
$message = '--' . $boundary . $this->newline; | |
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline; | |
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline; | |
$message .= $this->text . $this->newline; | |
} else { | |
$message = '--' . $boundary . $this->newline; | |
$message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $this->newline . $this->newline; | |
$message .= '--' . $boundary . '_alt' . $this->newline; | |
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline; | |
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline; | |
if ($this->text) { | |
$message .= $this->text . $this->newline; | |
} else { | |
$message .= 'This is a HTML email and your email client software does not support HTML email!' . $this->newline; | |
} | |
$message .= '--' . $boundary . '_alt' . $this->newline; | |
$message .= 'Content-Type: text/html; charset="utf-8"' . $this->newline; | |
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline; | |
$message .= $this->html . $this->newline; | |
$message .= '--' . $boundary . '_alt--' . $this->newline; | |
} | |
foreach ($this->attachments as $attachment) { | |
if (file_exists($attachment)) { | |
$handle = fopen($attachment, 'r'); | |
$content = fread($handle, filesize($attachment)); | |
fclose($handle); | |
$message .= '--' . $boundary . $this->newline; | |
$message .= 'Content-Type: application/octet-stream; name="' . basename($attachment) . '"' . $this->newline; | |
$message .= 'Content-Transfer-Encoding: base64' . $this->newline; | |
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $this->newline; | |
$message .= 'Content-ID: <' . basename(urlencode($attachment)) . '>' . $this->newline; | |
$message .= 'X-Attachment-Id: ' . basename(urlencode($attachment)) . $this->newline . $this->newline; | |
$message .= chunk_split(base64_encode($content)); | |
} | |
} | |
$message .= '--' . $boundary . '--' . $this->newline; | |
ini_set('sendmail_from', $this->from); | |
if ($this->parameter) { | |
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header, $this->parameter); | |
} else { | |
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header); | |
} | |
} elseif ($this->protocol == 'smtp') { | |
if (file_exists(DIR_SYSTEM . 'phpmailer/PHPMailerAutoload.php')) { | |
require_once(DIR_SYSTEM . 'phpmailer/PHPMailerAutoload.php'); | |
$mail = new PHPMailer; | |
// $mail->SMTPDebug = 3; | |
$mail->isSMTP(); | |
$mail->Host = $this->hostname; | |
$mail->SMTPAuth = true; | |
$mail->Username = $this->username; | |
$mail->Password = $this->password; | |
if ((int)$this->port == 587) { | |
$mail->SMTPSecure = 'tls'; | |
} else { | |
$mail->SMTPSecure = 'ssl'; | |
} | |
$mail->Port = $this->port; | |
$mail->setFrom($this->from, $this->sender); | |
if (is_array($this->to)) { | |
foreach ($this->to as $to) { | |
$mail->addAddress($to); | |
} | |
} else { | |
$mail->addAddress($this->to); | |
} | |
foreach ($this->attachments as $attachment) { | |
if (file_exists($attachment)) { | |
$mail->addAttachment($attachment); | |
} | |
} | |
$mail->CharSet = 'utf-8'; | |
$mail->isHTML(true); | |
$mail->Subject = $this->subject; | |
$mail->Body = $this->html ? $this->html : $this->text; | |
// https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#user-content-php-56-certificate-verification-failure | |
if (version_compare(PHP_VERSION, '5.6.0', '>=')) { | |
$mail->SMTPOptions = array( | |
'ssl' => array( | |
'verify_peer' => false, | |
'verify_peer_name' => false, | |
'allow_self_signed' => true | |
) | |
); | |
} | |
if (!$mail->send()) { | |
echo "Message could not be sent.\n"; | |
echo "Mailer Error: " . $mail->ErrorInfo; | |
exit(); | |
} | |
} else { | |
echo "PHPMailer does not exists.\n"; | |
exit(); | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment