I have a custom HTML form with input types and checkboxes. When I submit this form I want to send a mail will all the form details from the same php. How do I send the email from WordPress? I am pretty new and would thank for ur help.

up vote 3 down vote accepted

wp_mail is the function you are looking for.

You can take the posted form data ($_POST['email'], for example) and then use it to build and execute the wp_mail function. The example below was taken from https://developer.wordpress.org/reference/functions/wp_mail/#user-contributed-notes

$to = $_POST['email']; //sendto@example.com
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');

wp_mail( $to, $subject, $body, $headers );

Also the above script would need to check for malicious attacks or bad input from the user but the wp_mail function will allow you to send email.

Source: https://developer.wordpress.org/reference/functions/wp_mail/

  • do i add it to the same php file as my form? – Sourav Oct 25 '17 at 17:33
  • It depends on how you are handling your forms. If you have the same PHP running on the template page as your form then you can (though I wouldn't recommend it). I would check out these two links for an idea on how to handle forms in PHP (w3schools.com/php/php_forms.asp and w3schools.com/php/php_form_complete.asp). You can also use a plugin like Contact Form 7 (wordpress.org/plugins/contact-form-7) if you want to not write the PHP and just work within the WP-Admin. – Tom Oct 25 '17 at 17:36
  • Thanks can i write the body as a custom html so that my mail can look unique? – Sourav Oct 25 '17 at 18:52
  • @Sourav Yes you would insert that into the $body. – Tom Oct 25 '17 at 19:48
  • I have a small doubt i dont have access to the complete files . can i do this using the admin dashboard? which file should i edit for this? – Sourav Oct 26 '17 at 6:30

If you don't want to write your own code (and the mail form), consider a 'contact form' plugin. I use (and recommend) the Contact Form 7 plugin. It allows you to create any form, customize the look/feel/fields, and the mail that is sent out. Very powerful and popular plugin. Lots of other plugins available to add additional fields.

The only minor issue is sparse and sometimes hard-to-find (and sometimes older) technical documentation (like available filters). But I've worked around that.

  • well i have a custom form can i use contact form plugin to send the mails ? – Sourav Oct 26 '17 at 6:29
  • You will have to 'build' the form via the plugin's interface. Although it might be possible to use CF7's 'submit' process, you would have to set many CF7 variables for it to work well, and those variables are not well-documented; you'd have to go through their code. Easiest to use CF7's form builder, I'd think. – Rick Hellewell Oct 26 '17 at 17:55

One simple way to post a form is to use a form backend service like https://www.formkeep.com

All you have to do is modify the action tag in your form and backend service will store the data and provide ways to integrate data with the other systems.

Here's a sample for how to modify the form html. You would just replace "example token" with a key provided by the formkeep service

<form accept-charset="UTF-8" action="https://formkeep.com/f/exampletoken" method="POST">

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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