PHPMailer saying could not connect to SMTP host

前端 未结 4 1460
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-24 09:30

For the past 2 days I\'ve been trying to get a PHP script to send an e-mail and it doesn\'t seem to work. First I\'ve tried the normal php mail function, then PHPMailer and then

相关标签:
4条回答
  • 2021-01-24 09:38

    The same problem I had using Gmail and I solved it by downloading the cacert.pem certificate from the site https://curl.haxx.se/docs/caextract.html You should also write the php.ini file as follows:

    extension = php_openssl.dll openssl.cafile = C: \ xampp \ php \ extras \ ssl \ cacert.pem

    It must be activated in the GMail account in the label: Access and security of the option: Allow less secure apps access option: ON

    This solution is thanks to matteobin user contribution, of stackoverflow

    0 讨论(0)
  • 2021-01-24 09:40

    I have the same problem i tried with SSL with port 465 then it said services is unavailable then i try this thing it solve my problem hopefully it solve also yours 1) Make sure you use $mail->IsSMTP(); for the authentication. Ismail is only working for the same server with mail function

    2) This thing fix my problem

    $mail->SMTPSecure = "tsl"; 
    $mail->Port = 587;
    

    i add these two lines now email sending is perfectly working earlier i face this error SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting but above thing solve my problem hopefully it work for you thank you

    0 讨论(0)
  • 2021-01-24 09:59

    Here is the PHP Mailer Example code. Ensure to download the PHPMailer Classes from https://github.com/PHPMailer/PHPMailer

    To find your mail server,

    If you are using windows operating system:

    1. Open up a command prompt (CMD.exe).
    2. Type nslookup and hit enter.
    3. Type set type=MX and hit enter.
    4. Type the domain name and hit enter, for example: youtube.com
    5. The results will be a list of host names that are set up for SMTP

    If you are using Linux

    1. Open a command prompt.
    2. Type dig domain.name MX and hit enter where domain.name is the domain you are trying to find out the smtp server for.
    3. If you do not get any response back, your cpanel might be missing SMTP configuration. Call your technical support for help.

    Mostly, smtp servers name are something like smtp.yourdomain.com or mail.yourdomain.com.

    Open command prompt try to run following two commands:

    1. ping smtp.yourdomain.com
    2. ping mail.yourdomain.com

    You will most probably get response from any one from the above two commands and that might be your smtp server.

    If this doesn't work open your cpanel --> go to your mailing accounts -- > click on configure mail account settings-- > there somewhere in the page you will get the information about your smtp server

    It will be written like this way may be :

    • Incoming Server: mail.yourdomain.com
    • IMAP Port: ---
    • POP3 Port: ---
    • Outgoing Server: mail.yourdomain.com
    • SMTP Port: ---

    Here's the code.

    <?php
    
    // 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\Exception;
    
    require 'your_directory/php_mailer/Exception.php';
    require 'your_directory/php_mailer/PHPMailer.php';
    require 'your_directory/php_mailer/SMTP.php';
    
    $to = 'your_email_here@gmail.com';
    $name = 'my first name';
    // Passing `true` enables exceptions
    //$mail = new PHPMailer(TRUE);                              
    $mail = new PHPMailer();                              
    
    try {
    
      //Set SMTP Options
      $mail->SMTPOptions = array(
                      'ssl' => array(
                          'verify_peer' => false,
                          'verify_peer_name' => false,
                          'allow_self_signed' => true
                      )
                  );
    
      //Server settings
      // Enable verbose debug output
      $mail->SMTPDebug = 2;
      // Set mailer to use SMTP
      $mail->isSMTP();
      // Specify main and backup SMTP servers
      $mail->Host = 'mail.your_domain.com;your_backup_smtp.your_domain.com.co.ke';
      // Enable SMTP authentication
      $mail->SMTPAuth = true;
      // SMTP username (This is smtp sender email. Create one on cpanel e.g noreply@your_domain.com)
      $mail->Username = 'sender_email@your_domain.com';
      // SMTP password (This is that emails' password (The email you created earlier) )
      $mail->Password = 'your_password';
      // Enable TLS encryption, `ssl` also accepted
      $mail->SMTPSecure = 'tls';
      // TCP port to connect to. the port for TLS is 587, for SSL is 465 and non-secure is 25
      $mail->Port = 25;
    
      //Recipients
      $mail->setFrom('sender_email@your_domain.com', 'Company Name');
      // Add a recipient
      $mail->addAddress('recipient_address@gmail.com', 'Recipient Name');
      // Name is optional
      //$mail->addAddress('another_email@example.com');
      $mail->addReplyTo('info@your_domain.com', 'Information Team');
      //$mail->addCC('cc@example.com');
      //$mail->addBCC('bcc@example.com');
    
      //Attachments (Ensure you link to available attachments on your server to avoid errors)
      //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
      //$mail->addAttachment('/tmp/image.jpg', 'some_imaje.jpg');    // Optional name
    
      //Content
      $mail->isHTML(true);                                  // Set email format to HTML
      $mail->Subject = 'Here is the subject';
      $mail->Body    = 'This is the HTML message body <b><i>in bold and italicized!</i></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;
    }
    

    ?>

    0 讨论(0)
  • 2021-01-24 10:01

    The SMTPOptions line in @David Kariuki's solution fixed my issue. Was getting a SMTP connect failure due to the failure in the TLS handshake.

    $mail->SMTPOptions = array(
                      'ssl' => array(
                          'verify_peer' => false,
                          'verify_peer_name' => false,
                          'allow_self_signed' => true
                      )
                  );
    
    0 讨论(0)
提交回复
热议问题