• Hi guys,

    Sorry, little new to wordpress.
    I can’t seem to find out how to send an email with WP Mail SMTP via Ajax. I’ve heard/read it’s possible – but not entirely sure what endpoint to point at? I’ve built a theme in angular, so would need to access it this way.

    If you could point me in the right direction, that’d be great.

    Cheers,

    D

    https://ww.wp.xz.cn/plugins/wp-mail-smtp/

Viewing 3 replies - 1 through 3 (of 3 total)
  • create your own endpoint in your theme, and from there pass your parameters to wp_mail. then your theme will work with any SMTP plugin, not just one in particular.

    Thread Starter pwlarry

    (@pwlarry)

    thanks for the reply.

    Apologies, but not entirely sure of how to create the endpoint in the theme.
    Have you got a quick example?

    Cheers

    I suppose you could add this to your theme’s functions.php, but I’m not a theme dev, so don’t quote me.

    You’ll need your endpoint function:

    function sendMail() {
        $to = $_POST ['to'];
        $subject = $_POST ['subject'];
        $message = $_POST ['message'];
        wp_mail($to, $subject, $message); // send mail
        wp_send_json_success(); // return and die
    }

    Then you’ll need to hook it into WordPress:

    if (is_admin ()) {
        add_action ( 'wp_ajax_MyAwesomeMailFunction', 'sendMail' );
        add_action ( 'wp_ajax_nopriv_MyAwesomeMailFunction', 'sendMail' );
    }

    .. where MyAwesomeMailFunction is the action you’ll use in your Ajax post.

    Give the codex a read. https://codex.ww.wp.xz.cn/AJAX

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Send with AJAX’ is closed to new replies.