Как отправить письмо по SMTP в PHP? - PullRequest
0 голосов
/ 06 марта 2020

Я изо всех сил пытаюсь отправить электронную почту, используя SMTP PHP. Я использую SMTP Gmail. Может кто-нибудь сказать мне, где проблема с этим кодом? Я пытаюсь этот код в cpanel, я не могу найти, где проблема в этом коде. В журналах ошибок cpanel отсутствует сообщение об ошибке PHP.

    <?php
    require "config/config.php"; //include config file
    // GOOGLE GOODNESS
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    if (isset($_POST['submit'])) {
    $userIP            = $_SERVER["REMOTE_ADDR"];
    $recaptchaResponse = $_POST['g-recaptcha-response'];
    $secretKey         = $yoursecretkey;
    $request           = file_get_contents("https://www.google.com/recaptcha  /api/siteverify?secret={$secretKey}&response={$recaptchaResponse}&remoteip={$userIP}");

    if (!strstr($request, "true")) {
        echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong>There was a   problem with the Captcha, you lied to us! you are a robot! or you just didnt click it :)</div>';
    } else {
        // echo "WORKS MOTHERFUCKER CONGRATS!";
        if (isset($_POST['submit'])) {

            $message = 'Full Name: ' . $_POST['fullname'] . '<br />
        Subject:    ' . $_POST['subject'] . '<br />
        Phone:  ' . $_POST['phone'] . '<br />
        Email:  ' . $_POST['emailid'] . '<br />
        Comments:   ' . $_POST['comments'] . '
        ';
            require "PHPMailer-master/class.phpmailer.php"; //include phpmailer class


            // Instantiate Class  
            $mail = new PHPMailer();

            // Set up SMTP  
            $mail->IsSMTP(); // Sets up a SMTP connection  
            $mail->SMTPAuth   = true; // Connection with the SMTP does require authorization    
            $mail->SMTPSecure = "ssl"; // Connect using a TLS connection  
            $mail->Host       = "smtp.gmail.com"; //Gmail SMTP server address
            $mail->Port       = 465; //Gmail SMTP port
            $mail->Encoding   = '7bit';

            // Authentication  
            $mail->Username = "xxxxxxx"; // Your full Gmail address
            $mail->Password = "xxxxxxx"; // Your Gmail password

            // Compose
            $mail->SetFrom($_POST['emailid'], $_POST['fullname']);
            $mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
            $mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)  
            $mail->MsgHTML($message);

            // Send To  
            $mail->AddAddress("xxxxxxx", "xxxxxxx"); // Where to send it - Recipient
            $result  = $mail->Send(); // Send!  
            $message = $result ? '<div class="alert alert-success" role="alert"><strong>Success!  </strong>Message Sent Successfully!</div>' : '<div class="alert alert-danger" role="alert">  <strong>Error!</strong>There was a problem delivering the message.</div>';

            unset($mail);

            }
         }
     }


     ?>

The only error i get is: There was a problem delivering the message on contact form.  class.phpmailer.php is included in PHPMailer-master folder. I will appreciate your assistance.

сильный текст

1 Ответ

0 голосов
/ 07 марта 2020

Спасибо, ребята, я использовал свой адрес электронной почты веб-хостинга, SMTP-хост и SMTP-порт. Оно работает. Я не понимаю, почему он не отправляет с использованием gmail SMTP.

...