
Php Mailer là một thư viện giúp chúng ta có thể gửi email đến người dùng trên website một cách dễ dàng, tỉ lệ thành công cao. Nếu bạn đang muốn gửi mail khi người dùng đăng ký tài khoản, kích hoạt tài khoản, xác nhận đơn hàng, liên hệ thì đây là một công cụ rất tuyệt vời, tôi sẽ gửi đến bạn ngay bây giờ. là một thư viện giúp chúng ta có thể gửi email đến người dùng trên website một cách dễ dàng, tỉ lệ thành công cao. Nếu bạn đang muốn gửi mail khi người dùng đăng ký tài khoản, kích hoạt tài khoản, xác nhận đơn hàng, liên hệ thì đây là một công cụ rất tuyệt vời, tôi sẽ gửi đến bạn ngay bây giờ.
Trên thực tế trong Php có hàm
Bạn Đang Xem: Hướng dẫn phpmailer reply to – phpmailer trả lời cho
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
0 để gửi mail tuy nhiên nó thường bị bị chặn bởi các đơn vị cung cấp server vì email được gửi đi thường vào hòm spam và không mang lại kết quả như mong muốn.
Chính vì vậy Php Mailer đó là một giải pháp cứu cánh vô cùng hiệu quả, với một số ưu điểm như sau:
Tại sao cần gửi email bằng PhpMailer
- Là thư viện gửi email phổ biến nhất của php
- Tỉ lệ email gửi đi thành công đến được với hòm inbox của người dùng cao, tỉ lệ email vào spam rất ít.
- Email gửi với tốc độ nhanh, sau vài giây là xong
- Nội dung email có thể định dạng hiển thị như mong muốn bằng html
- Có thể gửi đến nhiều người cùng một lúc CC/BCC
- Có thể cấu hình người nhận email phản hồi
Checklist sử dụng PhpMailer
Bước 1: Download PhpMailer
Bạn download ngay tại đây: https://github.com/PHPMailer/PHPMailer
Bước 2: Khai báo thư viện vào file gửi mail
Bên dưới là cấu trúc file thư mục chúng ta gửi email
sendmail.php PHPMailer ---|src -------|Exception.php -------|PHPMailer.php -------|SMTP.php
Trong đó:
- sendmail.php là file dùng để gọi hàm gửi mail
- PhpMailer là thư viện sau khi đã download ở bước trên
Để có thể sử dụng được thư viện này chúng ta cần require
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
1 vào file gửi mail
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
2.
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
Quan trọng: Bạn cần chú ý ở đường dẫn file được require bên trên, nó phải chính xác thì mới chạy ok.
Bước 4: Cấu hình gửi email bằng PhpMailer
Khai báo server gửi mail, thông thường chúng ta gửi mail qua tài khoản gmail chính vì vậy bạn cần khai báo như sau.
Xem Thêm : 6 lập dàn ý tả người lớp 5 hot nhất, đừng bỏ qua
Xem Thêm : 4 oppo share tốt nhất
Host
$mail->Host = 'smtp.gmail.com';
Port
Port bạn khai báo
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
3hoặc
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
4
$mail->Port = 587;
Tiếp tục là thông tin tài khoản email người gửi. Bạn có thể tạo ra một tài khoản gmail mới chuyên dùng để gửi mail hệ thống sau đó khai báo cấu hình như bên dưới
Tài khoản email người gửi
$mail->Username = ''; $mail->Password = 'secret';
Giả sử tôi sử dụng email
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
5 với mật khẩu
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
6 thì tôi khai báo như sau:
$mail->Username = ''; $mail->Password = '[email protected]#';
Thông tin người nhận email
$mail->setFrom('', 'Mailer'); $mail->addAddress('', 'Joe User'); $mail->addAddress(''); $mail->addReplyTo('', 'Information'); $mail->addCC(''); $mail->addBCC('b');
Trong đó:
- sendmail.php là file dùng để gọi hàm gửi mail
- PhpMailer là thư viện sau khi đã download ở bước trên
- Để có thể sử dụng được thư viện này chúng ta cần require
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
1 vào file gửi mail
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
2.
- Quan trọng: Bạn cần chú ý ở đường dẫn file được require bên trên, nó phải chính xác thì mới chạy ok.
Bước 4: Cấu hình gửi email bằng PhpMailer
Khai báo server gửi mail, thông thường chúng ta gửi mail qua tài khoản gmail chính vì vậy bạn cần khai báo như sau.
$mail->addAttachment('https://cdn.quatangtiny.com/var/tmp/file.tar.gz'); $mail->addAttachment('https://cdn.quatangtiny.com/tmp/image.jpg', 'new.jpg');
Xem Thêm : 6 lập dàn ý tả người lớp 5 hot nhất, đừng bỏ qua
Xem Thêm : 4 oppo share tốt nhất
Host
Port
$mail->isHTML(true); $mail->Subject = 'Đây là tiêu đề email'; $mail->Body = 'Đây là nội dung email gửi từ <b>Unitop!</b>'; $mail->AltBody = 'Đây là nội dung khi gửi plain text không sử dụng định dạng html';
Port bạn khai báo
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
3hoặc
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
4
- Tiếp tục là thông tin tài khoản email người gửi. Bạn có thể tạo ra một tài khoản gmail mới chuyên dùng để gửi mail hệ thống sau đó khai báo cấu hình như bên dưới
- Tài khoản email người gửi
- Giả sử tôi sử dụng email
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
5 với mật khẩu
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
6 thì tôi khai báo như sau:
- Thông tin người nhận email
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
7: Hiển thị thông tin người gửi khi người nhập mở email
<?php // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerSMTP; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; // Instantiation and passing `true` enables exceptions $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER;// Enable verbose debug output $mail->isSMTP();// gửi mail SMTP $mail->Host = 'smtp.gmail.com';// Set the SMTP server to send through $mail->SMTPAuth = true;// Enable SMTP authentication $mail->Username = '';// SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;// Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('', 'Mailer'); $mail->addAddress('', 'Joe User'); // Add a recipient $mail->addAddress(''); // Name is optional $mail->addReplyTo('', 'Information'); $mail->addCC(''); $mail->addBCC('b'); // Attachments $mail->addAttachment('https://cdn.quatangtiny.com/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('https://cdn.quatangtiny.com/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 <b>in bold!</b>'; $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}"; }
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
8: Khai báo email người nhận
<?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';
9: Khai báo email nhận được phản hồi của người nhận, nếu không khai báo nó mặc định gửi lại địa chỉ chúng ta gửi đi.: Tài khoản email người gửi cần bỏ bảo mật 2 lớp và khởi động chế độ gửi email ở ứng dụng kém an toàn.
$mail->Host = 'smtp.gmail.com';
0: Gửi kèm cho ai đó
Đính kèm file khi gửi mail
Đây là tiện ích khá hay khi bạn muốn đính kèm file trong quá trình gửi mail đến khách hàng. Bạn khai báo đường dẫn file vào đoạn code bên dưới.
Cấu hình nội dung email
Nguồn: https://quatangtiny.com
Danh mục: Blog