Jump to content

Mail() Won't Send


timothyarden

Recommended Posts

Hi PHP Freaks,

I have written a php email form and then this code behind it. When I run it comes up with the error page however all inputs are filled in and valid. Here is my code:

 

<?php
   //variables from form
   $firstname = ucwords(strtolower($_POST['firstname']));
   $lastname = ucwords(strtolower($_POST['lastname']));
   $client_email_address = $_POST['email_address'];
   $subject = $_POST['subject'];
   $message = wordwrap($_POST['message'], 150, '<br />');
   $fullname = $firstname.' '.$lastname;

   //error pages
   $email_successful_page = 'mail_successful.php'; //create mail_successful.php;
   $email_failed_page = 'delivery_failed.php'; //create delivery_failed.php; //create emailform.php (and in it the ability to read the error from this script and tell the user

   //set date
   date_default_timezone_set('Australia/NSW');

   //email 1 is sent to the scd admin email
   $email_1_recipient_address = '[email protected]'; //'[email protected]';
   $email_1_subject = 'Email sent to '.$dentist_name.' from '.$client_email.' regarding '.$subject;
   $email_1_message = '<html>\n\t<body>\n\t\t<img src=\"emailheader.jpg\" width="" height="" style="float:center;"/>\n\t\t';
   $email_1_message .= '\n\t\t<h1>Hi System Administrator, </h1>';
   $email_1_message .= '\n\t\t<br /><p>For records, </p>';
   //$email_1_message .= '';
   //$email_1_message .= '';
   $email_1_headers = "From: [email protected] \r\n";
   $email_1_headers .= "Reply-To: [email protected] \r\n";
   $email_1_headers .= "X-Mailer: PHP/".phpversion()." \r\n";
   $email_1_headers .= "To: [email protected] \r\n";
   $email_1_headers .= "Content-type: text/html; charset=UTF-8 \r\n";
   $email_1_headers .= "MIME-Version: 1.0 \r\n";
   //$email_1_headers .= " \r\n";

   //email 2 is sent to the intended dentist / practitioner
   $email_2_recipient_address = '[email protected]'; //'[email protected]';
   $email_2_subject = $subject;
   $email_2_message = 'message';
   //$email_2_message .= '';
   //$email_2_message .= '';
   $email_2_headers = "From: [email protected] \r\n";
   $email_2_headers .= "Reply-To: [email protected] \r\n";
   $email_2_headers .= "X-Mailer: PHP/".phpversion()." \r\n";
   $email_2_headers .= "To: [email protected] \r\n";
   $email_2_headers .= "Content-type: text/html; charset=UTF-8 \r\n";
   $email_2_headers .= "MIME-Version: 1.0 \r\n";
   //$email_2_headers .= " \r\n";

   //email 3 is a confirmation email sent to the client
   $email_3_recipient_address = $client_email_address;
   $email_3_subject = 'Confirmation Email Regarding: '.$subject;
   $email_3_message = 'message';
   //$email_3_message .= '';
   //$email_3_message .= '';
   $email_3_headers = "From: [email protected] \r\n";
   $email_3_headers .= "Reply-To: [email protected] \r\n";
   $email_3_headers .= "X-Mailer: PHP/".phpversion()." \r\n";
   $email_3_headers .= "To: ".$client_email." \r\n";
   $email_3_headers .= "Content-type: text/html; charset=UTF-8 \r\n";
   $email_3_headers .= "MIME-Version: 1.0 \r\n";
   //$email_3_headers .= " \r\n";



   //form validation function
   function form_validation($var1,$var2,$var3,$var4,$var5){
       if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email_address']) && isset($_POST['subject']) && isset($_POST['message'])){
           if(strlen($var1) < 1){
               return false;
               $error = "firstname";
           }

           elseif(strlen($var2) < 1){
               return false;
               $error = "lastname";
           }

           elseif(!filter_var($var3, FILTER_VALIDATE_EMAIL)){
               return false;
               $error = "invalid email address";
           }

           elseif(strlen($var4) < 1){
               return false;
               $error = "subject";
           }

           elseif(strlen($var5) < 1){
               return false;
               $error = "message";
           }

           else{
               return true;
           }
       }

       else{
           return false;
       }
   }

   //emailing function
   function email($email_1_var_1,$email_1_var_2,$email_1_var_3,$email_1_var_4,$email_2_var_1,$email_2_var_2,$email_2_var_3,$email_2_var_4,$email_3_var_1,$email_3_var_2,$email_3_var_3,$email_3_var_4){
       if(!mail($email_1_var_1,$email_1_var_2,$email_1_var_3,$email_1_var_4)){
           $email_error = "email 1 error";
       }

       if(!mail($email_2_var_1,$email_2_var_2,$email_2_var_3,$email_2_var_4)){
           $email_error = "email 2 error";
       }

       if(!mail($email_3_var_1,$email_3_var_2,$email_3_var_3,$email_3_var_4)){
           $email_error = "email 3 error";
       }
   }

   //if form validation fails - redirect to error page - mail
   if(!form_validation($firstname,$lastname,$email_address,$subject,$message)){
       header('Location: '.$email_failed_page.'?error='.$error);
   }

   else{
       if(!email($email_1_recipient_address,$email_1_subject,$email_1_message,$email_1_headers,$email_2_recipient_address,$email_2_subject,$email_2_message,$email_2_headers,$email_3_recipient_address,$email_3_subject,$email_3_message,$email_3_headers)){
           header('Location: '.$email_failed_page.'?email_error='.$email_error);
       }

       else{
           header('Location: '.$email_succesful_page);
       }    
   }

 

Any advice is welcome.

Thanks everyone,

 

Timothy

Link to comment
https://forums.phpfreaks.com/topic/269157-mail-wont-send/
Share on other sites

Christian - Thats the thing, it wont send the emails but there is no error - the error log is empty - the error variable is not set as all inputs were filled and valid. But it still redirected to my error page and the mail won't send can't figure it out.

 

Thanks for the advice Jessica, will do.

Link to comment
https://forums.phpfreaks.com/topic/269157-mail-wont-send/#findComment-1383375
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.