PHP mailer not sending email on server - php

My PHPMailer script works fine on xampp local server.But on my website that is located on a apache server isn't working.I think in my apache server doesn't finds the right location of phpMailerAutoload.php but I'm not sure.
I'll be very thankfull if someone can help me with this .
EDIT:When I press the "send" button it displays the error that i set up to display in my PhpMailer script.(the last else)
My PHPMailer script:
<?php
session_start();
require_once 'phpMailer/PHPMailerAutoload.php';
$errors= [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
$fields=[
'name'=>$_POST['name'],
'email'=>$_POST['email'],
'message'=>$_POST['message']
];
foreach($fields as $field => $data){
if(empty($data)){
$errors[]='Il <b>' . $field . '</b> è necessario.';
}
}
if(isset($_POST['tel'])){
$sec_fields=[
'tel'=>$_POST['tel']
];
}
/*------Validation--------*/
if(!empty($_REQUEST['name'])){
$name = $_REQUEST['name'];
if(!preg_match("/^[a-zA-Z'-]+$/",$name)){
$errors[]='Il <b>nome</b> non è valido.';
}
}
if(!empty($_REQUEST['email'])){
$email = $_REQUEST['email'];
$valid = filter_var($email, FILTER_VALIDATE_EMAIL);
if($valid == false){
$errors[]='L\'indirizzo <b>email</b> non è valido.';
}
}
/*------Creating PHPMailer Class-------*/
if(empty($errors)) {
$m= new PHPMailer;
$m -> isSMTP();
$m -> SMTPAuth = true;
$m->Host = 'wolf.dnshigh.com';
$m->Username = 'email#danadesign.it';
$m->Password = 'password';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML();
$m->Subject = 'Avete ricevuto un nuovo messaggio .' ;
$m->Body =' Inviato da : ' . $fields['name'] . ' (' .$fields['email'] . ') <br><br> <p>' . $fields['message'] . '<br><br> </p> <p>Telefono: ' . $sec_fields['tel'] . '</p>' ;
$m->FromName = 'DanaDesign Mail';
$m->AddAddress ('MyinboxEmail#yahoo.it', 'My name');
/*-----Sending Email------*/
if ($m->send()) {
header('Location: redirect.html');
die();
} else {
$errors[]='Siamo spiacenti,non siamo riusciti a inviare il messaggio.Si prega di riprovare più tardi.';
}
}
} else {
$errors[]='Qualcosa è andato storto.';
}
header('Location: contact.php');
$_SESSION['errors']= $errors;
$_SESSION['fields']= $fields;
$_SESSION['sec_fields']= $sec_fields;
?>
And my contact form:
<section>
<h2>Write us a message</h2>
<div class="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul><li> <?php echo implode('</li><li> ',$errors); ?> </li></ul>
</div>
<?php endif; ?>
<form method="post" action="contact-script.php">
<label>
<img src="img/person.png" alt="Person Icon"> Nome*
<input type="text" name="name" autocomplete="off" <?php echo isset($fields['name']) ? 'value="' . e($fields['name']) . '"' : '' ?> />
</label>
<label>
<img src="img/telephone.png" alt="Telephone Icon"> Telefono
<input type="text" name="tel" autocomplete="off" <?php echo isset($sec_fields['tel']) ? 'value="' . e($sec_fields['tel']) . '"' : '' ?> />
</label>
<label>
<img src="img/mail.png" alt="Mail Icon">Indirizzo email*
<input type="text" name="email" autocomplete="off" <?php echo isset($fields['email']) ? 'value="' . e($fields['email']) . '"' : '' ?> />
</label>
<label>
<img src="img/message.png" alt="Message Icon">Messaggio*
<textarea name="message" cols="20" rows="2"> <?php echo isset($fields['message']) ? e($fields['message']) : '' ?> </textarea>
</label>
<input type="submit" value="Invia" />
</form>
</div>
</section>
Some text is in Italian language , I'm sorry about that.

Related

PHP Contact Form (PHPMailer)

