Php phpmailer not sending email

I had created an automation system with php which takes information straight from a contact form on my website and fills in the information into a word template contract and sends me the contract with the filled in information.

My code was working just fine until recently when my website had an issue but now the automation is not working. I created this automation 2 years ago and I found the original code that I used before but even that isn't working.

<?php
    require_once "vendor/autoload.php";
    
    // define variables and set to empty values
    $name_1 = $name_2 = $surname_1 = $surname_2 = $id_number_1 = $id_number_2 = $email_1 = $email_2 = $phone_1 = $phone_2 = $valofestate_1 = $valofestate_2 = "";
    
    // define variables and set to empty values
    $property_owner_1 = $property_owner_2 = $property_desc_1 = $property_desc_2 = $business_owner_1 = $business_owner_2 = $business_desc_1 = $business_desc_2 = "";
      
    // define variables and set to empty values
    $success = "";
    $error = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    if($_POST['marital_dispensation'] == 'With Accrual')
    {
          function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
      }  
      // test input for party 1
      $name_1 = test_input($_POST["name_1"]);
      $surname_1 = test_input($_POST["surname_1"]);
      $id_number_1 = test_input($_POST["id_number_1"]);
      $email_1 = test_input($_POST["email_1"]);
      $phone_1 = test_input($_POST["phone_1"]);
      $valofestate_1 = test_input($_POST["valofestate_1"]);
      $property_owner_1 = test_input($_POST["property_owner_1"]);
      $property_desc_1 = test_input($_POST["property_desc_1"]);
      $business_owner_1 = test_input($_POST["business_owner_1"]);
      $business_desc_1 = test_input($_POST["business_desc_1"]);
    

      
      // test additional inputs
      $marriage_date = test_input($_POST["marriage_date"]);  
      $marital_dispensation = test_input($_POST["marital_dispensation"]);  
      $city = test_input($_POST["city"]);   
      $template_file_name = '(Accrual 2020).docx';;
     
    
      $fileName = "(Accrual 2020)" . ".docx";
     
      $folder   = "results";
      $full_path = $folder . '/' . $fileName;
     
    try
    {
        if (!file_exists($folder))
        {
            mkdir($folder);
        }       
             
        //Copy the Template file to the Result Directory
        copy($template_file_name, $full_path);
     
        // add calss Zip Archive
        $zip_val = new ZipArchive;
    $fill_1 = (string) $_POST["name_1"];
    $fill_2 = (string) $_POST["surname_1"];
    $fill_3 = (string) $_POST["name_2"];
    $fill_4 = (string) $_POST["surname_2"];
    $fill_5 = (string) $_POST["id_number_1"];
    $fill_6 = (string) $_POST["id_number_2"];
    $fill_7 = (string) $_POST["valofestate_1"];
    $fill_8 = (string) $_POST["valofestate_2"];
        //Docx file is nothing but a zip file. Open this Zip File
        if($zip_val->open($full_path) == true)
        {
            // In the Open XML Wordprocessing format content is stored.
            // In the document.xml file located in the word directory.
             
            $key_file_name = 'word/document.xml';
            $message = $zip_val->getFromName($key_file_name);                
                         
            $timestamp = date('d-M-Y H:i:s');
             
            // this data Replace the placeholders with actual values
            $message = str_replace("token1", " $fill_1",       $message);
            $message = str_replace("token2", " $fill_2",           $message);
            $message = str_replace("Token3", " $fill_3",                  $message);      
            $message = str_replace("token4", " $fill_4",           $message); 
            $message = str_replace("token5", " $fill_5",       $message);
            $message = str_replace("token6", " $fill_6",           $message);
            $message = str_replace("token7", " $fill_7",                  $message);      
            $message = str_replace("token8", " $fill_8",           $message); 
             
            //Replace the content with the new content created above.
            $zip_val->addFromString($key_file_name, $message);
            $zip_val->close();
        }
    }
    catch (Exception $exc) 
    {
        $error_message =  "Error creating the Word Document";
        var_dump($exc);
    };
    require_once "vendor/autoload.php";
    
     //$file_to_attach = 'AntenuptialContract.doc';
     
        $mail = new PHPMailer;
    
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->Timeout  =   10;                               // Set Timeout for script
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'email@gmail.com';                 // SMTP username
        $mail->Password = 'password';                           // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to
    
        $mail->setFrom('email@gmail.com', 'Website');
        $mail->addAddress('email@gmail.com'); // Add receipient Name is optional
        $mail->addReplyTo('email');
          $mail->addReplyTo('info@example.com', 'Information');
    
    
        $file_name="(Accrual 2020).docx";
    
        $mail->addAttachment("results/".$file_name);
        $mail->isHTML(true);                                  // Set email format to HTML
    
        $mail->Subject = 'Website E-mail: Antenuptial Form';
        $mail->Body    = 'Thank you for visiting. The draft contract will follow soon.'
          
                        .'<h3>Party One</h3>'
                        .'<b>Name</b>: '.$name_1.'<br>'
                        .'<b>Surname</b>: '.$surname_1.'<br>'
                        .'<b>ID Number</b>: '.$id_number_1.'<br>'
                        .'<b>E-mail</b>: '.$email_1.'<br>'
                        .'<b>Phone</b>: '.$phone_1.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_1.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_1.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_1.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_1.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_1.'<br>'
                        .'<br><h3>Party Two</h3>'
                        .'<b>Name</b>: '.$name_2.'<br>'
                        .'<b>Surname</b>: '.$surname_2.'<br>'
                        .'<b>ID Number</b>: '.$id_number_2.'<br>'
                        .'<b>E-mail</b>: '.$email_2.'<br>'
                        .'<b>Phone</b>: '.$phone_2.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_2.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_2.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_2.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_2.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_2.'<br>'
                        .'<br><h3>Additional Information</h3>'
                        .'<b>Date of Marriage</b>: '.$marriage_date.'<br>'
                        .'<b>Marital Dispensation</b>: '.$marital_dispensation.'<br>'
                        .'<b>Current City</b>: '.$city.'<br>'
          
                  ;
      
      
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        if(!$mail->send()) {
            $error = "<div class=\"alert alert-dismissible alert-danger\"> ";
            $error .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $error .= "<strong>Error!</strong> The form was not send, please call us directly.";
            $error .= "</div>";
        } else {
            $success = "<div class=\"alert alert-dismissible alert-success\"> ";
            $success .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $success .= "<strong>Success!</strong> The form was E-mailed successfully.";
            $success .= "</div>"; 
            $success .= header("Location:thanks.php");
        }
      
      
      
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
     
        $mail = new PHPMailer;
    
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->Timeout  =   10;                               // Set Timeout for script
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'email@gmail.com';                 // SMTP username
        $mail->Password = 'password';                           // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to
    
        $mail->setFrom('email@gmail.com', 'Website');
        $mail->addReplyTo('email');
        $mail->addCC($email_1);
        $mail->isHTML(true);                                  // Set email format to HTML
        
        $mail->Subject = 'Website E-mail: Antenuptial Form';
        $mail->Body    = 'Thank you for visiting. The draft contract will follow soon.'
          
                        .'<h3>Party One</h3>'
                        .'<b>Name</b>: '.$name_1.'<br>'
                        .'<b>Surname</b>: '.$surname_1.'<br>'
                        .'<b>ID Number</b>: '.$id_number_1.'<br>'
                        .'<b>E-mail</b>: '.$email_1.'<br>'
                        .'<b>Phone</b>: '.$phone_1.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_1.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_1.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_1.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_1.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_1.'<br>'
                        .'<br><h3>Party Two</h3>'
                        .'<b>Name</b>: '.$name_2.'<br>'
                        .'<b>Surname</b>: '.$surname_2.'<br>'
                        .'<b>ID Number</b>: '.$id_number_2.'<br>'
                        .'<b>E-mail</b>: '.$email_2.'<br>'
                        .'<b>Phone</b>: '.$phone_2.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_2.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_2.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_2.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_2.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_2.'<br>'
                        .'<br><h3>Additional Information</h3>'
                        .'<b>Date of Marriage</b>: '.$marriage_date.'<br>'
                        .'<b>Marital Dispensation</b>: '.$marital_dispensation.'<br>'
                        .'<b>Current City</b>: '.$city.'<br>'
          
                  ;
      
      
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        
        
       if(!$mail->send()) {
            $error = "<div class=\"alert alert-dismissible alert-danger\"> ";
            $error .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $error .= "<strong>Error!</strong> The form was not send, please call us directly.";
            $error .= "</div>";
        } else {
            $success = "<div class=\"alert alert-dismissible alert-success\"> ";
            $success .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $success .= "<strong>Success!</strong> The form was E-mailed successfully.";
            $success .= "</div>"; 
            $success .= header("Location:thanks.php");
        }
    
    
    }
    }   
    if($_POST['marital_dispensation'] == 'Without Accrual')
    {  
     
          function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
      }  
      // test input for party 1
      $name_1 = test_input($_POST["name_1"]);
      $surname_1 = test_input($_POST["surname_1"]);
      $id_number_1 = test_input($_POST["id_number_1"]);
      $email_1 = test_input($_POST["email_1"]);
      $phone_1 = test_input($_POST["phone_1"]);
      $valofestate_1 = test_input($_POST["valofestate_1"]);
      $property_owner_1 = test_input($_POST["property_owner_1"]);
      $property_desc_1 = test_input($_POST["property_desc_1"]);
      $business_owner_1 = test_input($_POST["business_owner_1"]);
      $business_desc_1 = test_input($_POST["business_desc_1"]);
    

      
      // test additional inputs
      $marriage_date = test_input($_POST["marriage_date"]);  
      $marital_dispensation = test_input($_POST["marital_dispensation"]);  
      $city = test_input($_POST["city"]);   
      $template_file_name = '(Excluding 2020).docx';
     
    
      $fileName = "Accrual(Excluding 2020)" . ".docx";
     
      $folder   = "results";
      $full_path = $folder . '/' . $fileName;
     
    try
    {
        if (!file_exists($folder))
        {
            mkdir($folder);
        }       
             
        //Copy the Template file to the Result Directory
        copy($template_file_name, $full_path);
     
        // add calss Zip Archive
        $zip_val = new ZipArchive;
        $fill_1 = (string) $_POST["name_1"];
        $fill_2 = (string) $_POST["surname_1"];
        $fill_3 = (string) $_POST["name_2"];
        $fill_4 = (string) $_POST["surname_2"];
        $fill_5 = (string) $_POST["id_number_1"];
        $fill_6 = (string) $_POST["id_number_2"];
        $fill_7 = (string) $_POST["valofestate_1"];
        $fill_8 = (string) $_POST["valofestate_2"];
        //Docx file is nothing but a zip file. Open this Zip File
        if($zip_val->open($full_path) == true)
        {
            // In the Open XML Wordprocessing format content is stored.
            // In the document.xml file located in the word directory.
             
            $key_file_name = 'word/document.xml';
            $message = $zip_val->getFromName($key_file_name);                
                         
            $timestamp = date('d-M-Y H:i:s');
             
            // this data Replace the placeholders with actual values
            $message = str_replace("token1", " $fill_1",       $message);
            $message = str_replace("token2", " $fill_2",           $message);
            $message = str_replace("Token3", " $fill_3",                  $message);      
            $message = str_replace("token4", " $fill_4",           $message); 
            $message = str_replace("token5", " $fill_5",       $message);
            $message = str_replace("token6", " $fill_6",           $message);
            $message = str_replace("token7", " $fill_7",                  $message);      
            $message = str_replace("token8", " $fill_8",           $message); 
             
            //Replace the content with the new content created above.
            $zip_val->addFromString($key_file_name, $message);
            $zip_val->close();
        }
    }
    catch (Exception $exc) 
    {
        $error_message =  "Error creating the Word Document";
        var_dump($exc);
    };
    require_once "vendor/autoload.php";
    
     //$file_to_attach = 'AntenuptialContract.doc';
     
        $mail = new PHPMailer;
    
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->Timeout  =   10;                               // Set Timeout for script
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'email@gmail.com';                 // SMTP username
        $mail->Password = 'password';                           // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to
    
        $mail->setFrom('email@gmail.com', 'Website');
        $mail->addAddress('email@gmail.com'); // Add receipient Name is optional
        $mail->addReplyTo('email');
    
    
        $file_name="Accrual(Excluding 2020).docx";
    
        $mail->addAttachment("results/".$file_name);
        $mail->isHTML(true);                                  // Set email format to HTML
    
        $mail->Subject = 'Website E-mail: Antenuptial Form';
        $mail->Body    = 'Thank you for visiting. The draft contract will follow soon.'
          
                        .'<h3>Party One</h3>'
                        .'<b>Name</b>: '.$name_1.'<br>'
                        .'<b>Surname</b>: '.$surname_1.'<br>'
                        .'<b>ID Number</b>: '.$id_number_1.'<br>'
                        .'<b>E-mail</b>: '.$email_1.'<br>'
                        .'<b>Phone</b>: '.$phone_1.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_1.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_1.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_1.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_1.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_1.'<br>'
                        .'<br><h3>Party Two</h3>'
                        .'<b>Name</b>: '.$name_2.'<br>'
                        .'<b>Surname</b>: '.$surname_2.'<br>'
                        .'<b>ID Number</b>: '.$id_number_2.'<br>'
                        .'<b>E-mail</b>: '.$email_2.'<br>'
                        .'<b>Phone</b>: '.$phone_2.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_2.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_2.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_2.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_2.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_2.'<br>'
                        .'<br><h3>Additional Information</h3>'
                        .'<b>Date of Marriage</b>: '.$marriage_date.'<br>'
                        .'<b>Marital Dispensation</b>: '.$marital_dispensation.'<br>'
                        .'<b>Current City</b>: '.$city.'<br>'
          
                  ;
      
      
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        if(!$mail->send()) {
            $error = "<div class=\"alert alert-dismissible alert-danger\"> ";
            $error .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $error .= "<strong>Error!</strong> The form was not send, please call us directly.";
            $error .= "</div>";
        } else {
            $success = "<div class=\"alert alert-dismissible alert-success\"> ";
            $success .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $success .= "<strong>Success!</strong> The form was E-mailed successfully.";
            $success .= "</div>"; 
            $success .= header("Location:thanks.php");
        }
      
      
      
      
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
         
        $mail = new PHPMailer;
    
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output
    
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->Timeout  =   10;                               // Set Timeout for script
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'email@gmail.com';                 // SMTP username
        $mail->Password = 'password';                           // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to
    
        $mail->setFrom('email@gmail.com', 'Website'); 
        $mail->addReplyTo('email');
     
        $mail->addCC($email_1);
        $mail->isHTML(true);                                  // Set email format to HTML
        
        $mail->Subject = 'Website E-mail: Antenuptial Form';
        $mail->Body    = 'Thank you for visiting. The draft contract will follow soon'
          
                        .'<h3>Party One</h3>'
                        .'<b>Name</b>: '.$name_1.'<br>'
                        .'<b>Surname</b>: '.$surname_1.'<br>'
                        .'<b>ID Number</b>: '.$id_number_1.'<br>'
                        .'<b>E-mail</b>: '.$email_1.'<br>'
                        .'<b>Phone</b>: '.$phone_1.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_1.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_1.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_1.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_1.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_1.'<br>'
                        .'<br><h3>Party Two</h3>'
                        .'<b>Name</b>: '.$name_2.'<br>'
                        .'<b>Surname</b>: '.$surname_2.'<br>'
                        .'<b>ID Number</b>: '.$id_number_2.'<br>'
                        .'<b>E-mail</b>: '.$email_2.'<br>'
                        .'<b>Phone</b>: '.$phone_2.'<br>'
                        .'<b>Value of Estate</b>: '.$valofestate_2.'<br>'
                        .'<b>Property Owner</b>: '.$property_owner_2.'<br>'
                        .'<b>Property Description</b>: '.$property_desc_2.'<br>'
                        .'<b>Business Owner</b>: '.$business_owner_2.'<br>'
                        .'<b>Business Description</b>: '.$business_desc_2.'<br>'
                        .'<br><h3>Additional Information</h3>'
                        .'<b>Date of Marriage</b>: '.$marriage_date.'<br>'
                        .'<b>Marital Dispensation</b>: '.$marital_dispensation.'<br>'
                        .'<b>Current City</b>: '.$city.'<br>'
          
                  ;
      
      
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        
        
       if(!$mail->send()) {
            $error = "<div class=\"alert alert-dismissible alert-danger\"> ";
            $error .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $error .= "<strong>Error!</strong> The form was not send, please call us directly.";
            $error .= "</div>";
        } else {
            $success = "<div class=\"alert alert-dismissible alert-success\"> ";
            $success .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>";
            $success .= "<strong>Success!</strong> The form was E-mailed successfully.";
            $success .= "</div>"; 
            $success .= header("Location:thanks.php");
        }
    
    
    
    }
     
    
    } 
    
    }
    ?>
    
     <!-- THis is where my form code is -->
    
    
    <!-- End of document -->
    <?php include 'footer.php'; ?>

2 answers

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum