php发送邮件函数,php发邮件简单例子

发布时间:2021-01-13编辑:脚本学堂
分享一个php发邮件的函数代码,php发送邮件代码,用php自定义函数来处理邮件发送,需要的朋友参考下。

例子,php发邮件的小函数。

专题推荐:php怎么发邮件?

代码:
 

复制代码 代码示例:
<?php
/**
 * 记录数据
 */
function runlog($mode = 'SMTP',$b = '',$c = '',$d='') {
}
 
/**
 * 发送邮件
 * @param $toemail 收件人email
 * @param $subject 邮件主题
 * @param $message 正文
 */
function sendmail($toemail, $subject, $message) {
    $config = array (
        'mailsend' => 2,
        'maildelimiter' => 1,
        'mailusername' => 1,
        'server' => 'smtp.qq.com',
        'port' => 25,
        'mail_type' => 1,
        'auth' => 1,
        'from' => '123456@qq.com',
        'auth_username' => '123456@qq.com',
        'auth_password' => 'password',
    );
 
    $charset   = 'utf-8';
    $mail      = $config;
    $from      = $config['from'];
    $mail_type = $config['mail_type'];
 
    //mail 发送模式
    if($mail_type==0) {
        $headers  = 'MIME-Version: 1.0' . "rn";
        $headers .= 'Content-type: text/html; charset='.$charset.'' . "rn";
        $headers .= 'From: <'.$from.'>' . "rn";
        mail($toemail, $subject, $message, $headers);
        return true;
    }
    //邮件头的分隔符
    $maildelimiter = $mail['maildelimiter'] == 1 ? "rn" : ($mail['maildelimiter'] == 2 ? "r" : "n");
    //收件人地址中包含用户名
    $mailusername = isset($mail['mailusername']) ? $mail['mailusername'] : 1;
    //端口
    $mail['port'] = $mail['port'] ? $mail['port'] : 25;
    $mail['mailsend'] = $mail['mailsend'] ? $mail['mailsend'] : 1;
     
    //发信者
    $email_from = $from == '' ? '=?'.$charset.'?B?'."?= <".$from.">" : (preg_match('/^(.+?) <(.+?)>$/',$from, $mats) ? '=?'.$charset.'?B?'.base64_encode($mats[1])."?= <$mats[2]>" : $from);
     
    $email_to = preg_match('/^(.+?) <(.+?)>$/',$toemail, $mats) ? ($mailusername ? '=?'.$charset.'?B?'.base64_encode($mats[1])."?= <$mats[2]>" : $mats[2]) : $toemail;;
     
    $email_subject = '=?'.$charset.'?B?'.base64_encode(preg_replace("/[r|n]/", '', $subject)).'?=';
    $email_message = chunk_split(base64_encode(str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $message))))));
     
    $headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: PHPcms-V9 {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html; charset=".$charset."{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
         
    if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
        runlog('SMTP', "($mail[server]:$mail[port]) CONNECT - Unable to connect to the SMTP server", 0);
        return false;
    }
    stream_set_blocking($fp, true);
 
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != '220') {
        runlog('SMTP', "$mail[server]:$mail[port] CONNECT - $lastmessage", 0);
        return false;
    }
 
    fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')." phpcmsrn");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
        runlog('SMTP', "($mail[server]:$mail[port]) HELO/EHLO - $lastmessage", 0);
        return false;
    }
 
    while(1) {
        if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
            break;
        }
        $lastmessage = fgets($fp, 512);
    }
 
    if($mail['auth']) {
        fputs($fp, "AUTH LOGINrn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334) {
            runlog('SMTP', "($mail[server]:$mail[port]) AUTH LOGIN - $lastmessage", 0);
            return false;
        }
 
        fputs($fp, base64_encode($mail['auth_username'])."rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334) {
            runlog('SMTP', "($mail[server]:$mail[port]) USERNAME - $lastmessage", 0);
            return false;
        }
 
        fputs($fp, base64_encode($mail['auth_password'])."rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 235) {
            runlog('SMTP', "($mail[server]:$mail[port]) PASSWORD - $lastmessage", 0);
            return false;
        }
 
        $email_from = $mail['from'];
    }
 
    fputs($fp, "MAIL FROM: <".preg_replace("/.*<(.+?)>.*/", "1", $email_from).">rn");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        fputs($fp, "MAIL FROM: <".preg_replace("/.*<(.+?)>.*/", "1", $email_from).">rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 250) {
            runlog('SMTP', "($mail[server]:$mail[port]) MAIL FROM - $lastmessage", 0);
            return false;
        }
    }
 
    fputs($fp, "RCPT TO: <".preg_replace("/.*<(.+?)>.*/", "1", $toemail).">rn");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        fputs($fp, "RCPT TO: <".preg_replace("/.*<(.+?)>.*/", "1", $toemail).">rn");
        $lastmessage = fgets($fp, 512);
        runlog('SMTP', "($mail[server]:$mail[port]) RCPT TO - $lastmessage", 0);
        return false;
    }
 
    fputs($fp, "DATArn");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 354) {
        runlog('SMTP', "($mail[server]:$mail[port]) DATA - $lastmessage", 0);
        return false;
    }
 
    $headers .= 'Message-ID: <'.gmdate('YmdHs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_SERVER['HTTP_HOST'].">{$maildelimiter}";
 
    fputs($fp, "Date: ".gmdate('r')."rn");
    fputs($fp, "To: ".$email_to."rn");
    fputs($fp, "Subject: ".$email_subject."rn");
    fputs($fp, $headers."rn");
    fputs($fp, "rnrn");
    fputs($fp, "$email_messagern.rn");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250) {
        runlog('SMTP', "($mail[server]:$mail[port]) END - $lastmessage", 0);
    }
    fputs($fp, "QUITrn");
    return true;
}
?>

您可能感兴趣的文章: