Create mail form using PHPmailer

Question

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"><br><br>
<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 = "you@yourdomail.com";
    $phpmailer->FromName = "Your Name";
    $phpmailer->Subject = "Subject Text";
    $phpmailer->Body = "<i>Mail body in HTML</i>";
    $phpmailer->AltBody = "This is the plain text version of the email content";
    $phpmailer->AddAddress("my@mail.com", "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!

0
, kyrie 8 months 0 Answers 96 views 0

Leave an answer