0

I am trying to make a form for every post/thread on my site. It finds the mail from behind every post (every post got an unique email) but i doesn't send it foward. My form is stuck somewhere.

<?php 

global $wpdb;
global $post;
global $current_user;
global $wp_query;

$author_ID = get_query_var('post_author');
$to=the_author_meta('user_email',$author_ID);

//searching and showing up the email on the screen. working fine until here.

if(isset($_POST['submit'])){

$from = get_option( 'admin_email' ); //getting admin email

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$subject = "Form submission";

$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];

$headers = "From:" . $from;

// mail($to,$subject,$message,$headers);

if (mail($to,$subject,$message,$headers)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }  

//email is not send. "something went wrong" popping up.

}

?>

Every post/thread has a email behind, calling it, it shows up well. But the email is not send to the addres.

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 
  • You know that you are sending same email 2 times , 2nd one is being sent from if condition , and as you are using wordpress, you can use `wp_mail();` at the place of `mail();` – Arsh Singh Mar 26 '17 at 17:03
  • @ArshSingh well...that's my last problem. It doesn't send in first place. – Christian Tampa Mar 26 '17 at 17:04

1 Answers1

0

First of all, you call mail() twice. It will get execucted and in the if-clause it will be executed the second time.

Second point, have you debugged, what you $to=the_author_meta('user_email',$author_ID); returns for $to?

Another thing, I personally use a PHP library for sending mails, because the mail() function is really hard to make it right in all circumstances, one mistake and everything is screwed up. I would recommend you PHPMailer.

Edit: Possible thing which can go wrong with the plain mail() function:

  • Wrong Charset
  • Encoding (you have not encoded your message)
  • mail is actually not active on your system (e.g. enable it in PHP, for Windows you have to configure an alternative, etc.)
Fabian N.
  • 1,221
  • 10
  • 18
  • I saw that i call it twice and i repaired it. It return the email behind the post. Already check that. – Christian Tampa Mar 26 '17 at 17:09
  • if i put it manually $to='example@yahoo.com'; it works very well. I already echo "$to" and it seems to take the right email. So my problem is why is not sending it. – Christian Tampa Mar 26 '17 at 17:12
  • For debugging, I would suggest to use `var_dump()` instead of `echo`. Also look at the generated source code, maybe there are some weird tags around the email, which the browser will not show, but the mail will reject. – Fabian N. Mar 26 '17 at 17:20