Fist time using the GitHub PHPMailer and I have an issue with sending emails.
The data seems to be sent to the form and I am lost beyond that.
I know I am not using the errors in my page, which is why for now I am printing them to the php form.
html (index.php)
<form name="contactform" id="myform" class="fs-form fs-form-full" autocomplete="off" method="post" action="send.php">
<ol class="fs-fields">
<li>
<label class="fs-field-label fs-anim-upper" for="q1">What's your name?</label>
<input class="fs-anim-lower" id="q1" name="q1" type="text" placeholder="Johnny Bravo" required/>
</li>
<li>
<label class="fs-field-label fs-anim-upper" for="q2" data-info="We won't send you spam, we promise...">What's your email address?</label>
<input class="fs-anim-lower" id="q2" name="q2" type="email" placeholder="me#email.com" required/>
</li>
<li data-input-trigger>
<label class="fs-field-label fs-anim-upper" for="q3" data-info="This will help us know what kind of service you need">What can we do for you? <span style="font-size:20px;">(select one)</span></label>
<div class="fs-radio-group fs-radio-custom clearfix fs-anim-lower">
<span><input id="q3b" name="q3" type="radio" value="graphic"/><label for="q3b" class="radio-graphic">Graphic Design</label></span>
<span><input id="q3c" name="q3" type="radio" value="web"/><label for="q3c" class="radio-web">Web Design</label></span>
<span><input id="q3a" name="q3" type="radio" value="motion"/><label for="q3a" class="radio-motion">Motion Graphics</label></span>
<span><input id="q3d" name="q3" type="radio" value="other"/><label for="q3d" class="radio-other">Other/More</label></span>
</div>
</li>
<li>
<label class="fs-field-label fs-anim-upper" for="q4">Describe your project in detail.</label>
<textarea class="fs-anim-lower" id="q4" name="q4" placeholder="Describe here"></textarea>
</li>
<li>
<label class="fs-field-label fs-anim-upper" for="q5">What's your budget? (USD)</label>
<input class="fs-mark fs-anim-lower" id="q5" name="q5" type="number" placeholder="1000" step="100" min="100"/>
</li>
</ol><!-- /fs-fields -->
<button class="fs-submit" type="submit" value="Send">Send It</button>
</form><!-- /fs-form -->
PHP (send.php)
<?php
session_start();
require_once 'libs/phpmailer/PHPMailerAutoload.php';
$errors = [];
if(isset($_POST['q1'], $_POST['q2'], $_POST['message'])) {
$fields = [
'q1' => $_POST['q1'],
'q2' => $_POST['q2'],
'q3' => $_POST['q3'],
'q4' => $_POST['q4'],
'q5' => $_POST['q5']
];
foreach($fields as $field => $data) {
if(empty($data)) {
$errors[] = 'The ' . $field . ' field is required.';
}
}
if(empty($errors)) {
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = 'smtp.gmail.com';
$m->Username = 'myemail#gmail.com';
$m->Password = 'mypassword';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML();
$m->Subject = 'Contact form submitted';
$m->Body = 'From: ' . $fields['q1'] . ' (' . $fields['q2'] . ')<p>Budget</p><p>' . $fields['q5'] . '</p><p>Service</p><p>' . $fields['q3'] . '</p><p>Details</p><p>' . $fields['q4'] . '</p>';
$m->FromName = 'Contact';
$m->AddAddress('myemail#gmail.com', 'Ollie Taylor');
if($m->send()) {
header('Location: thanks.php');
die();
} else {
$errors[] = 'Sorry, could not send email. Try again later.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: index.php');
Thankyou!
You have some syntax errors for starters.
Line 30 of send.php
$m-Host = 'smtp outgoing here';
Should be
$m->Host = 'smtp outgoing here';
and line 39
$m->Body = 'From:: ' . $fields['q1'] . '(' $fields['q2'] ')' . $fields['q3'] . $fields['q4'] . $fields['q5'];
Should be
$m->Body = 'From:: ' . $fields['q1'] . '(' . $fields['q2'] . ')' . $fields['q3'] . $fields['q4'] . $fields['q5'];
(added concatenation inbetween your parenthesis)
Overall, I'd suggest you look into your PHP error log. I haven't tested it further to see if the email actually sends. But big picture here: Check PHP error log.

What language(s) i have to use to make a success or an error situation for PHP mail form?

I've got a PHP mail form which is already coded :
And I would like to appear a banner i coded using CSS and jQuery, if the form succeed to send a mail, or not :
I would like to know what language could I use to make it happens ? What are your advices ?
Here is my html code for the form :
<form id="my_form" enctype="multipart/form-data" method=POST action=formmail.php>
<input type="hidden" name="subject" value="formmail">
<!-- PSEUDO -->
<p>
<h3>
PSEUDO
<font class="color-red">
* :
</font>
<span class="padding1"><input type="text" class="form-control" name="pseudo" aria-required="true" aria-invalid="false"></span></p><br>
<!-- EMAIL -->
<p>
EMAIL
<font class="color-red">
* :
</font>
<span class="your-email padding1"><input type="email" class="form-control" name="email" aria-required="true" aria-invalid="false"></span></p><br>
<!-- MOTO -->
<p>
QUOTE
<font class="color-red">
* :
</font>
<span class="moto padding1"><input type="moto" class="form-control" name="moto" aria-invalid="false"></span> </p><br>
<!-- LINK OF THE GIF -->
<p>
LINK OF THE GIF
<font class="color-red">
* :
</font>
<span class="link-url padding1"><input type="link_url" class="form-control" name="link_url" aria-invalid="false"></span> </p><br>
<!-- RECAPTCHA -->
<div align="center">
<p><div class="g-recaptcha" data-sitekey="6LehFQoTAAAAAMHlOs6vgktrCdFo485AoHwmp9Cp"></div></p><br><h3>
<!-- SUBMIT -->
<p><input type="image" src="img/submitbutton.png" value="send"/><img class="ajax-loader" src="http://s584101063.onlinehome.fr/wp-content/plugins/contact-form-7/images/ajax-loader.gif" alt="Envoi en cours ..." style="visibility: hidden;"></p></div>
</div>
</form>
Here is my PHP :
if($_SERVER["REQUEST_METHOD"] === "POST")
{
//form submitted
//check if other form details are correct
//verify captcha
$recaptcha_secret = "MY_SECRET_RECAPTCHA";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true);
if($response["success"] === true)
{
Header("Location: MY_WEBSITE");
$TO = "MY_MAIL";
$subject = "Submission";
$h = "From: " . $_POST['email'];
$pseudo = $_POST['pseudo'];
$email = $_POST['email'];
$link_url = $_POST['link_url'];
$moto = $_POST['moto'];
$HTTP_POST_VARS = $_POST;
$message = "Pseudo : " . $pseudo . "\n\n " . "Email : " . $email . " \n\n" . "Text : " . $moto . "\n\n " . "URL du GIF : " . $link_url;
mail($TO, $subject, $message, $h);
}
else
{
$result
}
}
?>
VERY basic idea:
$test=mail($TO, $subject, $message, $h);
if($test){
echo 'EmAIL WORK';
}else{
echo 'EmAIL Fail';
}
EDIT : The Dagon's answer works very well, but my question was not clear, here is what I found to do what I wanted :
Thanks Dagon, but it was not really what I expected to do,
I found how to do it, it works :)
<?php
if(isset($_GET['id']) AND $_GET['id']==2) {
echo '<script type="text/javascript">';
echo '
$("#success-bar").css("display", "inline");
$("#success-bar").addClass("animated bounceInDown")
$("#success-bar").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",
function(){
$("#success-bar").removeClass("animated bounceInDown")
$( "#close2" ).click(function() {
$("#success-bar").addClass("animated bounceInUp")
$("#success-bar").hide()
$("#success-bar").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",
function(){
$("#success-bar").addClass("animated bounceInDown")
});
});
});';
echo '</script>';
}
elseif(isset($_GET['id']) AND $_GET['id']==3) {
echo 'FALSE';
}
?>

