Search code examples
phpemailsslxampp

php mail function sometimes works, sometimes fails and tells me to upgrade/update TLS. Solution?


Like the title says. Here is my function:

<?php
  //---------------EMAIL PORTION--------------------
  error_reporting(E_ALL);
  ini_set('display_errors', 'On');
  set_error_handler("var_dump");

  $from_email = 'email2@website.com';
  $to = 'email@website.com';

  $subject = "XXXXXXXXXXX";
  
  $message = 
  '
  <html>
    <body>
      <div style="padding:10px 20px;">
        <h3>Hello Person, </h3>
        <img src="###########"/>
      </div>
    </body>
  </html>
  ';
  
  // Headers
  $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
  $headers .= "Content-type: text/html\r\n";
  $headers .= "From: \"XXXXXXXXXXXXXX\" <".$from_email.">\r\n"; // Sender Email
  $headers .= "Reply-To: " . $from_email . "\r\n";
    
  $sentMailResult = mail($to,$subject,$message,$headers);

  if($sentMailResult) {
    echo "Email Sent Successfully: ", $sentMailResult;
  }
  else {
    die("Sorry but the email could not be sent, Please go back and try again!");
  }

Sometimes it works but most of the time it doesn't. I will be on the php page in my browser, and I will refresh it, and it will say email sent successfully. Then I'll refresh it and it will say failed. I am using Xampp for my server. When it fails I get the following in my server log:

sendmail: Error during delivery: TLS 1.0 and 1.1 are not supported. Please upgrade/update your client to support TLS 1.2. Visit https://aka.ms/smtp_auth_tls. [prod.outlook.com]

Then when it works, my mail log has this in it after the email info:

250 2.0.0 OK
QUIT

I have done everything I can think of and I am beyond confused. I have tried adding this line onto my httpd-ssl.conf file: SSLProtocol all +TLSv1.2 But it doesn't seem to help. I don't understand why it can work sometimes but not all the time. My php.ini mail function should be set up correctly and it links to a sendmail folder and the ini in there should also be setup correctly. Any help would be greatly appreciated.

Update:

Apache version: 2.4.51

PHP version: 8.0.13

OpenSSL version: 1.1.1l


Solution

  • I spent so much time on this, and got help from people a lot smarter than me, and it never worked. PHPMailer is the way to go. Set it up and it worked the first try, and consistently works since. You don't even have to deal with any ini files.