Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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);   
}
share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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