skip to Main Content

I am using SMTP to send an email. for that, I used the WP Mail SMTP plugin ( https://wordpress.org/plugins/wp-mail-smtp/ ) I tested the mail with the WP Mail SMTP plugin and it works. Now I want to send a mail with the SMTP function which I created in the WordPress template

Here is my template file code

<?PHP 
/* Template Name: smt */
get_header(); 

        require '/PHPMailer/class.PHPMailer.php';
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "******";
        $mail->Username = "******";
        $mail->Password = "***********";
        $mail->SMTPAuth   = true;
        $mail->SMTPSecure = "tls";
        $mail->Port       = 587;
        $mail->From = "no-reply@*****.com";
        $mail->FromName = "zaata";
        $mail->AddAddress('[email protected]');
        $mail->IsHTML(true);
        $mail->Subject = "zaata Varification code";
        $mail->Body    = "testmail";
        $mail->IsHTML(true);                      
        if(!$mail->Send())
        {
            echo 'false';
        }
            echo 'send';
    }

 ?>

It show me itsugestion.com/:1 GET *******.com/dev/smt/ 500 (Internal Server Error) in console

Please help me how can I set up the SMTP in WordPress template
Any help is appreciated.

2

Answers


  1. As I mentioned in the comment to your question its best to just use wp_mail() as the plugin makes it use smtp, as for the error 500 your missing a the else part of the if statement

    <?PHP 
    /* Template Name: smt */
    get_header(); 
    
            require '/PHPMailer/class.PHPMailer.php';
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->Host = "******";
            $mail->Username = "******";
            $mail->Password = "***********";
            $mail->SMTPAuth   = true;
            $mail->SMTPSecure = "tls";
            $mail->Port       = 587;
            $mail->From = "no-reply@*****.com";
            $mail->FromName = "zaata";
            $mail->AddAddress('[email protected]');
            $mail->IsHTML(true);
            $mail->Subject = "zaata Varification code";
            $mail->Body    = "testmail";
            $mail->IsHTML(true);                      
            if(!$mail->Send())
            {
                echo 'false';
            } else {
                echo 'send';
            }
     ?>
    
    Login or Signup to reply.
  2. Function.php

       add_action('wp_ajax_mail_before_submit', 'my_function');
    
    function my_function(){
        include_once( ABSPATH . '/wp-includes/pluggable.php' );
    
        // check_ajax_referer('my_email_ajax_nonce');
        if ( isset($_POST['action']) && $_POST['action'] == "mail_before_submit" ){
    
       //send email  wp_mail( $to, $subject, $message, $headers, $attachments ); 
       wp_mail($_POST['toemail'], $_POST['headline'], $_POST['message'], 'Headline Hospowork');
        echo 'email sent';
        die();
            
        }
        echo 'error';
        die();
    }
    

    JQuery

    let data = {
      toemail: thesenderEmail, // change this to the email field on your form
      action: 'mail_before_submit',
     _ajax_nonce: nonce,
      headline: 'Invitation',
      message: 'Hello'
     };
    
    $.post(__wanted__api.ajaxurl, data, function (res) {
      console.log('Got this from the server: ' + res);
    });
    

    This is what worked to me. but my problem is the wp_mail() is still return true. even the SMTP got an error or did not really send. What I want is wp_mail() should not proceed if the SMTP got an error.

    if you add that

    if(!$mail->Send())
     {
      echo 'false';
     } else {
     echo 'send';
    }
    

    That produces the Ajax error on the console.

    Now this is my config in wp-config.

    define( 'SMTP_HOST', 'smtp.gmail.com');
    define( 'SMTP_USER', 'yourSmtpEmail');
    define( 'SMTP_PASS', 'yourPassword');
    define( 'SMTP_FROM', 'yourSmtpEmail');
    define( 'SMTP_NAME', 'yourSmtpEmail or any');
    define( 'SMTP_PORT', '465');
    define( 'SMTP_SECURE', 'ssl');
    define( 'SMTP_AUTH', true );
    

    They work all fine except handling the SMTP Error before wp_mail() is to send.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search