I’m using the wp_mail function to send emails in WordPress. Before I run the function, I retrieve a heap of data from a session and some others from a form that was just submitted. For example:
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$phone_number = $_POST['phone_number'];
$message = $first_name . $last_name . $phone_number;
wp_mail($myemail,"Email sent",$message);
I’m using the plugin called WP-MAIL SMTP to send via Gmail’s SMTP servers. When I disable this, no emails are sent at all.
For some reason, I receive two emails. One has the POST AND SESSION information and the other only has the SESSION information.
Any help would be greatly appreciated! Thanks in advance,
Jamie
I’m using WordPress 3.3
It sounds like you don’t have your code wrapped into an
if
statement that’ll check it only gets executed if the form was actually submitted, i.e. it gets run when you first view the page (GET
) and again when you submit the form (POST
). You can use the below snippet to ensure that the e-mail is only sent when the form has been submitted: