PHPMailer是一个用于发送电子邮件的PHP函数包。直接用PHP就可以发送,无需搭建复杂的Email服务。

邮箱相关信息定义

define('MAIL_TO', 'xxxxx@gmail.com');
define('MAIL_FROM', 'xxxxx@gmail.com');
define('MAIL_PASSWORD', '123456');
define('MAIL_FROM_NAME', 'mail');
define('MAIL_SERVER', 'smtp.gmail.com');
define('MAIL_PORT', '25');

邮件发送php示例代码

function send_mail($mail_server,$from_address,$from_name='',$to_address,$Bcc_address='',$body='',$subject='',$htmlflag = false,$Cc_address = array()){
   
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Port = MAIL_PORT;
    $mail->Host = $mail_server;
    $mail->Sender = $from_address;
    $mail->Username = $from_address;
    $mail->Password = MAIL_PASSWORD;
    $mail->From = $from_address;
    $mail->FromName = $from_name;
    $mail->SetLanguage("zh","/language/");
    $mail->CharSet = "ISO-2022-JP";
    $mail->Encoding = "7bit";
    $mail->MimeHeader = "B";
    $mail->MultibyteFlag = "TRUE";
    $mail->SMTPDebug = false;
    $mail->IsHTML($htmlflag);
    $mail->Subject = $subject;
    $mail->Body = mb_convert_encoding($body,$mail->CharSet,"UTF-8");

    $tmp_to_address = explode(";", $to_address);
    for($i = 0; $i < count(explode(";", $to_address)); $i++)
    {
        $mail->AddAddress($tmp_to_address[$i]);
    }

    if($Bcc_address != ''){
        $mail->AddBCC($Bcc_address);
    }
    if(sizeof($Cc_address)){
        foreach($Cc_address as $cc){
            $mail->AddCC($cc);
        }
    }

    ga4_error_log(print_r($mail,1));
    if($mail->send()){
        echo 'OK';
    }else{
        echo 'NG';
        echo 'ErrorInfo : ' . $mail->ErrorInfo;
    }
}

调用示例

    require 'class.phpmailer.php';
    require 'class.smtp.php';
    $mail_to_admin = MAIL_TO;
    $mail_body = '123';
    $mail_subject = '邮件标题';
    ga4_send_mail(MAIL_SERVER, MAIL_FROM, MAIL_FROM_NAME, $mail_to_admin, '', $mail_body, $mail_subject);

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注