PHPMailer saying could not connect to SMTP host

前端 未结 4 1523
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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.

    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 in bold and italicized!';
      $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;
    }
    

    ?>

提交回复
热议问题