1

I am reusing an HTML Bootstrap page as a static front page on a WordPress site. The front page has a form in it which I want to use as a Contact Me form. I've installed the widely used WP Mail SMTP plugin and it sends mail using the admin testing process. Surprisingly the free version of the plugin even sends to Microsoft Office365 live.com addresses.

I want to learn how (if it possible) to send mail from the HTML form in the static page using the SMTP plugin to send the mail.

I've discovered these pages when reading through questions here before posting and find them insightful but they do not speak to hooking into a plugin:

https://www.sitepoint.com/handling-post-requests-the-wordpress-way/

https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)

I've coded with PHPMailer before but doing so gets TARFU when needing to use SMTP for transactional email; especially with Office365 endpoints. That will be my fallback if I learn I am not going to be able to learn to hook into a plugin to send mail.

Can I or can I not do this and how to do so if I can?

1

You're making it way more complicated than in needs to be.

WP Mail SMTP is a plugin that allows you to have WP's wp_mail() function send through an SMTP account instead of relying on the web server.

You're not, in this case, doing anything specific with WP Mail SMTP. What you're going to do is use wp_mail() - and the result is generic, so it doesn't matter whether you're using WP Mail SMTP, some other plugin, or no plugin. It's all the same.

So what you actually need to know is how to use wp_mail(), which is documented here. By virtue of the fact that you're using WP Mail SMTP, anything you send through wp_mail() will be going through the account specified in the plugin.

Your question does lack some specifics in terms of what you're doing specifically (i.e. how you expect your form to work) and also what you actually know how to do (do you know PHP? I assume you do). So, I'm going to answer the question you have and assume that you know how to get form $_POST results and also how to handle PHP for this.

Wherever you are posting your form to, if it's a non-WP page, you'll need to make sure need to make sure that WordPress is loaded in order to use the wp_mail() function to send your form results. So whether that's the original page itself or another "thank_you" type page, either way it will need to be PHP.

<?php
// Not loading WP templates...
define( 'WP_USE_THEMES', false );

// Load WordPress Core (make sure the path is correct)
require_once( 'path/to/wp-load.php' );

// Assume you know how to check if the form is posted, so this is generic...
if ( $_POST ) {

    $to = 'your_email@address.com';

    $subject = 'Someone sent you a message!';

    // Build the body based on your form...
    $name  = sanitize_text_field( $_POST['name'] );
    $email = sanitize_email( $_POST['email'] );
    $body  = sanitize_textarea( $_POST['message'] );

    $message = "Someone filled out your form as follows: \r\n\r\n";
    $message.= "name: $name \r\n";
    $message.= "email: $email \r\rn";
    $message.= "the message: \r\n";
    $message.= $body;

    // Send the message...
    wp_mail( $to, $subject, $message );

}
  • 1
    I've upvoted and marked as a correct answer and appreciate butlerblog going beyond the call as I also learned something about how to respect WordPress in this context.. – ClintonGallagher Aug 29 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.