0
votes

I have a website I'm working on with a contact form. Simple Name, Email, and Message fields. However, I'm not receiving the email from the form; though I do get the "Message Sent" success option.

I had found a way to test wp_mail to see if that was it, using https://gist.github.com/butlerblog/5c9b805529c419b81447#file-test_wp_mail-php and that works just fine - so I have no clue what the problem is.

<?php

//loading wordpress functions
    require( '../../../wp-load.php' );


define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');


 $to = get_option('admin_email');                  //Enter your e-mail here.
 $subject =  get_theme_option(tk_theme_name.'_contact_contact_subject');

 $from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');

 $body = "You received e-mail from ".$from."  [".$name."] "." using ".$sitename."\n\n\n".$message;

 $send = wp_mail($to, $subject, $body, $headers) ;


if($send){
wp_redirect($return.'?sent=success');
}else{
    wp_redirect($return.'?sent=error');

    }

 ?> 

As I said, it does hit

wp_redirect($return.'?sent=success')

so I have no clue what is wrong.

EDIT:

Did a variable dump

To: [email protected]
Subject: E-Mail from HollyBrown.Net
From: TestName
Name: [email protected]
Message: TestMessage
Headers: From: [email protected] Reply-To: E-Mail from HollyBrown.Net
Return: http://www.hollybrown.net/contact/
Sitename: The Artwork of Holly Brown
Body: You received e-mail from TestName [[email protected]] using The Artwork of Holly Brown TestMessage
Send: 1

Warning: Cannot modify header information - headers already sent by (output started at /home/panoramicpanda/hollybrown.net/wp-content/themes/widely/sendcontact.php:21) in /home/panoramicpanda/hollybrown.net/wp-includes/pluggable.php on line 1178
1
Try to log/ echo /vardump your values, then body, see what params are wrong. Maybe POST should be GET? Just a guess. Also your $send may be always true. - ares777
Added a variable dump - PanoramicPanda
What you got? Sorry I'm on mobile now and don't see anything? Post your vardump - ares777
It's added into the post; doing it in a comment seems to remove all formatting. - PanoramicPanda

1 Answers

0
votes

The header already sent error leads me to believe output has already been started before you try and send the headers. In this case, the redirect won't work. Try this instead:

    /loading wordpress functions
    require( '../../../wp-load.php' );

ob_start(); // Starts data buffer.
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');


 $to = get_option('admin_email');                  //Enter your e-mail here.
 $subject =  get_theme_option(tk_theme_name.'_contact_contact_subject');

 $from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');

 $body = "You received e-mail from ".$from."  [".$name."] "." using ".$sitename."\n\n\n".$message;

 $send = wp_mail($to, $subject, $body, $headers) ;
 ob_end_clean();// Purges data in buffer. 

if($send){
wp_redirect($return.'?sent=success');
}else{
    wp_redirect($return.'?sent=error');

    }

Notice the ob_start and ob_end_clean. This will throw your output into the "buffer" and output it clean. The reason this is a problem, is because if any markup is sent BEFORE your sendmail (ie: html generated through templates etc, then the headers have already been started. At that point, unless your server configuration specifically allows you to modify already sent headers, then you will get the error you are getting. Using output buffering allows you to send the data as you intend.