- This topic is empty.
-
AuthorPosts
-
July 31, 2013 at 11:17 am #145298
SkyTown
ParticipantOk so here is my current php code for my contact form.
This is the back end:
<?php /* * Contact Form Class */ header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); $admin_email = '[email protected]'; // Your Email $message_min_length = 5; // Min Message Length class Contact_Form{ function __construct($details, $email_admin, $message_min_length){ $this->name = stripslashes($details['name']); $this->email = trim($details['email']); $this->subject = 'Contact from Your Website'; // Subject $this->message = stripslashes($details['message']); $this->email_admin = $email_admin; $this->message_min_length = $message_min_length; $this->response_status = 1; $this->response_html = ''; } private function validateEmail(){ $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i'; if($this->email == '') { return false; } else { $string = preg_replace($regex, '', $this->email); } return empty($string) ? true : false; } private function validateFields(){ // Check name if(!$this->name) { $this->response_html .= '
Please enter your name
'; $this->response_status = 0; } // Check email if(!$this->email) { $this->response_html .= 'Please enter an e-mail address
'; $this->response_status = 0; } // Check valid email if($this->email && !$this->validateEmail()) { $this->response_html .= 'Please enter a valid e-mail address
'; $this->response_status = 0; } // Check message length if(!$this->message || strlen($this->message) < $this->message_min_length) { $this->response_html .= 'Please enter your message. It should have at least '.$this->message_min_length.' characters
'; $this->response_status = 0; } } private function sendEmail(){ $mail = mail($this->email_admin, $this->subject, $this->message, "From: ".$this->name." <".$this->email.">\r\n" ."Reply-To: ".$this->email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { $this->response_status = 1; //$this->response_html = 'Thank You!
'; $this->response_html = $mail; } } function sendRequest(){ $this->validateFields(); if($this->response_status) { $this->sendEmail(); } $response = array(); $response['status'] = $this->response_status; $response['html'] = $this->response_html; echo json_encode($response); } } $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length); $contact_form->sendRequest(); ?>I’m getting a thank you, but the email never reaches my inbox. Can you guys help me out? The live page that this is used on can be found at : http://www.inthecloudscreative.com .
August 1, 2013 at 8:09 pm #145420thechrisroberts
ParticipantMany shared hosts restrict where email can come from – the from address has to be something hosted with them. You might modify your code to make from use a hosted domain and just include the person’s name and email address as part of the message.
August 1, 2013 at 9:01 pm #145437SkyTown
ParticipantThanks. Thats exactly what was up. Thanks!
August 18, 2013 at 6:17 pm #147301helainefpsantos
ParticipantI`m having troubles with the validating fields.
It always shows the error message if i didnt complete de fields.
Here is the code :
name = stripslashes($details[‘Nome’]);
$this->phone = stripslashes($details[‘Telefone’]);
$this->email = trim($details[‘Email’]);
$this->subject = ‘Fale Conosco – Kaloma’; // Subject
$this->message = stripslashes($details[‘Menssagem’]);$this->email_admin = $email_admin; $this->message_min_length = $message_min_length; $this->response_status = 1; $this->response_html = ''; } private function validateEmail(){ $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i'; if($this->email == '') { return false; } else { $string = preg_replace($regex, '', $this->email); } return empty($string) ? true : false; } private function validateFields(){ // Check name if(!$this->name) { $this->response_html .= '<p>Favor preencher o campo NOME</p>'; $this->response_status = 0; } // Check email if(!$this->email) { $this->response_html .= '<p>Favor preencher o campo EMAIL</p>'; $this->response_status = 0; } // Check valid email if($this->email && !$this->validateEmail()) { $this->response_html .= '<p>Favor preencher o campo com email válido</p>'; $this->response_status = 0; } // Check message length if(!$this->message || strlen($this->message) message_min_length) { $this->response_html .= '<p>Favor digitar sua mensagem. Ela deverá conter no mínimo '.$this->message_min_length.' characters</p>'; $this->response_status = 0; } } private function sendEmail(){ $mail = mail($this->email_admin, $this->subject, $this->message, "From: ".$this->name." email.">\r\n" ."Reply-To: ".$this->email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { $this->response_status = 1; $this->response_html = '<p>Obrigado!</p>'; } } function sendRequest(){ $this->validateFields(); if($this->response_status) { $this->sendEmail(); } $response = array(); $response['status'] = $this->response_status; $response['html'] = $this->response_html; echo json_encode($response); }
}
$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();?>
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.