I want to mention first a very similar question I already found here, but the answer proposed there I did , but still its not working so am posting my problem here.
<?php
/**
* Plugin Name: GCI Contact Form
* Plugin URI: http://www.example.com
* Description: A Simple Plugin To Implement Contact Form into your WordPress Site
* Version: 0.0.1
* Author: Parnasree Chowdhury
* Author URI: http://www.example.com
* License: GPL2
*/
function html_form_code() {
echo '<form method="post" action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '">';
echo '<fieldset>';
echo '<input type="text" name="cf-name" value="' .esc_attr( isset( $_POST["cf-name"] ) ? $_POST["cf-name"] : '' ) . '" placeholder="Name*" class="form-control form-dark" required/>';
echo '<input type="email" name="cf-email" value="' . esc_attr( isset( $_POST["cf-email"] ) ? $_POST["cf-email"] : '' ) . '" placeholder="Email*" class="form-control form-dark" required/>';
echo '<textarea rows="6" name="cf-message" placeholder="Message*" class="form-control form-dark" required>' . esc_attr( isset( $_POST["cf-message"] ) ? $_POST["cf-message"] : '' ) . '</textarea>';
echo '<input name="cf-submitted" class="btn btn-border border-white" type="submit" value="Submit" />';
//echo '<input type="hidden" name="action" value="gci_deliver_mail" />';
echo '<div class="loading"></div>';
echo '</fieldset>';
echo '</form>';
}
function gci_deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field($_POST["cf-name"]);
$email = sanitize_email($_POST["cf-email"]) ;
$subject = "Enquiry message submitted from iceindia";
$message = esc_textarea($_POST["cf-message"]);
// get the blog administrator's email address
$to = 'mymailid@gmail.com';
$headers = "From: $name <$email>" . "
";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p>Thanks for contacting me, expect a response soon.</p>';
echo '</div>';
} else {
echo 'An unexpected error occurred';
}
}
}
function cf_shortcode() {
ob_start();
gci_deliver_mail();
html_form_code();
return ob_get_clean();
}
add_shortcode( 'gci_contact_form', 'cf_shortcode' );
?>
The above is my contact-form plugin code. I used the short code in my theme's front page like following : -->
<div class="contact-form">
<?php echo do_shortcode('[gci_contact_form]');?>
</div>
It worked perfectly and the contact form is getting appeared in my theme's front page as it should be and even the form is getting submitted , but mail is not sent it's triggering the error i.e. echoing the error in the else part of the gci_deliver_mail() function.
Here is a screenshot where the error is occuring --> http://tinyurl.com/omcp7zz Thanks in advance
I added the following line in else part :->
var_dump($to, $subject, $message, $headers);
Now the Output:
string(23) "mymaildid@gmail.com" string(39) "Enquiry message submitted from iceindia" string(4) "test" string(27) "From: par " An unexpected error occurred
change this line
$headers = "From: $name <$email>" . "
";
to
$headers = "From: " . $name . " <" . $email. ">" . "
";
maybe you have any unescaped data in $name
As you have mention your output.I think your header had an error Just remove or comment the header.Then it will sent mail to your mail.
//$headers = "From: $name " . " "; Thank you.
First check if your mail server is working properly or not. You can install Easy SMTP mail plugin Download From Here. Try to send the test mail.
Use only the blog admin mail id which is registered while creating the blog in wordpress. example: *
**if your blog name is example.com then use your mail id as admin@example.com*
**