This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 6 years ago.
I am getting issues on the 4th line. Everyone time someone submits and email is a vaild entry I get this error
Fatal error: Uncaught Error: Call to undefined function eregi() in /homepages/29/d409157025/htdocs/contacts.php:31 Stack trace: #0 {main} thrown in /homepages/29/d409157025/htdocs/contacts.php on line 4
The form was working perfectly since I uploaded it, thats been over a year.
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'info#roswellhistoriccottage.com, artem.alek#icloud.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
$headers = 'From: RHC Website Information Request <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
The eregi() function is deprecated and was removed in PHP 7. It appears you or your host recently updated you to PHP 7.
To fix the issue, you need to change the code to use preg_match() instead.
Related
Sorry for the stupid question (I'm just starting out) but I can't quite understand why the phone field of my PHP script won't appear in the email I receive. I've added the field in HTML in works fine, the error message if the phone field is empty works good but if I run it, the email I receive will not contain the field.
Here is the PHP mail script:
<?php
require('email_config.php');
// sender information
$name = trim($_POST['name']);
$phone = trim($_POST['phone']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$error = "";
// check sender information
$pattern = "^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$^";
if(!preg_match_all($pattern, $email, $out)) {
$error = $invalid_email; // for invalid email
}
if(!$email) {
$error = $invalid_email; // for empty email field
}
if (!$phone) {
$error = $invalid_phone; // for empty phone field
}
if(!$message) {
$error = $invalid_message; // for empty message field
}
if (!$name) {
$error = $invalid_name; // for empty name field
}
// email header
$headers = "From: ".$name." <".$email.">\r\nReply-To: ".$email."";
if (!$error){
// sending email
$sent = mail($to_email,$subject,$message,$headers);
if ($sent) {
// if message sent successfully
echo "SEND";
} else {
// error message
echo $sending_error;
}
} else {
echo $error; // error message
}
?>
Thanks in advance!
Looks like you skipped adding the phone to the message, Your message
should be something like this
$message = trim($_POST['message']).' \n Phone: '.trim($_POST['phone']);
Seems like you are sending with plan text email so i used the new line special character /n.
To learn more about the special string characters
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
I have a simple form with a security question to prevent spamming.
link to my webpage with the form in question http://ddry.co.uk/contact_us.html.
I want to be able to output an html page if a user inputs an incorrect answer rather than just plain text.
I have a redirect to another html file if the form is successful at the bottom of my php script. looking at other forums someone suggested using readfile("contact-failed.html#fail"); to display the html; However I'm not entirely sure where to put the code for the redirect of an incorrect answer. I'm new to PHP, so if someone is able to explain what I'm doing wrong that would great. Or alternately if somone has a better spam filter code that would be great also Thanks in advance.
html code of anti spam
php file for post.
----- UPDATE --------
I think what i'm after is an if, else statement?
after researching I have altered my code to include an else statement; However due to my lack of PHP knowledge I'm still getting a blank screen instead of my error redirect html page, which is shown at the bottom of my php code.
Question: how can I properly configure the if, else statement so if the anti-spam result is wrong (doesn't equal to 12) then proceed to contact-failed.html?
Thanks in advance
<?php
// Email address verification
function isEmail($clientEmail) {
return filter_var($clientEmail, FILTER_VALIDATE_EMAIL);}
if($_POST) {
// Enter the email where you want to receive the message
$myemail = 'info#ddry.co.uk';
$name = addslashes(trim($_POST['name']));
$clientEmail = addslashes(trim($_POST['email']));
$subject = addslashes(trim($_POST['phone']));
$phone = addslashes(trim($_POST['phone']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$array = array('nameMessage' => '','emailMessage' => '', 'phoneMessage' => '', 'messageMessage' => '', 'antispamMessage' => '');
if(!isEmail($clientEmail)) {
$array['nameMessage'] = 'Empty name';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Invalid email!';
}
if($phone == '') {
$array['phoneMessage'] = 'Empty phone number!';
}
if($message == '') {
$array['messageMessage'] = 'Empty message!';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Incorrect Answer!';
}
if(isEmail($clientEmail) && $clientEmail != '' && $message != '' && $antispam == '12') {
// Send email
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $clientEmail\n Message: \n $message\n Phone: $phone";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $clientEmail";
mail($to,$email_subject,$email_body,$headers);
echo json_encode($array);
header('Location: contact-success.html#success');
}
else (isEmail($clientEmail) && $clientEmail != '' && $message != '' && $antispam !== '12'){
echo('Location: contact-failed.html#fail');}
?>
Why not try something simple like this ?
function isEmail($clientEmail) {
return filter_var($clientEmail, FILTER_VALIDATE_EMAIL);
}
if($_POST){
// Enter the email where you want to receive the message
$myemail = 'info#ddry.co.uk';
$name = addslashes(trim($_POST['name']));
$clientEmail = addslashes(trim($_POST['email']));
$subject = addslashes(trim($_POST['phone']));
$phone = addslashes(trim($_POST['phone']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
if($antispam == '12' && isEmail($clientEmail) && $phone != '' && $message != '' ){
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $clientEmail\n Message: \n $message\n Phone: $phone";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $clientEmail";
mail($to,$email_subject,$email_body,$headers);
echo json_encode($array);
header('Location: contact-success.html#success');
}
else {
header('Location: contact-failed.html#fail');
}
My question is why you need to show another page instead of the error message in the contact form. You are redirecting to another page on success, the same can be applied on error also, you can redirect to another html page to show different version .
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 7 years ago.
Can someone help me figure out why the below PHP is causing the email to be sent to Gmail spam? I tried following other directions for setting proper headers, but I still run into problems with my emails going into the spam filter in Gmail.
Any help would be appreciated!
<?php
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
$name = trim($_POST['txt_name']);
$fromemail = trim($_POST['txt_email']);
$inquiry = trim($_POST['txt_inquiry']);
$message = trim($_POST['txt_msg']);
//name can contain only alpha characters and space
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Please enter a real name";
}
if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
{
$error = true;
$fromemail_error = "Please enter a valid email address";
}
if(empty($inquiry))
{
$error = true;
$inquiry_error = "Please enter your subject";
}
if(empty($message))
{
$error = true;
$message_error = "Please enter your message";
}
if (!$error)
{
//send mail
$toemail = "myemail#gmail.com";
$subject = "inquiry from visitor " . $name;
$body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Inquiry: $inquiry \n Message: \n $message";
$headers = "From: $fromemail\n";
$headers .= "Reply-To: $fromemail";
$headers .= "Return-Path: $fromemail";
if (mail ($toemail, $inquiry, $body, $headers))
$alertmsg = '<div class="alert alert-success text-center">Message sent successfully. We will get back to you shortly!</div>';
else
$alertmsg = '<div class="alert alert-danger text-center">There is error in sending mail. Please try again later.</div>';
}
}
?>
It could be that your ip address of the server that you are sending the email with it used for other purposes that marked it as a spam address.
Most of the time when you add DKIM and SPF to your email server (if you use one) will solve this problem.
An essier solution would be to use an external mailing service so that you they can handle that for you
I often made the experience, that mails that are directly getting sent by PHP are recognized as SPAM...
my approach is now, to always use a SMTP-Server for sending those emails...
this post could help you!
I have some trouble with this, Anyone know why it won't work?
I have this homepage where it uses this script for sending an email, but it won't work.
When I call that I should get an email sent, but it just runs without any error.
<?php
$emailTo = 'youremail';
$siteTitle = 'SiteTitle';
//error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if(isset($_POST['submitted'])) {
$hasError = false;
// require a name from user
if(trim($_POST['contactName']) === '') {
$nameError = 'name plz!';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot Email?';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'It's not right fool';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'Forgot something=';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$subject = 'New message to '.$siteTitle.' from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nMessage: $comments";
$headers = 'From: ' .' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
//Autoresponse
$respondSubject = 'Thank you for contacting '.$siteTitle;
$respondBody = "Your message to $siteTitle has been delivered! \n\nWe will answer back as soon as possible.";
$respondHeaders = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;
mail($email, $respondSubject, $respondBody, $respondHeaders);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
Add \ in ', in all occurences
$emailError = 'It\'s not right fool';
instead of
$emailError = 'It's not right fool';
You need to escape any 's with \ in strings that are enclosed in 's. For example, you need to change $emailError = 'It's not right fool' to $emailError = 'It\'s not right fool'.
The same goes for "s when enclosed in "s.
You said it runs without any error, so it may not be a PHP error. Have you made sure that SMTP is set up properly on your sever? With the new code you just posted, are there any new errors?
This question already has answers here:
How to validate an Email in PHP?
(7 answers)
How to validate an email address in PHP
(15 answers)
Closed 8 years ago.
I am trying to write a script that does the following:
Prints the users message to the screen and emails it to them as well.
here is my code For some reason it tells me my email address is invalid when it is a valid email address, any suggestions? what am i missing? :-\
<?php
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post');
$errors = null;
$success = true;
function checkEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if($_POST){
$errors = array();
$to = $_POST['email'];
$subject = 'Your Comment';
$message = $_POST['message'];
$headers = 'From: blahblah69#gmail.com' . "\r\n" . 'Reply-To:
mitides.constantin#gmail.com' . "\r\n" .
'X-Mailer: PHP/' .phpversion();
if(!$to || !$message){
$errors[] ="Please fill in both a email and a message.";
} else if(!checkEmail($to) && $_POST['message'] = filter_var($_POST['message'],
FILTER_SANITIZE_STRING));{
$errors[]= print ('You did not enter a valid email, please try again.');
}
if($errors || !$_POST){
if($errors){
foreach($errors as $error){
echo $error. "<br />";
}
}
}
if(!$errors && $success){
mail($to, $subject, $message, $headers) && print($_POST['message']);
} ?>
at first put else if part in a bracket such like this
($_POST['message'] = filter_var($_POST['message'],FILTER_SANITIZE_STRING))