Cần giúp đỡ sửa lỗi khi gửi mail bằng PHPmailer

Fatal error: Maximum execution time of 30 seconds exceeded in D:\xamp\htdocs\back-end\lesson\section-27\shopping-cart\libraries\PHPMailer\src\SMTP.php on line 1294

Cho mình hỏi ở đây ai gặp lỗi này ko ạ, mình chỉnh thời gian ở trong php.ini ko được nha, ở email gửi đi thì Cho phép ứng dụng kém an toàn: BẬT , mình cũng ko đặt bảo vệ 2 lớp j cả, ai giúp mình với ạ, mình cảm ơn ạ

xem lại config mail server thôi, thường là không tìm thấy server mail nên mới lâu như vậy

3 Likes

Phần config đây ạ:

$config['email'] = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_port' => 578,
    'smtp_user' => '[email protected]',
    'smtp_fullname' => 'Anh Trần',
    'smtp_pass' => 'xxxxxxx',
    'smtp_secure' => 'tls', 
    'smtp_timeout' => '7',
    'mailtype' => 'html',
    'charset' => 'UTF-8'
);

Còn đây là hàm send_mail ạ:

<?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 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

// Instantiation and passing `true` enables exceptions
function send_mail($sent_to_email, $sent_to_fullname, $subject,$content , $option = array()) {
    global $config;
    $config_email =$config['email'];
    $mail = new PHPMailer(true);
        
    try {
        //Server settings
        $mail->SMTPDebug = 0;                                       // Enable verbose debug output
        $mail->isSMTP();
        // SMTP mail google
        $mail->Host = $config_email['smtp_host'];  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;
        // Khai báo thông tin tài khoản của mình - Người gửi
        $mail->Username =  $config_email['smtp_user'];                     // SMTP username
        $mail->Password =  $config_email['smtp_fullname'];                               // SMTP password
        $mail->SMTPSecure = $config_email['smtp_secure'];
        // Port smtp mail google
        $mail->Port =  $config_email['smtp_port'];
        $mail->CharSet = 'UTF-8'; // TCP port to connect to
        //Recipients
        // Người gửi
        $mail->setFrom($config_email['smtp_user'], $config_email['smtp_fullname']);
        // Người nhận
        $mail->addAddress($sent_to_email, $sent_to_fullname);     // Add a recipient
        // Thêm người nhận
//    $mail->addAddress('[email protected]');               // Name is optional
//    $mail->addAddress('[email protected]'); 
//    $mail->addAddress('[email protected]'); 
        // -----------------------------
        // Nếu phản hồi thì phản hồi qua mail nào
        $mail->addReplyTo($config_email['smtp_user'], $config_email['smtp_fullname']);

        // CC đến người nhận khác
//    $mail->addCC('[email protected]');
//    $mail->addBCC('[email protected]');
        // Attachments
        // Đính kèm file
//        $mail->addAttachment('images/1.jpg');         // Add attachments
//        $mail->addAttachment('images/1.jpg', 'girl.jpg');    // Optional name
        // Content
        // Nội dung gửi thư
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body = $content;
//    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        return 'Message has been sent';
    } catch (Exception $e) {
        return "Message could not be sent. Mailer Error: . {$mail->ErrorInfo}";
    }
}

Đây là gọi hàm gửi mail ạ:

send_mail('[email protected]', 'Anh Trần', 'Khôi phục mật khẩu', $content);

Quào!

Cho mọi người biết mật khẩu luôn à bạn. Đăng lên nhớ kiểm xem có nội dung nào bảo mật thì che đi nhé. Cho dù đó là tài khoản để thử thôi. Ai ác ý thì tự nhiên lại có lỗi mà không biết.

4 Likes

đừng xài thẳng password của gmail, vào Google Account enable 2 step verification rồi tạo app password mà xài ấy :V

vô gmail đổi pass đi :V

các bước tạo app password:

  • Vào https://myaccount.google.com/security. Enable 2-step verification

  • Khi đã enable 2-step verification nó sẽ hiện dòng App passwords, click vô dòng đó.

  • Bấm vào ô Select app,
    image

  • Chọn Custom name,
    image

  • Gõ 1 cái tên gì đó để sau này khi cần xóa app password nào thì xóa, ví dụ ở đây là PHPmailer_<tên_trang_web_của_em>
    image

  • Bấm Generate, nó sẽ hiện ra
    image

  • Copy 16 chữ cái trong cái ô vàng, rồi paste vào dòng 'smtp_pass' => 'aaaabbbbccccdddd', (viết liền ko cần khoảng trống)

  • Sau này khi cần bỏ quyền send mail của demo app này, em chỉ cần vào App passwords tìm tới dòng có tên app em cần xóa, rồi bấm xóa app password này là xong.
    image

  • Mỗi 1 app em nên tạo 1 app passw riêng, đừng xài chung :V.Lỡ sau này có bị lộ pass kiểu này thì em chỉ cần vào App passwords xóa rồi tạo app pw mới copy paste vô lại là được, chỉ ảnh hưởng 1 app vì ko xài chung :V

6 Likes

dạ e cảm ơn a ạ, e mới học cái này nên thử xem gửi như nào nên cũng chưa biết bảo mật lắm

E cảm ơn nha, do e tạo cái gmail thử xem gửi như nào nên cũng ko quan tâm lắm ạ

MÌnh vào sửa rồi vẫn bị lỗi như vậy, ko biết sao nữa, mình đang làm đến phần xác nhận mật khẩu với lấy lại mật khẩu, mà bị như này thì bó tay

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?