As the title implies, I have a simple form in php that keeps giving me an undefined error for the object $error. I have tried declaring it globally and setting it to null but all that does is prevent any error logs. Code below, thanks!
<?php
$siteOwnersEmail = 'myemail#yahoo.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
Declare the error variable as an array. Add these new lines to your code to fix
if($_POST) {
$error = array();#declare error variable
if (!isset($error)) {#check if we have an error message of some kind
#proceed
Create $error as blank array.Use below code.
<?php
$siteOwnersEmail = 'myemail#yahoo.com';
$error=array();
if(!empty($_POST)) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
Related
i'm using a normal php script for recive mail from my website directly to my personal mail. When i try to test and send from contact form a mail to me, output always said: "Something went wrong, please try again" like if there's no validation. It's my first time that i try to incorporate a php script so probably i'm missing something very oblivious.
Here's the code:
<?php
$siteOwnersEmail = 'Myemail#gmail.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
PHP mail function send email without SMTP authentication. It would be more accurate to send e-mail with SMTP authentication. I can suggest you to check the link below.
https://github.com/PHPMailer/PHPMailer
I deployed my website and I'm trying to send an email from my website that is hosted by Byethost and it displays a success message but for some reason the email is not being received. What might be the problem?
Below you will find the PHP script.
<?php
// Replace this with your own email address
$siteOwnersEmail = 'shouman882#gmail.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
mail() function is not avilable in byethost free hosting you need to upgrade to premium.
Notice: Undefined variable: error in
/storage/ssd5/047/2981047/public_html/inc/sendEmail.php on line 47
OK
this error is being shown when I send an email from my own site...
I want to find a solution to this since the error is being shown on the site itself, not in the console, etc...
The message up is what is being shown on my site when the form is filled correctly...
Please, not I have no experience in PHP.
<?php
$siteOwnersEmail = '*********************';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message = "";
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//Error Starts is her if im not mistaken....
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
When you call if (!$error) { the variable $error might not have been created - it only comes into existence when either the length test or regex test fail.
As the $error variable seems to be an array when it is created you ought to create a default empty variable. You ought to also check for the other POST variables being available before trying to use them or similar errors will occur potentially.
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['contactName'],$_POST['contactEmail'],$_POST['contactSubject'],$_POST['contactMessage'] ) ){
$error=[]; # default, empty error variable
if (strlen($name) < 2) $error['name'] = "Please enter your name.";
/* etc */
if ( empty( $error ) ) {
/* try to send email */
} else {
/* errors happened - let user know or perform other tasks */
}
}
You have to assign default values to variable $error, for example
$error = '';
My PHP code for sending email its working good, but there is a problem that doesn't show who is the sender just show noreply#example.com. I hosted my website at ecowebhosting.
<?php
$error = "";
$successMessage = "";
if ($_POST) {
if (!$_POST["email"]) {
$error .= "An email address is required.<br>";
}
if (!$_POST["message"]) {
$error .= "The message field is required.<br>";
}
if (!$_POST["subject"]) {
$error .= "The subject is required.<br>";
}
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email address is invalid.<br>";
}
if ($error != "") {
$error = '<div><p>There were error(s) in your form:</p>' . $error . '</div>';
} else {
$to = "example#gmail.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From: ".$_POST['email'] . "\r\n";
$headers = "Reply-To: ".$_POST['email'] . "\r\n";
$headers = "Subject: ".$_POST['subject'] . "\r\n";
$headers = "Message : ".$_POST['message'] . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo $successMessage = '<div>Your message was sent, we\'ll get back to you ASAP!</div>';
} else {
echo $error = '<div><p><strong>Your message couldn\'t be sent - please try again later</div>';
}
}
}
?>
I am noob with php so pardon my ignorance. I have created php form and it is working fine except one thing. When i get my mail it says that its send from nobody , i have no idea how can i solve this issue so i am asking for little help! Thank you!!! Here's my code:
<?php
if (isset($_POST['Submit'])) {
if (!empty($_POST['name'])) {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Molimo unesite Vaše ispravno ime.';
}
} else {
$errors .= '<p>Molimo unesite Vaše ime.</p>';
}
if (!empty($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NIJE</strong> valjana email adresa.<br/><br/>";
}
} else {
$errors .= '<p>Molimo unesite email adresu.</p>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
}
if (!$errors) {
$mail_to = 'dejo.dekic#yahoo.com';
$subject = 'Kontakt';
$tema = 'Info';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Od: ' . $_POST['name'] . "\n";
$headers .= 'Email: ' . $_POST['email'] . "\n";
$headers .= "Poruka:\n" . $_POST['message'] . "\n\n";
$user = $_POST['email'];
$poruka ='Vaš kontakt je uspjesno zaprimljen! Odgovorit ću vam u najkraćem mogućem roku. Hvala! Molimo ne odgovarajte na ovu poruku. Ova poruka je automatska.';
mail($mail_to, $subject, $headers, "Content-Type: text/plain; charset=UTF-8;");
mail($user, $tema, $poruka, "Content-Type: text/plain; charset=UTF-8;");
echo "<div style='color:white;margin:0px auto;padding-top:20px;width:290px;background-color:white;font-weight:bold;text-align:center;'><p>Hvala Vam na kontaktu!</p></div>";
} else {
echo '<div class="errors">' . $errors . '<br/></div>';
}
}
?>
And this is how it looks in my yahoo mail:http://www.homepagepays.robertpeic.com/yahoo.png
$from = 'yourdesiredemail#here.com';
$headers .= "From: " . $from . "\r\n";
Just need an addition to the header.
After little struggle i have managed to solve this with your help:) So i am posting a solution if someone had the same problem. Thx! Notice: $add_headers and inside mail function: -fmail#robertpeic.com Here my full code:
if (isset($_POST['Submit'])) {
if (!empty($_POST['name'])) {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Molimo unesite Vaše ispravno ime.';
}
} else {
$errors .= '<p>Molimo unesite Vaše ime.</p>';
}
if (!empty($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NIJE</strong> valjana email adresa.<br/><br/>";
}
} else {
$errors .= '<p>Molimo unesite email adresu.</p>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
}
if (!$errors) {
$mail_to = 'dejo.dekic#yahoo.com';
$subject = 'Kontakt';
$tema = 'Info';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8" . "\r\n";
$headers .= 'Od: ' . $_POST['name'] . "\n";
$headers .= 'Email: ' . $_POST['email'] . "\n";
$headers .= "Poruka:\n" . $_POST['message'] . "\n\n";
$add_headers = 'From: mail#robertpeic.com' . "\r\n". "Return-path: mail#robertpeic.com" . "\r\n";
$user = $_POST['email'];
$poruka ='Vaš kontakt je uspjesno zaprimljen! Odgovorit ću vam u najkraćem mogućem roku. Hvala! Molimo ne odgovarajte na ovu poruku. Ova poruka je automatska.';
mail($mail_to, $subject, $headers, $add_headers, "-fmail#robertpeic.com");
mail($user, $tema, $poruka, $add_headers, "-fmail#robertpeic.com");
echo "<div style='color:white;margin:0px auto;padding-top:20px;width:290px;background-color:white;font-weight:bold;text-align:center;'><p>Hvala Vam na kontaktu!</p></div>";
} else {
echo '<div class="errors">' . $errors . '<br/></div>';
}
}
?>
This is how it look's NOW in my yahoo mail: http://www.homepagepays.robertpeic.com/yahoo2.png