phpmailer--用例

来源:互联网 发布:游戏大厅源码 编辑:程序博客网 时间:2024/04/29 15:59
/**
 * 邮件发送
  +----------------------------------------------------------
 * @access public
  +----------------------------------------------------------
 * @param string $sender   发件人信息(地址:名称)
 * @param array $recipient   收件人信息(地址:名称)
 * @param string $subject   邮件主旨
 * @param string $body   邮件内容
 * @param string $attachment 附件列表
 * @param array $Cc   抄送人信息(地址:名称)
 * @param array $Bcc   加密抄送人信息(地址:名称)
 * @param string $Reply   邮件回复接收人信息(地址:名称)
 *  +----------------------------------------------------------
 * @return array $result 发送结果
 */

function postMail($sender, $recipient, $subject, $body, $attachment = '', $Cc = '', $Bcc = '', $Reply = '') {
    require_once('class.phpmailer.php');
    include('class.smtp.php');
    $mail = new PHPMailer;
    $mail->SMTPDebug = 0;
    //  0 = close SMTP debug;1 = errors and messages; 2 = messages only;3= Enable verbose debug output
    $mail->CharSet = "UTF-8"; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
    $mail->isSMTP();                                      // Set mailer to use SMTP serve
    $mail->Host = C('MAIL_SMTP'); //Specify main and backup SMTP servers
    $mail->SMTPAuth = false;                               // Enable SMTP authentication
    //$mail->Username = 'user@example.com';                 // SMTP username
    //$mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = C('MAIL_SMTP_SECURE'); //'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = C('MAIL_SMTP_PORT'); //587;                                    // TCP port to connect to
    $mail->Helo = C('MAIL_SMTP_HELO'); // 'ismetoad';
   
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $sendArray = explode(':', $sender);
    $mail->setFrom($sendArray[0], $sendArray[1]);
    foreach ($recipient as $value) {
        $recipientArray = explode(':', $value);
        $mail->addAddress($recipientArray[0], $recipientArray[1]);
    }
    if (!empty($Reply)) {
        $ReplyArray = explode(':', $Reply);
        $mail->addReplyTo($ReplyArray[0], $ReplyArray[1]);
    }
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    if (!empty($Cc)) {
        foreach ($Cc as $value) {
            $CcArray = explode(':', $value);
            $mail->addCC($CcArray[0], $CcArray[1]);
        }
    }
    //$mail->addBCC('bcc@example.com');
    if (!empty($Bcc)) {
        foreach ($Bcc as $value) {
            $BccArray = explode(':', $value);
            $mail->addCC($BccArray[0], $BccArray[1]);
        }
    }
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    if (!empty($attachment)) {
        if (is_array($attachment)) { // 添加附件
            foreach ($attachment as $file) {
                if (is_array($file)) {
                    is_file($file['path']) && $mail->AddAttachment($file['path'], $file['name']);
                } else {
                    is_file($file) && $mail->AddAttachment($file);
                }
            }
        } else {
            is_file($attachment) && $mail->AddAttachment($attachment);
        }
    }
    //$mail->isHTML(true);                                  // Set email format to HTML
    //$mail->Subject = 'Here is the subject';
    $mail->Subject = $subject;
    //$mail->Body = 'This is the HTML message body <b>in bold!</b>';
    $mail->Body = $body;
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    if (!$mail->send()) {
        $result['result'] = "fail";
        $result['error'] = $mail->ErrorInfo;
    } else {
        $result['result'] = "pass";
    }
    return $result;
}


;

;
   

0 0
原创粉丝点击