Hello,
I am experiencing the problem that I tried to get a PHP Mailer work.
This is the error I get:

2022-03-07 16:16:11 SERVER -> CLIENT: 220 mail.MYDOMAIN.de ESMTP Postcow
2022-03-07 16:16:11 CLIENT -> SERVER: EHLO ServerIPAddress
2022-03-07 16:16:11 SERVER -> CLIENT: 250-mail.MYDOMAIN.de250-PIPELINING250-SIZE 104857600250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 CHUNKING
2022-03-07 16:16:11 CLIENT -> SERVER: STARTTLS
2022-03-07 16:16:11 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
2022-03-07 16:16:11 CLIENT -> SERVER: QUIT
2022-03-07 16:16:11
2022-03-07 16:16:11
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

    unknwn_usr It looks like you’ve got a problem with your SSL configuration in your PHP.

    Try the code below, if that doesn’t work you’ve got an issue with your certs

    $mail = new PHPMailer(true);
    try {
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->isSMTP();
    $mail->Host = $mailhost; //MAIL SERVER
    $mail->SMTPAuth = true; //REQUIRES AUTH
    $mail->Username = $mailuser; //USERNAME
    $mail->Password = $mailpass; //PASSWORD
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //USE SSL
    $mail->Port = 587; //SSL PORT
    $mail->addReplyTo($replyemail, $replyname); //REPLY TO SET TO EMAIL FROM FORM
    $mail->setFrom($mailuser, $fromname); //FROM EMAIL
    $mail->addAddress($to); //SET RECIPIENT
    $mail->Subject = $subject; //SET MESSAGE SUBJECT
    $mail->Body = $data;
    if($file_name!=""){
    $mail->addAttachment($basedir.$file_name,'Analytics.png');
    }
    $mail->send();
    return true;
    }
    catch (Exception $e) {
    return false;
    }

      Have something to say?

      Join the community by quickly registering to participate in this discussion. We'd like to see you joining our great moo-community!

      Bauhu
      Thanks for your message.

      I got it working with the following:
      $mail->IsSMTP();
      $mail->Host = “localhost”;
      // $mail->SMTPAuth = true;
      $mail->SMTPSecure = false;
      $mail->SMTPAutoTLS = false;
      $mail->Username = “admin@MyDOMAINde”;
      $mail->Password = “myPASSWORD”;
      $mail->Port = 25;

      Somehow it does work, but only to temp-mails. It get’s blocked in Gmail and Outlook.
      Probably because I need the SMTP. While trying to get it working again, an error occurs.
      Could this be a problem with Cloudflare SSL/TLS?

        unknwn_usr

        Looking at your code I’m assuming you’ve got Mailcow on the same machine as your webserver. Your using localhost as the hostname, thus the SSL cert wouldn’t match.

        If your not using Cloudflare as a proxy you should be able to use your FQDN as the mail host.

          Bauhu
          Thanks for your reply! 🙂 I appreciate your help.

          I do have these DNS in my Cloudflare account:
          A - myDomain.de - IPv4 - Proxied
          A - mail - IPv4 - DNS only
          CNAME - autoconfig - mail.myDomain.de - DNS only
          CNAME - autodiscover - mail.myDomain.de - DNS only
          CNAME - www - myDomain.de - Proxied
          MX - myDomain.de - mail.myDomain.de - Priority 10 - DNS only
          TXT - myDomain.de - “v=spf1 mx a -all” - DNS only
          TXT - dkim._domainkey - domain key from mailcow UI
          TXT - _dmarc = “v=DMARC1; p=reject; rua=mailto:admin@myDomain.de

          Cloudflare SSL/TLS is currently turned off. When I change it to “Full”, what do I have to change in the php script, to get Mailcow working with the PHP?

          Replace this:
          $mail->IsSMTP();
          $mail->Host = “localhost”;
          // $mail->SMTPAuth = true;
          $mail->SMTPSecure = false;
          $mail->SMTPAutoTLS = false;

          with:
          $mail->IsSMTP();
          $mail->Host = “mail.myDomain.de”;
          // $mail->SMTPAuth = true;
          $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //USE SSL
          $mail->Port = 587; //SSL PORT

            Bauhu
            I get the following error:
            Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

            Can you ping mail.myDomain.de from your server?

              Bauhu
              root@dedicated-1:~# ping -c 10 mail.myDomain.de
              PING mail.myDomain.de (myIPv4) 56(84) bytes of data.
              64 bytes from myIPv4 (myIPv4): icmp_seq=1 ttl=64 time=0.043 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=2 ttl=64 time=0.067 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=3 ttl=64 time=0.053 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=4 ttl=64 time=0.060 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=5 ttl=64 time=0.063 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=6 ttl=64 time=0.055 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=7 ttl=64 time=0.067 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=8 ttl=64 time=0.057 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=9 ttl=64 time=0.058 ms
              64 bytes from myIPv4 (myIPv4): icmp_seq=10 ttl=64 time=0.058 ms

              — mail.myDomain.de ping statistics —
              10 packets transmitted, 10 received, 0% packet loss, time 222ms

              Can you share the output of your PHPmailer script after adding this:

              $mail->SMTPDebug = SMTP::DEBUG_SERVER;

                Bauhu
                Thanks for your reply!

                I get the following error:
                Invalid host: 0de”
                SMTP Error: Could not connect to SMTP host.
                Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

                My index.php:
                <?php

                $p = $_GET;

                if($p[‘pass’] != “sitePass”) {
                die(‘message’);
                }
                //Import PHPMailer classes into the global namespace
                //These must be at the top of your script, not inside a function
                use PHPMailer\PHPMailer\PHPMailer;
                use PHPMailer\PHPMailer\SMTP;
                use PHPMailer\PHPMailer\Exception;

                //Load Composer’s autoloader
                require ‘vendor/autoload.php’;

                //Create an instance; passing true enables exceptions
                $mail = new PHPMailer(true);

                try {
                //Server settings
                $mail->IsSMTP();
                $mail->Host = “mail.myDomain.de”;
                // $mail->SMTPAuth = true;
                $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //USE SSL
                $mail->Port = 587; //SSL PORT
                $mail->SMTPDebug = SMTP:😃EBUG_SERVER;

                //Recipients
                // $mail->setFrom('from@example.com', 'Mailer');
                $mail->addAddress('example@example.com');     //Add a recipient
                
                //Content
                $mail->isHTML(true);                                  //Set email format to HTML
                $mail->Subject = 'Here is the subject';
                $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
                $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
                
                $mail->send();
                echo 'Message has been sent';

                } catch (Exception $e) {
                echo “Message could not be sent. Mailer Error: {$mail->ErrorInfo}”;
                }
                ?>
                <?php

                Change This:
                $mail->Host = “mail.myDomain.de”;
                to
                $mail->Host = 'mail.myDomain.de';

                  Bauhu

                  2022-03-08 15:07:32 SERVER -> CLIENT: 220 mail.myDomain.de ESMTP Postcow
                  2022-03-08 15:07:32 CLIENT -> SERVER: EHLO myIPv4
                  2022-03-08 15:07:32 SERVER -> CLIENT: 250-mail.myDomain.de250-PIPELINING250-SIZE 104857600250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 CHUNKING
                  2022-03-08 15:07:32 CLIENT -> SERVER: STARTTLS
                  2022-03-08 15:07:32 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
                  SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
                  2022-03-08 15:07:32 CLIENT -> SERVER: QUIT
                  2022-03-08 15:07:32
                  2022-03-08 15:07:32
                  SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
                  Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

                  SSL/TLS is enabled in Cloudflare.

                  Do you get the same error if you add to your code?

                  $mail->SMTPOptions = array(
                  'ssl' => array(
                  'verify_peer' => false,
                  'verify_peer_name' => false,
                  'allow_self_signed' => true
                  )
                  );

                    a year later

                    Bauhu For me this last way did work, but I read in many cases that this is not reccomended for security purposes. What else can be done?

                    No one is typing