How to echo an error div if the reCaptcha isn't confirm

I have a script and it runs very well, but I want know how to echo an error div if recaptcha isn't confirmed. In this script if recaptcha wasn't confirmed the page will reload and nothing will be send to my mail, but I don't know how to display an error that tells the user: "You must verificate that you aren't a robot".
Can you help me?
CODE:
<?php
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../support/style/contactform.css\">\n";
$emailpattern="^[^# ]+#[^# ]+\.[^# \.]+$";
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<form method="post" id="contactformall">
<p>Nome</p>
<input type="text" name="name" class="contactformimput"/>
<p>Email (obbligatorio)</p>
<input type="text" name="email" class="contactformimput"/>
<p>Numero</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio (obbligatorio)</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="____key____"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>';
exit;
}
$responsejson=file_get_contents("google.com/recaptcha/api/…);
$response = json_decode($responsejson);
if($response->success==false) { echo "<div class=\"emailerror\" id=\"emailnoninviata\"><div><span>•</span> Email non inviata</div></div>";
**/////THIS part DON'T RUN**
}else
{
if (isset($_POST['send'])) {
if(!ereg($emailpattern,$_POST['email'])) {
$emailerror = true;
echo "<div class=\"emailerror\"><div><span>6</span> Email non valida</div></div>";
} if ($_POST['message'] == "") {
$emailerror = true;
echo "<div class=\"emailerror\"><div><span>6</span> Inserisci un messaggio</div></div>";
} elseif ($_POST['message'] != "" and ereg($emailpattern,$_POST['email'])){
$emailerror = false;
};
if ($emailerror == true) {
echo '<form method="post">
<p>Nome</p>
<input type="text" name="name" class="contactformimput"/>
<p>Email*</p>
<input type="text" name="email" class="contactformimput"/>
<p>Numero</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio*</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="___KEY___"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>';
}
if (isset($_POST['send']) and $emailerror == false) {
$to = "mail#gmail.com";
$subject = "B&B";
$user_name = 'Name: ' . $_POST['name'] . "\n";
$user_email = 'Email: ' . $_POST['email'] . "\n";
$user_ip = 'IP: ' . $_SERVER['REMOTE_ADDR'] . "\n";
$user_message = 'Message: ' . $_POST['message'];
$message = $user_name . $user_email . $user_ip . $user_message;
$success = mail($to, $subject, $message);
echo "<div class=\"emailerror\" id=\"emailinviata\"><div> <span>5</span> Email inviata correttamente</div></div>";}
}
}
?>
You are mixing javascript code with php code. Javascripts uses '.' to access class vars/functions, php uses '->'.
Please read this page in order to use Recaptcha with php: https://developers.google.com/recaptcha/old/docs/php
The recaptcha documentation shows that this API request should return JSON. You will need to decode the JSON response to refer to its contents. So do this after you use file_get_contents() to get the response:
$response = json_decode($response);
and then you should be able to check for success with a little adjustment to your syntax.
if($response->success==false)

alternative to closing if statement in php also trouble with arrays

Thank you in advance for looking at my code and helping me with the problem i am having. I am trying to build a simple contact form. The thing is when i hit my submit button everything that gets directed to the next page displays everything into an array. How can i go about fixing this?
Also, my if statement is giving me a hard time how can i go about fixing this? Am i using the wrong type of arrays?
contact.php
<?php
session_start();
require_once 'PHPMailerAutoload.php';
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'message' => $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}
if(empty($errors)){
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = 'smtp.gmail.com';
$m->Username = 'none#gmail.com';
$m->Host = 'Pa$$w0rd1';
$m->SMTPSecure = 'ssl';
$m->Port = '465';
$m->isHTML();
$m->Subject = 'Contact form submitted';
$m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';
$m->From = 'Contact';
$m->AddAddress('none#gmail.com', 'name');
if($m-> send()){
header('Location: thankyou.php');
die();
} else {
$errors[] = 'Sorry, could not send email. Try again late.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: contactform.php');
?>
contactform.php
<?php
session_start();
require_once 'security.php';
$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Contact Us</title>
<link rel="stylesheet" href="css/website.css">
</head>
<center><body>
<div class="contact">
<?php if(!empty($errors)): ?>
<div class="panel">
<ul><li><?php echo implode(('</li><li>'), $errors); ?></li></ul>
</div>
<?php endif; ?>
<form action="contact.php" method="post">
<label>
Your name *
<input type="text" name="name" autocomplete="off">
</label>
<br><label>
Your email address *
<input type="text" name="email" autocomplete="off">
</label>
<br><label>
Your message or concern *
<textarea name="message" rows="8"></textarea>
</label>
<input type="submit" value="Send">
<p class="muted">* means a required field</p>
</form>
</div>
</body></center>
</html>
<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>
This is the if statement i am referring to in the above code
<?php if(!empty($errors)); ?>
<div class="panel">
<ul><li><?php echo implode(('</li><li>'), $errors); ?></li></ul>
</div>
<?php endif ?>
Please Please Please help me with this.
Problem is semicolon at end of your if statement.
If you want to use if with endif you need to use ":" like this:
if(!empty($errors)):
code;
endif;
Try this:
<?php if(!empty($errors)): ?>
<div class="panel">
<ul><li><?php echo implode(('</li><li>'), $errors); ?></li></ul>
</div>
<?php endif; ?>
The proper way to use if ... endif is:
<?php if (condition): ?>
// Things to do when condition is true
<?php endif; ?>

Wordpress Custom Contact Form Not Working

I am trying to create a custom contact form page for my Wordpress theme. However on my way to finishing my codes the error message on my page won't show up.
Also, I am running on a localhost computer for testing this custom page and it seems it won't send my message to the ADMIN EMAIL that was set on my Wordpress Installation.
Here's the snippet on my WP Custom Contact Form:
<?php
/* Template Name: Contact Page */
?>
<?php
// Function for email address validation
function isEmail($verify_email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$verify_email));
}
$error_name = false;
$error_email = false;
$error_subject = false;
$error_message = false;
if (isset($_POST['submit'])) {
// Initialize the variables
$contact_name = '';
$contact_email = '';
$contact_subject = '';
$contact_message = '';
// Get the name
if (trim($_POST['contact_name']) === '') {
$error_name = true;
} else {
$name = trim($_POST['contact_name']);
}
// Get the email
if (trim($_POST['contact_email']) === '' || !isEmail($_POST['contact_email'])) {
$error_email = true;
} else {
$email = trim($_POST['contact_email']);
}
// Check if we have errors
if (!$error_name && !$error_email && !$error_subject && !$error_message) {
// Get the receiver email from the WP admin panel
$receiver_email = get_option('admin_email');
$subject = "Message from $contact_name";
$body = "You have a new quote request from $contact_name. Project details:" . PHP_EOL . PHP_EOL;
$body .= PHP_EOL . PHP_EOL;
$headers = "From: $contact_email" . PHP_EOL;
$headers .= "Reply-To: $contact_email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
// If all is good, we send the email
if (mail($receiver_email, $subject, $body, $headers)) {
$email_sent = true;
} else {
$email_sent_error = true;
}
}
}
?>
<?php get_header(); ?>
<!-- BLOG AREA -->
<section>
<hr class="no-margin" />
<div class="blog-container section-content">
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="box-layer custom-padding">
<div class="align-center">
<h2>We want to hear from you!</h2>
<p>If you are seeking to contact us, please fill up the form below. If you want to advertise or be partner with us just inform us on the message box below. </p>
<p>Thank you so much for your support!
<br/>We really appreciate!</p>
<?php if (isset($email_sent) && $email_sent == true) : ?>
<h2>Success!</h2>
<p>You have successfully sent the quote request. I'll get back to you as soon as possible.</p>
<?php elseif (isset($email_sent_error) && $email_sent_error == true) : ?>
<h2>There was an error!</h2>
<p>Unfortunately, there was an error while trying to send the email. Please try again.</p>
<?php else : ?>
<form action="<?php the_permalink(); ?>" method="POST" class="general-form" novalidate>
<p <?php if ($error_name) echo 'class="error"'; ?>><input name="contact_name" id="contact_name" class="form-control" placeholder="Your Name.." type="text" value="<?php if (isset($_POST['contact_name'])) echo $_POST['contact_name']; ?>" /></p>
<p <?php if ($error_email) echo 'class="error"'; ?>><input name="contact_email" id="contact_email" class="form-control" placeholder="Your Email.." type="email" value="<?php if (isset($_POST['contact_email'])) echo $_POST['contact_email']; ?>" /></p>
<p <?php if ($error_subject) echo 'class="error"'; ?>><input name="contact_subject" id="contact_subject" class="form-control" placeholder="Your Subject.." type="text" value="<?php if (isset($_POST['contact_subject'])) echo $_POST['contact_subject']; ?>"/></p>
<p <?php if ($error_message) echo 'class="error"'; ?>><textarea name="contact_message" id="contact_message" class="form-control" placeholder="Write your comment here.." rows="4" cols="100"><?php if (isset($_POST['contact_message'])) echo $_POST['contact_message']; ?></textarea></p>
<input class="btn btn-primary no-border" name="submit" type="submit" id="submit" value="Send Message">
</form>
<?php endif; ?>
</div>
</div>
<!-- RELATED ARTICLE AREA -->
</div>
<aside>
<!-- SIDEBAR AREA -->
<div class="col-md-3 col-md-offset-1 margin-sidebar">
<?php get_sidebar(); ?>
</div>
</aside>
</section>
<?php get_footer(); ?>
Any idea? or Is there a way you can correct my code if you see some errors?
To get you started
Make sure WP_DEBUG is set to true on your wp-config.php file.
You can get the last errors from mail() (answered here)
Use wordpress' functions if available, in this case wp_mail is at your disposal.
There are wordpress plugins such as gravity forms or contact form 7 which can accomplish the task.
Use filters and actions.
Had to post as an answer, couldn't comment yet.
HTH
If you would like to get the admin email address, you have to do it with:
$bloginfo = get_bloginfo( 'admin_email' );
get_option('admin_email') is only works when you save anything to that in the Theme Setup, probably it is empty now.
By the way, you can find many of professional, ready to use solution, like WordPress Contact Form Slider
<?php
/*
Plugin Name:Contact Form Plugin
Plugin URI: http://example.com
Description: Simple non-bloated WordPress Contact Form
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
*/
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
include( MY_PLUGIN_PATH . 'form/form.php');
include( MY_PLUGIN_PATH . 'admin/admin-menu.php');
include( MY_PLUGIN_PATH . 'database/table.php');
?>
Form.php
====================
<?php
function form()
{
echo '<form action="'.esc_url($_SERVER['REQUEST_URI']).'" method="post">';
echo '<p>';
echo 'Your First Name:';
echo '<br/>';
echo '<input type="text" name="fname" required/>';
echo '<br/>';
echo 'your Email-Id:';
echo '<br/>';
echo '<input type="email" name="email" required/>';
echo '<br/>';
echo 'Your Subject:';
echo '<br/>';
echo '<input type="text" name="subject" required/>';
echo '<br/>';
echo 'Your Message:';
echo '<br/>';
echo '<textarea name="message" cols="30" rows="4" required></textarea>';
echo '<br/>';
echo '<input type="submit" name="submit" value="Submit"/>';
echo '</form>';
}
if(isset($_REQUEST['submit']))
{
$name = $_POST['fname'];
$email = $_POST['email'];
$subject=$_POST['subject'];
$msg=$_POST['message'];
function insertuser($name,$email,$msg,$subject){
global $wpdb;
$table_name = $wpdb->prefix . "contactinfo";
$wpdb->insert($table_name, array('name' => $name, 'email' => $email , 'message'=> $msg , 'subject' => $subject) );
}
insertuser($name,$email,$msg,$subject);
}
function shortcode()
{
form();
}
add_shortcode('contact-form','shortcode');
?>
table.php
===================
<?php
global $wpdb;
$table_name = $wpdb->prefix . "contactinfo";
$charset_collate = $wpdb->get_charset_collate();
if( $wpdb->get_var ('SHOW TABLE LINK' .$table_name) != $table_name )
{
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
email varchar(40) NOT NULL,
message text NOT NULL,
subject text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
?>

Categories