1

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

2

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:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $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);   
}
1
  • 1
    Thanks for the quick reply. That was such a simple fix! Thanks for your help, it's working now.
    – Jamie
    Jan 7 '12 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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