PHPMailer setFrom

PHPMailer có lẽ là thư viện mã nguồn mở PHP phổ biến nhất để gửi email với. Nó được phát hành lần đầu tiên vào năm 2001 và kể từ đó, nó đã trở thành cách gửi email theo chương trình yêu thích của nhà phát triển PHP, ngoài một số ứng dụng yêu thích khác của người hâm mộ như Swiftmailer

Trong bài viết này, chúng tôi sẽ nói về lý do tại sao bạn nên sử dụng PHPMailer thay vì hàm


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 của PHP và chúng tôi sẽ hiển thị một số mẫu mã về cách sử dụng thư viện này

PHPMailer setFrom

Nó có phải là giải pháp thay thế cho hàm From = "[email protected]"; $mail->FromName = "Full Name"; //To address and name $mail->addAddress("[email protected]", "Recepient Name"); $mail->addAddress("[email protected]"); //Recipient name is optional //Address to which recipient will reply $mail->addReplyTo("[email protected]", "Reply"); //CC and BCC $mail->addCC("[email protected]"); $mail->addBCC("[email protected]"); //Send HTML or Plain Text email $mail->isHTML(true); $mail->Subject = "Subject Text"; $mail->Body = "Mail body in HTML"; $mail->AltBody = "This is the plain text version of the email content"; try { $mail->send(); echo "Message has been sent successfully"; } catch (Exception $e) { echo "Mailer Error: " . $mail->ErrorInfo; } 5 của PHP không?

Trong hầu hết các trường hợp, nó là một giải pháp thay thế cho hàm


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 của PHP, nhưng có nhiều trường hợp khác mà hàm

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 đơn giản là không đủ linh hoạt để đạt được những gì bạn cần

Trước hết, PHPMailer cung cấp giao diện hướng đối tượng, trong khi


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 không hướng đối tượng. Các nhà phát triển PHP thường ghét tạo các chuỗi

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
2 trong khi gửi email bằng hàm

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 vì chúng yêu cầu thoát nhiều. PHPMailer làm cho điều này trở nên dễ dàng. Các nhà phát triển cũng cần viết mã bẩn (ký tự thoát, mã hóa và định dạng) để gửi tệp đính kèm và email dựa trên HTML khi sử dụng hàm

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5, trong khi PHPMailer làm cho việc này trở nên đơn giản

Ngoài ra, chức năng


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 yêu cầu máy chủ thư cục bộ gửi email, điều này không phải lúc nào cũng đơn giản để thiết lập. PHPMailer có thể sử dụng máy chủ thư không cục bộ (SMTP) nếu bạn có xác thực

lợi thế hơn nữa bao gồm

  • Nó có thể in các loại thông báo lỗi khác nhau bằng hơn 40 ngôn ngữ khi gửi email không thành công
  • Nó đã tích hợp hỗ trợ và xác thực giao thức SMTP qua SSL và TLS
  • Nó có thể gửi một phiên bản email văn bản thuần thay thế cho các ứng dụng email không phải HTML
  • Nó có một cộng đồng nhà phát triển rất tích cực giúp nó luôn an toàn và cập nhật

PHPMailer cũng được sử dụng bởi các hệ thống quản lý nội dung PHP phổ biến như WordPress, Drupal và Joomla

Cài đặt PHPMailer

Bạn có thể cài đặt PHPMailer bằng Composer

composer require phpmailer/phpmailer

Gửi email từ máy chủ web cục bộ bằng PHPMailer

Đây là ví dụ đơn giản nhất về việc gửi email từ máy chủ web cục bộ bằng PHPMailer


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

Mã và nhận xét phải đủ rõ ràng để giải thích mọi thứ đang diễn ra

Gửi Email có tệp đính kèm

Đây là một ví dụ về cách gửi email có tệp đính kèm bằng PHPMailer


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

Ở đây, chúng tôi đang đính kèm hai tệp —


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
6, nằm trong cùng thư mục với tập lệnh và

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
7, nằm trong thư mục

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
8 của thư mục tập lệnh

