如何在自定义插件中发送电子邮件而不安装任何其他插件,例如“ WP Mail SMTP”

时间:2019-06-15 06:15:45

标签: wordpress smtp

我正在开发一个插件,我想在插入查询工作时发送电子邮件。它正在插入数据,但电子邮件未发送,我不想为此使用任何其他插件。我用Google搜索了它,但是他们提供了每种解决方案需要一个用于发送电子邮件的插件,我不需要为此使用任何其他插件,我希望在插件中自定义代码

if(isset($_POST['submit'])){

    $email = $_POST['email'];
    /*echo $email;
    die; 
 Here email_send_plugin is Database custom table name
    */


if(!empty($email)){
    $insert = $wpdb->insert('email_send_plugin',
        array(
            'email'=>$email
        ));
    if(is_wp_error($insert)){
        echo "Failed to insert email";

    }
    else{
        echo "Email Inserted";
        $to = 'example@gmail.com';
        $subject = 'The subject';
        $body = 'The email body content';
        $headers = array('Content-Type: text/html; charset=UTF-8');

    $email_status = wp_mail( $to, $subject, $body, $headers );
    if($email_status){
        echo "A mail sent to user";

    }
    else{
        echo "Sending failed";

    }
    }

}
else{
    echo "Email field is required";

}



}



 ?>

1 个答案:

答案 0 :(得分:0)

这是您可以插入主题功能的代码。php

remove_action('phpmailer_init', 'swpsmtp_init_smtp');    
add_action( 'phpmailer_init', 'custom_phpmailer_init' );
function custom_phpmailer_init( PHPMailer $phpmailer ) {
    $phpmailer->Host = 'HOSTNAME_HERE';
    $phpmailer->Port = 465; //or different  
    $phpmailer->Username = 'USERNAME_HERE'; 
    $phpmailer->Password = 'PASS_HERE'; 
    $phpmailer->SMTPAuth = true;
    $phpmailer->SMTPSecure = 'ssl';//or tls
    $phpmailer->IsSMTP();
}
function website_email() {
    $sender_email= 'YOURSENDEREMAIL_HERE';
    return $sender_email;
}
add_filter('wp_mail_from','website_email');

因此,只要为主机名,端口,用户名,密码,发件人电子邮件和安全参数提供正确的值,它将开始正常工作。