phpwordpresswp-mail

Trouble sending email using wp_mail on wordpress


im am trying to send emails from my wordpress site using the wp_mail function, however my Inbox remains empty . I am pretty much a beginner. If you know what i am doing worng I would appreciate it if you could help onto the right path.

The code runs on Wordpress

I dont get any errors if I execute it

I have the following code:

In my Index.php file I would like to send the mail upon hitting the following button:

<button class="btn btn-primary px-4 btn-lg" onclick="<?php my_function(); ?>" id="btn-s"> Angebot anfordern</button>

In my functions.php file I have the following 2 functions:

add_action( 'phpmailer_init', 'my_phpmailer' );
function my_phpmailer( $phpmailer ) {
    $phpmailer->isSMTP();     
    $phpmailer->Host = SMTP_HOST;
    $phpmailer->SMTPAuth = SMTP_AUTH;
    $phpmailer->Port = SMTP_PORT;
    $phpmailer->Username = SMTP_USER;
    $phpmailer->Password = SMTP_PASS;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From = SMTP_FROM;
    $phpmailer->FromName = SMTP_NAME;
}
function my_function() {

    $to = '...@gmail.com';
    $subject = 'Here is the subject';
    $message = 'This is the HTML message body <b>in bold!</b>';              

    wp_mail( $to, $subject, $message );
}

The SMTP variables are located in my wp-config.php file

The data regarding the gmail server should be valid

These to lines may be relevant

define ('SMTP_PORT','587');
define ('SMTP_SECURE','tls');

I assume there is an error in the first function in the functions.php.

Thank you in advance for any feedback.

Unfortunately since I coded the contact form directly into the Theme - since its only one site and I wanted to use bootstrap - I cant use contact form plugins. I tried utilizing 'phpmailer', which a couldnt get to work either. Though I feel there isnt much missing in my current attempt.


Solution

  • your php function is firing on page load. after that it wont get called.

    I would suggest you do this via ajax:

    define your ajax url on your page via your enqueued script:

     $args = array( 'ajaxurl' => admin_url( 'admin-ajax.php' ));
     wp_localize_script( 'YOUR_ENQUEUED_SCRIPT', 'site', $args );
    

    then define the ajax function:

    function custom_send_email(){
        
        $to = '...@gmail.com';
        $subject = 'Here is the subject';
        $message = 'This is the HTML message body <b>in bold!</b>';              
        $headers = array('Content-Type: text/html; charset=UTF-8');
        $send_email = wp_mail( $to, $subject, $message, $headers );
       
        
        if( $send_email ){
            wp_send_json_success();
        }
        wp_die();
    }
    
    add_action('wp_ajax_send_email', 'custom_send_email');
    add_action('wp_ajax_nopriv_send_email', 'custom_send_email');
    

    then in your js:

    $( "#btn-s" ).on( "click", function() {
            let data = {
                'action' : 'send_email',
            };
            $.post(site.ajaxurl, data, function(response) {
    
            if(response.success === true){
                console.log('email sent');
    
            }
    
            });
        });