How to get mail of contact form in wordpress
In WordPress, you can get the mail of a contact form using the wp_mail
function or a plugin like Contact Form 7 or Gravity Forms. Here are the steps:
Method 1: Using wp_mail
function
-
Create a new PHP file in your WordPress theme directory (e.g.,
contact-form-handler.php
). -
Add the following code to the file:
<?php function send_contact_form_email() { // Get the form data $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; // Set the email subject and body $subject = 'Contact Form Submission'; $body = "Name: $name\nEmail: $email\nMessage: $message"; // Send the email using wp_mail wp_mail('[email protected]', $subject, $body); // You can also use a custom email template // $template = get_template_part('contact-form-email-template'); // wp_mail('[email protected]', $subject, $template); }
// Add the function to the WordPress hook add_action('wp_ajax_nopriv_send_contact_form_email', 'send_contact_form_email'); add_action('wp_ajax_send_contact_form_email', 'send_contact_form_email');
3. In your contact form, add a form action attribute that points to the PHP file:
```html
<form action="<?php echo admin_url('admin-ajax.php');?>" method="post">
<!-- Your form fields here -->
<input type="submit" value="Send">
</form>
Method 2: Using Contact Form 7 plugin
-
Install and activate the Contact Form 7 plugin.
-
Create a new contact form using the plugin's form builder.
-
In the form settings, go to the "Mail" tab and set the email address and subject.
-
In your theme's functions.php file, add the following code:
add_action('wpcf7_mail_sent', 'send_contact_form_email'); function send_contact_form_email($contact_form) { // Get the form data $name = $_POST['your-name']; $email = $_POST['your-email']; $message = $_POST['your-message']; // Set the email subject and body $subject = 'Contact Form Submission'; $body = "Name: $name\nEmail: $email\nMessage: $message"; // Send the email using wp_mail wp_mail('[email protected]', $subject, $body); }
Method 3: Using Gravity Forms plugin
-
Install and activate the Gravity Forms plugin.
-
Create a new form using the plugin's form builder.
-
In the form settings, go to the "Notifications" tab and set up an email notification.
-
In your theme's functions.php file, add the following code:
add_action('gform_after_submission', 'send_contact_form_email'); function send_contact_form_email($entry) { // Get the form data $name = $entry['1']; // Replace with the field ID $email = $entry['2']; // Replace with the field ID $message = $entry['3']; // Replace with the field ID // Set the email subject and body $subject = 'Contact Form Submission'; $body = "Name: $name\nEmail: $email\nMessage: $message"; // Send the email using wp_mail wp_mail('[email protected]', $subject, $body); }
Remember to replace
[email protected]
with your actual email address.