John Davidson

php - Sending an email with form variables to an email using wp_mail()

0 comments
Message:


I have created a WP template and added to it a form.
Below the form I have added the wp_mail() function to send the data the user has input to their email.


Now, I'm getting notices that:


Notice: Undefined index: name in mypath.


Any help how to resolve this would be most welcome.


Edit 1: to get the notice I had to remove the if statement.


Edit 2: this is literally all the code I have concerning the form. Am I missing something? Do I need to add add_action() somewhere or something like that?


The code:



<form id="form" method="post" action="">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
</br>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname">
</br>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</br>
<label for="tel">Phone number:</label>
<input type="tel" id="tel" name="tel">
</br>
<input type="submit" value="Submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$tel = $_POST['tel'];

$to = $_POST[$email]; //[email protected]
$subject = 'Reservation';
$body = 'Name: ' . $name . '\r\n' .
'Surname: ' . $surname . '\r\n' .
'Email ' . $email . '\r\n' .
'Phone number: ' . $tel . '\r\n';

wp_mail( $to, $subject, $body );
echo "Sent!";
}
?>




The anwser


Edit 3: just to make it clear the solution was $to = $_POST['email']; //[email protected] so ['email'] not [$email].



My Answer:
To resolve the "Undefined index" notice, you need to check if the form variables are set before trying to access them. Here's an example of how you can modify your code to avoid the notice:

php
if (isset($_POST['submit'])) {
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';

// Use wp_mail() function to send the email
$to = 'recipient@example.com';
$subject = 'Message from ' . $name;
$body = 'Name: ' . $name . "\n";
$body .= 'Email: ' . $email . "\n";
$body .= 'Message: ' . $message;

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

// Redirect or display a success message
}


In this code snippet, we first check if the form was submitted by checking if the 'submit' variable is set. Then, we use the isset() function to check if the form variables ('name', 'email', 'message') are set before accessing them. This way, you won't get the "Undefined index" notice.

Additionally, make sure to sanitize and validate the user input before using it in the email to prevent any security vulnerabilities. You can use functions like sanitize_text_field() or sanitize_email() to sanitize the input.

Rate this post

4 of 5 based on 7933 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us