WordPress邮件发送方法wp_mail函数(wp内置的phpmailer)

Published
2023-10-20
浏览次数 :  27

首先接收form提交的post(PHP端) :

//check if the data is not empty or what 
        
         $wlf_name = !empty($_POST['wlf_name']) ? sanitize_text_field( $_POST['wlf_name'] ) : '' ;
         $wlf_email = !empty($_POST['wlf_email']) ? sanitize_text_field( $_POST['wlf_email'] ) : '' ;
         $wlf_phone = !empty($_POST['wlf_phone']) ? sanitize_text_field( $_POST['wlf_phone'] ) : '' ;
         $wlf_title = !empty($_POST['wlf_title']) ? sanitize_text_field( $_POST['wlf_title'] ) : '' ;
         $wlf_message = !empty($_POST['message']) ? sanitize_text_field( $_POST['message'] ) : '' ;

然后设置邮件发送的模板:

//寄出邮件之后仍然要备份到数据库
             // Customize your email subject and message
        $subject = '[表单插件]来自' .$wlf_name . '的消息';
        $email_message = $wlf_message;

        // // Set your email address to receive the form submissions
        $to = get_bloginfo( 'admin_email' );

        $headers[]  = 'From:[email protected]';
        // //可以加cc $header[] = 'cc:[email protected]';
        $headers[] = 'Content-Type: text/html';
        $headers[] = 'charset=UTF-8';

        $template = file_get_contents( plugin_dir_path( __FILE__ ) . '/wlf_mail_template.html');

        $template = str_replace('%NAME%', $wlf_name, $template);
        $template = str_replace('%EMAIL%', $wlf_email, $template);
        $template = str_replace('%PHONE%', $wlf_phone, $template);
        $template = str_replace('%CREATE_TIME%', date("Y-m-d H:i:s"), $template);
        $template = str_replace('%TITLE%', $wlf_title, $template);
        $template = str_replace('%MESSAGE%', $wlf_message, $template);
        $template = str_replace('%SITE_DESC%', get_bloginfo('description'), $template);
        $template = str_replace('%SITE_URL%', home_url('/'), $template);

然后去插件跟目录创建邮件的Html模板: 注意要使用html模板当做邮件模板的时候头部一定要添加传递信息 Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Form Submission</title>
    <style>
        /* Reset CSS */
        body, div, p, h1, h2, h3, h4, h5, h6 {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            background-color: #0077b6;
            color: #ffffff;
            text-align: center;
            padding: 20px;
        }

        .header h1 {
            font-size: 24px;
        }

        .content {
            background-color: #ffffff;
            padding: 20px;
        }

        .content p {
            font-size: 16px;
            line-height: 1.5;
        }

        .button {
            text-align: center;
            margin-top: 20px;
        }

        .button a {
            display: inline-block;
            background-color: #0077b6;
            color: #ffffff;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
        }

        .footer {
            background-color: #0077b6;
            color: #ffffff;
            text-align: center;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>来自网站表单的确认信息</h1>
        </div>
        <div class="content">
            <p>你好,</p>
            <p>你的网站接收到了一个新的表单联系信息,下面是信息详情:</p>
            <ul>
                <li><strong>姓名:</strong> %NAME%</li>
                <li><strong>邮件:</strong> %EMAIL%</li>
                <li><strong>手机:</strong> %PHONE%</li>
                <li><strong>留言时间:</strong> %CREATE_TIME%</li>
                <li><strong>留言标题:</strong> %TITLE%</li>
            </ul>
            <p><strong>留言内容:</strong></p>
            <p>%MESSAGE%.</p>
            <div class="button">
                <a href="%SITE_URL%">点击访问网站</a>
            </div>
        </div>
        <div class="footer">
            &copy; 2023 %SITE_DESC% | All rights reserved
        </div>
    </div>
</body>
</html>

一切就绪,用wp_mail发送

// // Send the email
        $result = wp_mail($to, $subject, $template,$headers);
//打印wp mail 错误 
// add_action('wp_mail_failed', 'log_mailer_errors', 10, 1);
// function log_mailer_errors( $wp_error ){
//   $fn = ABSPATH . '/mail.log'; // say you've got a mail.log file in your server root
//   $fp = fopen($fn, 'a');
//   fputs($fp, "Mailer Error: " . $wp_error->get_error_message() ."\n");
//   fclose($fp);
// }


//配置smtp  这样只要你端口开了  通过这个就可以配置smtp 

//add_action( 'phpmailer_init', 'mailer_config', 10, 1);
function mailer_config(PHPMailer $mailer){
  $mailer->IsSMTP();
  $mailer->Host = "mail.telemar.it"; // your SMTP server
  $mailer->Port = 25;
  $mailer->SMTPDebug = 2; // write 0 if you don't want to see client/server communication in page
  $mailer->CharSet  = "utf-8";
}

Top