首页 > 解决方案 > 如何在 PHP 中使用 SMTP 发送电子邮件?

问题描述

我正在努力使用 SMTP PHP 发送电子邮件。我正在使用 gmail SMTP。谁能告诉我这段代码的问题在哪里?我正在 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.

强文本

标签: phpsmtpphpmailercpanel

解决方案


谢谢大家,我使用了我的网络托管电子邮件地址、SMTP 主机和 SMTP 端口。有用。我不明白为什么它不使用 gmail SMTP 发送。


推荐阅读