/** * 快捷发送一封邮件 * @param string $to 收件人 * @param string $sub 邮件主题 * @param string $msg 邮件内容(HTML) * @param array $att 附件,每个键为文件名称,值为附件内容(可以为二进制文件),例如array('a.txt' => 'abcd' , 'b.png' => file_get_contents('x.png')) * @return bool 成功:true 失败:错误消息 */ public static function mail($to, $sub = '无主题', $msg = '无内容', $att = array()) { if (defined("SAE_MYSQL_DB") && class_exists('SaeMail')) { $mail = new SaeMail(); $options = array('from' => option::get('mail_name'), 'to' => $to, 'smtp_host' => option::get('mail_host'), 'smtp_port' => option::get('mail_port'), 'smtp_username' => option::get('mail_smtpname'), 'smtp_password' => option::get('mail_smtppw'), 'subject' => $sub, 'content' => $msg, 'content_type' => 'HTML'); $mail->setOpt($options); $ret = $mail->send(); if ($ret === false) { return 'Mail Send Error: #' . $mail->errno() . ' - ' . $mail->errmsg(); } else { return true; } } else { $From = option::get('mail_name'); if (option::get('mail_mode') == 'SMTP') { $Host = option::get('mail_host'); $Port = intval(option::get('mail_port')); $SMTPAuth = (bool) option::get('mail_auth'); $Username = option::get('mail_smtpname'); $Password = option::get('mail_smtppw'); $Nickname = option::get('mail_yourname'); if (option::get('mail_ssl') == '1') { $SSL = true; } else { $SSL = false; } $mail = new SMTP($Host, $Port, $SMTPAuth, $Username, $Password, $SSL); $mail->att = $att; if ($mail->send($to, $From, $sub, $msg, $Nickname)) { return true; } else { return $mail->log; } } else { $name = option::get('mail_yourname'); $mail = new PHPMailer(); $mail->setFrom($From, $name); $mail->addAddress($to); $mail->Subject = $sub; $mail->msgHTML($msg); $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; foreach ($att as $n => $d) { $mail->addStringAttachment($d, "=?UTF-8?B?" . base64_encode($n) . "?=", 'base64', get_mime(get_extname($n))); } if (!$mail->send()) { return $mail->ErrorInfo; } else { return true; } } } }
private function smtp_sendatt() { $head = ''; foreach ($this->att as $n => $v) { $head .= "\r\n\r\n" . '--' . $this->part_boundary; $head .= "\r\n" . 'Content-Type: ' . get_mime(get_extname($n)) . '; charset="utf-8"; name="' . $n . '"'; $head .= "\r\n" . 'Content-Disposition: attachment; filename="' . $n . '"'; $head .= "\r\n" . 'Content-Transfer-Encoding: base64'; $head .= "\r\n\r\n" . base64_encode($v); } return fputs($this->sock, $head . "\r\n"); }