Create mail form using PHPmailer

I'm learning about coding plugin.

I trying to create a form in plugin page and it can send mail.

This is my code, but it doesn't work(I did not receive email and did not show any error).

add_action('admin_menu', 'add_contact_us_page');

function add_contact_us_page() {
     add_menu_page(
        __( 'Contact Us', 'textdomain' ),
        __( 'Contact Us','textdomain' ),
        'manage_options',
        'support_form_page',
        'support_form',
        ''
    );
}

 function support_form() {
     ?
    form action="" method="post" enctype="text/plain"
Name:br
input type="text" name="name"br
E-mail:br
input type="text" name="email"br
Message:br
input type="text" name="message" size="50"brbr
input type="submit" value="save"
/form

?php
 }


if ( isset($_POST["submit"])) {
    do_action('my_phpmailer_example');
}

add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
    $phpmailer-isSMTP();     
    $phpmailer-Host = 'smtp.sendgrid.net';
    $phpmailer-SMTPAuth = true; 
    $phpmailer-Port = 587;
    $phpmailer-Username = '****';
    $phpmailer-Password = '****';
    $phpmailer-SMTPSecure = "tls"; 
    $phpmailer-From = "[email protected]";
    $phpmailer-FromName = "Your Name";
    $phpmailer-Subject = "Subject Text";
    $phpmailer-Body = "iMail body in HTML/i";
    $phpmailer-AltBody = "This is the plain text version of the email content";
    $phpmailer-AddAddress("[email protected]", "name"); 
    $phpmailer-SMTPDebug = true;
    $smtp_debug = ob_get_clean();
    if(!$phpmailer-send()) 
     {
    echo "Mailer Error: " . $phpmailer-ErrorInfo;
     } 
      else 
      {
     echo "Message has been sent successfully";
     echo $smtp_debug;
    }
}

Please help, thanks!

Topic smtp phpmailer Wordpress

Category Web


Just for the record, in case anyone looks at this later, there are a few issues with the original code. The first is with this line:

do_action('my_phpmailer_example');

do_action() expects the name of an action/hook and will execute all of the functions associated with that hook. In you're line, you called it with the name of the function you defined, not the name of the action. Usually you don't ever have to use do_action(). WordPress itself calls the 'phpmailer_init' action at the appropriate time. In your case, you've used the name of the function you were trying to call. So you told WP to do was to look for a hook named 'my_phpmailer_example' and execute all actions added to it. That's why your line did nothing when the form was submitted.

Second, you're combining two different tasks. You want to take the sending code out of your function that you've associated with the 'phpmailer_init' hook. That's only used for initializing phpmailer - setting the servers and from values, that sort of thing. the 'phpmailer_init' action will be executed every time WordPress prepares itself to send emails. And you don't want your plugin sending your emails when WordPress even just starts up. So take all of the stuff that doesn't apply for EVERY email WordPress sends out out of my_phpmailer_example().

Third, what you need is the wp_mail function. That would look something like this:

if ( isset( $_POST['submit'] ) ) {
   $to = '[email protected]';
   $subject = 'The subject';
   $body = 'The email body content';
   $headers = array('Content-Type: text/html; charset=UTF-8');
   wp_mail( $to, $subject, $body, $headers );
}

But you would use your form submissions. You can read about that function here: https://developer.wordpress.org/reference/functions/wp_mail/

Fourth, you need to build in security into your form or everyone and their uncle will use it to send spam. Look up how to use nonces on WordPress forms and current_user_can() and how to properly filter what your user has supplied, if you going to allow html emails.

Those topics should get anyone looking into this pointed in the right direction.

Cheers all!


I suggest you first of all to separate code from markup, especially dividing your executing file like the function support_form().

Next, please try to send email without form with hard coded message. It is possible you have mis-configuration on your server if you developing on local machine.

And finally, you should fill action attribute for <form> tag. And that must refer to the executable script. Otherwise it tries to execute you homepage where this code is absent. If you are not planning to separate your markup and code I suppose you should link your action to the file that contains this script. When that happens, your code

if ( isset($_POST["submit"])) {
   do_action('my_phpmailer_example');
}

will execute.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.