Để thêm tệp đính kèm vào email, chúng ta chỉ cần gọi hàm


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
9 của đối tượng PHPMailer bằng cách truyền đường dẫn tệp làm đối số. Để đính kèm nhiều tệp, chúng ta cần gọi nó nhiều lần

Xử lý sự cố

Trong hai ví dụ của chúng tôi, chúng tôi đã sử dụng lớp Ngoại lệ của PHPMailer để gỡ lỗi, do đó, bất kỳ lỗi nào được đưa ra sẽ giúp chúng tôi gỡ lỗi mọi sự cố có thể xảy ra. Chúng tôi cũng đã thêm đối số


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
0 vào hàm tạo PHPMailer, để xuất ra các ngoại lệ cấp cao hơn, mang tính mô tả hơn

Tùy thuộc vào hệ thống chúng tôi sử dụng, có lẽ lỗi thường gặp nhất mà chúng tôi thấy sẽ liên quan đến việc chạy hàm


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 trong nền

Lỗi người gửi thư. Không thể khởi tạo chức năng thư

Nếu chúng tôi cần thêm chi tiết về lỗi, chúng tôi cũng có thể thêm nội dung như thế này vào mệnh đề catch

print_r(error_get_last());

Thông thường, sự cố với chức năng thư sẽ liên quan đến thiết lập máy chủ thư bị thiếu, trong trường hợp đó, hàm


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
2 sẽ trả về kết quả như thế này

composer require phpmailer/phpmailer
1

Đây là sự cố mà chúng tôi có thể gặp phải thường xuyên nhất và chúng tôi có thể giải quyết vấn đề này một cách dễ dàng bằng cách sử dụng SMTP

Hiển thị thông báo lỗi cục bộ


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
3 có thể trả về thông báo lỗi bằng 43 ngôn ngữ khác nhau

Để hiển thị thông báo lỗi bằng một ngôn ngữ khác, hãy sao chép thư mục


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
4 từ mã nguồn của PHPMailer vào thư mục dự án

Ví dụ, để trả về các thông báo lỗi bằng tiếng Nga, hãy đặt đối tượng PHPMailer thành ngôn ngữ tiếng Nga bằng lệnh gọi phương thức bên dưới

composer require phpmailer/phpmailer
4

Bạn cũng có thể thêm các tệp ngôn ngữ của riêng mình vào thư mục


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
4

Sử dụng SMTP

Bạn có thể sử dụng máy chủ thư của một máy chủ khác để gửi email, nhưng để làm được điều này, trước tiên bạn cần phải có xác thực. Ví dụ: để gửi email từ máy chủ thư của Gmail, bạn cần có tài khoản Gmail

SMTP là một giao thức được sử dụng bởi các ứng dụng thư khách để gửi yêu cầu gửi email đến máy chủ thư. Khi máy chủ thư xác minh email, nó sẽ gửi nó đến máy chủ thư đích

Dưới đây là ví dụ về cách gửi email từ máy chủ thư của Gmail từ miền của bạn. Bạn không cần máy chủ thư cục bộ để chạy mã. Chúng tôi sẽ sử dụng giao thức SMTP

composer require phpmailer/phpmailer
6

Gmail yêu cầu mã hóa TLS qua SMTP, vì vậy chúng tôi đặt nó cho phù hợp. Trước khi gửi qua SMTP, bạn cần tìm hiểu tên máy chủ, số cổng, loại mã hóa nếu được yêu cầu và nếu cần xác thực, bạn cũng cần có tên người dùng và mật khẩu. Lưu ý rằng việc bật xác thực hai yếu tố trên Gmail sẽ không cho phép bạn sử dụng SMTP của họ với tên người dùng/mật khẩu. Thay vào đó, cấu hình bổ sung sẽ được yêu cầu

Một lợi thế lớn trong việc sử dụng SMTP từ xa so với thư cục bộ là nếu bạn sử dụng hàm


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");

