Gửi mail PHP MYSQL với gói PHPMailer version 6


Gửi mail PHP MYSQL với gói PHPMailer 6

1.Tại sao phải dùng PHPMailer?

Mặc dù trong php đã có sẵn hàm gửi mail rồi nhưng thường thì khi mail sẽ rất khó khăn phát nổ phát xịt, mail của bạn có thể nằm trong mục spam hoặc sever của bạn đã bị chặn hàm gửi mail. Lúc này bạn sẽ cần phải nghĩ đến một giải pháp thay thế đó là sử dụng một thư viện hỗ trợ việc gửi mail. Và ở đây tôi chọn PHPMailer để làm việc này. Ngoài ra còn có pear mail nhưng ít phổ biến hơn và mình cũng chưa dùng nó bao giờ các bạn có thể tìm hiểu thêm và cho ý kiến.

Các tính năng của PHPMailer

  • Gửi mail thông qua giao thức SMTP.
  • Có thể dễ dàng dùng địa chỉ email cá nhân hoặc email công ty của bạn để gửi đi làm tăng độ uy tín cho mail.
  • Gửi mail nhanh ít lỗi mail được chuyển vào thằng vào inbox.
  • Có thể thêm cc,bcc, đính kèm file.
  • Có thể tương tác hai chiều tức là khi khách hàng rep mail bạn sẽ nhận được mail trong hộp thư đến của bạn.

Và còn nhiều hơn thế nữa nếu bạn chưa biết đến nó hãy dùng đi và cảm nhận.

Trước tiên bạn cần download thư viện PHPMailer 6.05 tại đây.

Import thư viện

Bạn thực hiện import thư viện vào như sau, nhớ thay đổi đường dẫn tới mã nguồn mà bạn đã lưu nhé, và phải đặt đoạn code này ở trên cùng của file.

include APPPATH . "../storage/PHPMailer-master/src/PHPMailer.php";
include APPPATH . "../storage/PHPMailer-master/src/Exception.php";
include APPPATH . "../storage/PHPMailer-master/src/OAuth.php";
include APPPATH . "../storage/PHPMailer-master/src/POP3.php";
include APPPATH . "../storage/PHPMailer-master/src/SMTP.php";
 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

Thư viện gửi mail

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
 
    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
 
    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.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!';
    $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;
}

Chi tiết bạn nên xem video để hiểu rõ hơn.



Đóng góp ý kiến của bạn về bài học để admin liên hệ hoặc giúp nhé


CÔNG TY THIẾT KẾ WEBSITE CHUYÊN NGHIỆP

Kết nối với chúng tôi