Sử dụng SwiftMailer trong ThinkPHP để gửi email qua các dịch vụ SMTP

Gửi email bằng SwiftMailer với tài khoản QQ Mail trong ThinkPHP

Để cấu hình gửi thư qua máy chủ SMTP của QQ Mail, cần bật xác thực SSL và sử dụng mã ủy quyền thay cho mật khẩu đăng nhập thông thường.

<?php
require_once APPPATH . 'libraries/swiftmailer/autoload.php';

use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;

$transport = Swift_SmtpTransport::newInstance('smtp.qq.com', 465, 'ssl')
    ->setUsername('11111@qq.com')
    ->setPassword('asfacasafa'); // Mã cấp quyền từ QQ Mail

$mailer = Swift_Mailer::newInstance($transport);

$email = Swift_Message::newInstance()
    ->setSubject('Thông báo thử nghiệm')
    ->setFrom(['11111@qq.com' => 'Niu Niu'])
    ->setTo(['22222@qq.com'])
    ->setBody('Nội dung thử nghiệm gửi email', 'text/html', 'utf-8');

$sentCount = $mailer->send($email);
var_dump($sentCount); // Số lượng email gửi thành công

Tích hợp với email doanh nghiệp (tên miền tùy chỉnh)

Đối với hệ thống email nội bộ như mail.wifire.net, thường dùng cổng 587 với bảo mật TLS. Cấu hình tương tự nhưng không cần chỉ định ssl nếu server hỗ trợ STARTTLS.

<?php
require_once APPPATH . 'libraries/swiftmailer/autoload.php';

$transport = Swift_SmtpTransport::newInstance('mail.wifire.net', 587)
    ->setUsername('cccc@wifire.net')
    ->setPassword('wvasecae23');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance()
    ->setSubject('Kiểm tra hệ thống')
    ->setFrom(['cccc@fastweb.com.cn' => 'Quản trị viên an ninh đám mây'])
    ->setTo(['3333@qq.com'])
    ->setCc(['backup@wifire.net']) // Gửi bản sao nếu cần
    ->setBody('Thân gửi, đây là email thử nghiệm.', 'text/html', 'utf-8');

$result = $mailer->send($message);
echo "Email đã gửi: " . ($result ? 'Thành công' : 'Thất bại');

Hàm hoàn chỉnh có thể tái sử dụng:

<?php
function sendEmail($to, $subject, $content, $cc = null, $bcc = null) {
    require_once APPPATH . 'libraries/swiftmailer/autoload.php';
    
    $transport = Swift_SmtpTransport::newInstance('mail.wifire.net', 587)
        ->setUsername('cccc@wifire.net')
        ->setPassword('casd2323');
    
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom(['cccc@fastweb.com.cn' => 'Quản trị viên an ninh đám mây'])
        ->setTo($to)
        ->setBody($content, 'text/html', 'utf-8');

    if (!empty($cc)) {
        $message->setCc($cc);
    }
    if (!empty($bcc)) {
        $message->setBcc($bcc);
    }

    return $mailer->send($message);
}

Sử dụng SwiftMailer với 163 Mail

163 Mail kiểm tra nghiêm ngặt nội dung và tiêu đề để chống spam. Cần đảm bảo nội dung hợp lệ, tránh từ khóa nhạy cảm. Dùng cổng 25 hoặc 465 tùy cấu hình.

<?php
require_once APPPATH . 'libraries/swiftmailer/autoload.php';

$transport = Swift_SmtpTransport::newInstance('smtp.163.com', 25)
    ->setUsername('abc@163.com')
    ->setPassword('xuxu1016');

$mailer = new Swift_Mailer($transport);

$subject = 'Buổi chiều bận việc';
$recipientList = ['asd@qq.com'];
$bodyContent = '<p>Chiều nay có việc bận, không đi làm được.</p>';

$message = new Swift_Message();
$message->setSubject($subject)
    ->setFrom(['abc@163.com' => 'Xu Xu'])
    ->setTo($recipientList)
    ->setCharset('utf-8')
    ->setBody($bodyContent, 'text/html');

$errorList = [];
$sent = $mailer->send($message, $errorList);

if (!$sent) {
    echo "Gửi thất bại. Lỗi: ";
    print_r($errorList);
} else {
    echo "Gửi thành công!";
}

Lưu ý: Nếu gặp lỗi, tham khảo mã lỗi tại trang hỗ trợ của 163: http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html

Thẻ: SwiftMailer ThinkPHP gửi email SMTP PHP mail

Đăng vào ngày 20 tháng 6 lúc 10:25