//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
5 của PHP để gửi email với miền địa chỉ

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
7 được đặt thành bất kỳ thứ gì khác ngoài tên miền cục bộ (tên của máy chủ), thì bộ lọc tấn công của máy chủ email của người nhận sẽ . Ví dụ: nếu bạn gửi email từ một máy chủ có tên máy chủ thực tế là

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
8 với địa chỉ

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
7 là
print_r(error_get_last());
0 đến
print_r(error_get_last());
1, thì các máy chủ của Yahoo sẽ đánh dấu email đó là thư rác hoặc hiển thị thông báo cho người dùng không tin vào email đó vì nguồn gốc của thư là

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->addAddress("[email protected]", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
8 . Mặc dù bạn sở hữu
print_r(error_get_last());
0 nhưng không có cách nào để Yahoo tìm ra điều đó

Lấy Email bằng POP3

PHPMailer cũng cho phép xác minh POP trước SMTP để gửi email. Nói cách khác, bạn có thể xác thực bằng POP và gửi email bằng SMTP. Rất tiếc, PHPMailer không hỗ trợ truy xuất email từ máy chủ thư bằng giao thức POP3. Nó bị giới hạn chỉ gửi email

Phần kết luận

Nếu bạn là nhà phát triển PHP, có rất ít cơ hội để tránh phải gửi email theo chương trình. Mặc dù bạn có thể chọn các dịch vụ của bên thứ ba như Mandrill hoặc SendGrid, nhưng đôi khi đó không phải là một tùy chọn và thậm chí còn ít sử dụng thư viện gửi email của riêng bạn hơn. Đó là nơi PHPMailer và các lựa chọn thay thế của nó (Zend Mail, Swift Mailer, v.v.) xuất hiện

Bạn có thể tìm hiểu về các API của thư viện này trong kho lưu trữ wiki hoặc trong tài liệu chính thức

Bạn đang bị sa lầy với các phụ thuộc thư viện PHP?

Chia sẻ bài viết này

PHPMailer setFrom

Narayan Prusty

Narayan là một phi hành gia web. Ông là người sáng lập QNimate. Anh ấy thích dạy học. Anh ấy thích chia sẻ ý tưởng. Khi không viết mã, anh ấy thích chơi bóng đá. Bạn sẽ thường tìm thấy anh ấy tại các lớp học QScutter

    PHPMailer setFrom

    Tonino Jankov

    Tonino là một nhà phát triển web và tư vấn CNTT, người đã nghiên cứu mã nguồn mở trong hơn một thập kỷ. Anh ấy cũng là một người đam mê tiền điện tử, người hâm mộ Linux và người theo chủ nghĩa tự do ôn hòa

    Làm cách nào để đặt PHPMailer?

    Thêm PHPMailer theo cách thủ công (Windows và macOS) . Giải nén tệp đã cài đặt mà bạn muốn cài đặt PHPMailer . Khi bạn bấm đúp vào tệp đã cài đặt, bạn sẽ được nhắc về vị trí tệp đã giải nén. PHPMailer đã được cài đặt và sẵn sàng sử dụng tập lệnh PHP của bạn.

    Làm cách nào để thay đổi từ địa chỉ trong PHPMailer?

    Dòng liên quan trong câu trả lời của Theolodis là. $mail->SetFrom('tên@miền của bạn. com', 'First Last'); Không cần sử dụng AddReplyTo() đây là một cái gì đó hoàn toàn khác. Bạn chỉ cần đặt địa chỉ từ (và tên tùy chọn) bằng cách sử dụng SetFrom().

    Làm cách nào để tích hợp PHPMailer trong php?

    Tải trình tải tự động của nhà soạn nhạc. yêu cầu 'nhà cung cấp/tự động tải. php'; . .
    isHTML(). Nếu được thông qua đúng, hãy đặt định dạng email thành HTML
    Vấn đề. Đặt chủ đề của Thư
    Thân thể. Đặt nội dung của Thư
    AltBody. Nội dung thay thế trong trường hợp ứng dụng e-mail không hỗ trợ HTML

    PHPMailer có sử dụng SMTP không?