Gửi email localhost php

"../storage/PHPMailer-master/src/PHPMailer.php"4 "../storage/PHPMailer-master/src/PHPMailer.php"58"../storage/PHPMailer-master/src/PHPMailer.php"59

$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;
}
17"../storage/PHPMailer-master/src/PHPMailer.php"61
$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;
}
17"../storage/PHPMailer-master/src/PHPMailer.php"63
$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;
}
17"../storage/PHPMailer-master/src/PHPMailer.php"65
$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;
}
17"../storage/PHPMailer-master/src/PHPMailer.php"67"../storage/PHPMailer-master/src/PHPMailer.php"5"../storage/PHPMailer-master/src/PHPMailer.php"69
$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;
}
17"../storage/PHPMailer-master/src/PHPMailer.php"71
$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;
}
17"../storage/PHPMailer-master/src/PHPMailer.php"73
$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;
}
19

Điều này rất hữu ích, bạn có thể thực hiện điều này trên các trang web dự án của mình. Nhưng tất nhiên bạn cần có 1 máy chủ để gửi email - bạn phải trả tiền để mua nó đấy. Tuy nhiên về phần code PHP thì cực kỳ đơn giản

In the content section này bao gồm 3 tệp

- Chỉ mục. php
- SMTPconfig. php // Cấu hình máy chủ SMTP
- SMTPClass. php // Lớp gửi thư SMTP

Tệp đầu tiên, đó là tệp cấu hình các thông tin liên quan đến việc đăng nhập

SMTPcấu hình. php Bạn phải thay đổi chi tiết máy chủ SMTP

//Địa chỉ máy chủ
$SmtpServer="127. 0. 0. 1"; // máy chủ gửi email của bạn
$SmtpPort="25"; //mặc định  // cổng
$SmtpUser="username";

Ví dụ

//Địa chỉ máy chủ
$SmtpServer="112. 78. 8. 20"; // địa chỉ ip máy chủ chia sẻ lưu trữ
$SmtpPort="2095"; // cổng đăng nhập vào hệ thống thư
$SmtpUser="[
$SmtpPass="password";
?>

Tuy nhiên, mình không chắc chắn sẽ thực hiện thành công với dịch vụ chia sẻ dịch vụ lưu trữ. Tốt nhất là đầu tư luôn 1 con server chuyên gửi email

Các

sendmail giả, localhost, mail, php, gửi thư, sendmail, wamp, xampp

Để sử dụng hàm mail của php trong localhost, bạn cần cấu hình 1 vài thứ để nó có thể hoạt động được. Có nhiều cách để gửi thư từ máy chủ cục bộ như sử dụng máy chủ thư. hMailServer, Mecury Mail,…

Hôm nay mình sẽ hướng dẫn 1 cách đơn giản hơn là sử dụng gửi mail giả mạo với máy chủ Wamp hoặc Xampp

Nếu bạn dùng Xampp thì bỏ qua bước 1 và 2 vì Xampp nó tích hợp sẵn cái này rồi

  1. Tải nguồn ở đây
  2. Copy thư mục sendmail vào C. /wamp/ hoặc C. /xampp

  3. Open file C. /wamp/sendmail/sendmail. ini hoặc C. /xampp/gửi thư/gửi thư. ini và cấu hình như sau

  • smtp_server=smtp. gmail. com
  • smtp_port=465
  • smtp_ssl=ssl
  • default_domain=localhost
  • error_logfile=lỗi. đăng nhập
  • debug_logfile=gỡ lỗi. đăng nhập
  • auth_username=[your_gmail_account_username]@gmail. com
  • auth_password=[your_gmail_account_password]

4. Open php. ini, bỏ dấu ; . dll, tiếp tục tìm “sendmail_path” được gán.

6
7

*** Nếu sử dụng Xampp thì bạn chỉ cần bỏ dấu ; .

5. Open httpd. bỏ dấu # ở dòng LoadModule ssl_module modules/mod_ssl. vì thế

xong. You try to use mail function to test please see. Nếu không được, bạn vào …/sendmail/error. log cóp dán cho mình xem nhé

Bạn có một trang web bán hàng trực tuyến, bạn muốn nhận được thông báo khi có đơn hàng mới? . Bạn có thể sử dụng hàm mail mặc định của PHP để thực hiện điều này nhưng thường hay bị lỗi, và trong bài ngày hôm nay mình muốn giới thiệu tới các bạn cách gửi thư trong php bằng cách sử dụng PHPMailer. Có thể nói đây là một trong những thư viện cơ bản nhưng chắc chắn là ai cũng biết đến nó và bài này mình muốn giới thiệu đến các bạn đang gặp khó khăn trong vấn đề gửi mail trong php nên các bạn chém tay ạ.

Bài viết này đã được đăng tại freetuts. net , không được sao chép dưới mọi hình thức.

1. Tại sao phải sử dụng PHPMailer?

Mặc dù trong php đã có sẵn chức năng gửi thư rồi nhưng thông thường thì khi thư sẽ rất khó phát nổ, thư của bạn có thể nằm trong thư mục thư rác hoặc máy chủ của bạn đã bị chặn gửi thư. Lúc này bạn 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ợ gửi thư. Và ở đây tôi chọn PHPMailer để thực hiện công việc này. Ngoài ra còn có lê 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 sử 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 thư
  • Gửi mail nhanh mail ít lỗi được chuyển vào hộp thư đến của anh ấy
  • Có thể thêm cc,bcc,file đính kèm
  • Có thể tương tác hai chiều tức thời là khi khách hàng trả lời thư, bạn sẽ nhận được thư đã nhận 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 sử dụng đi và cảm nhận

Bài viết này đã được đăng tại [free tuts. mạng lưới]

2. Hướng dẫn sử dụng phiên bản PHPMailer 6x

Trước tiên bạn cần tải xuống thư viện PHPMailer 6. 05 tại đây

Nhập thư viện

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

 

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ực hiện gửi thư

 

$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

3. Hướng dẫn sử dụng PHPMailer Phiên bản 5x

Trước khi bạn muốn sử dụng nó, bạn cần phải tải mã của nó về. Bạn có thể tải nó về tại đây

Sau khi tải về bạn giải nén nó ra bạn sẽ nhận được một số tệp nhưng bạn chỉ cần sử dụng hai tệp đó là

8 và 
9 còn những tệp khác bạn có thể tìm hiểu thêm trong đó có một số mẫu ví dụ ứng dụng hay cũng được . Bây giờ bạn hãy sao chép tệp đó qua dự án của mình

Sau khi đã có 2 tệp trên chúng ta cùng tạo 1 tệp có tên

$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;
}
0 tất nhiên bạn đặt cũng không được gì

And content of file as after

 

 

Chỉ đơn giản là thế thôi, rất nhàn nhã phải không nào, bạn thử chạy tệp

$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;
}
0 và mở hộp thư đến ra trọng lượng xem có nhận được thư không nhé

Hướng dẫn tạo hàm gửi thư bằng PHPMailer

Như ở phần trên bạn đã gửi email thành công nhưng giả sử dụng trang web của bạn bạn muốn gửi thư ở nhiều nơi, ví dụ như khi khách hàng liên hệ bạn cần gửi thư đến quản trị viên để thông báo. Khi có đơn hàng mới, bạn cần gửi thư xác nhận đơn hàng và thông báo tới quản trị viên. Không lẽ bạn cứ phải ôm theo một mã mới như thế nào để ném ở mọi tệp rõ ràng là không thu gọn gàn chút nào phải không nào. Và ở phần này mình sẽ hướng dẫn các bạn để họ vào một hàm và trong hàm này hơi khác phần trên một chút là mình có bổ sung thêm tính năng địa chỉ cc. Thực tế thì dựa vào phần bạn hoàn toàn có thể tự tạo hàm cho riêng mình nhưng ở đây mình tạo mẫu cho các bạn chưa chán lắm về hàm có thể áp dụng. Bạn hãy tạo một file functions. php to write các hàm vào đấy. Và ở đoạn mã sau tôi đã tạo sẵn hai hàm một là hàm sendMail để gửi thư bình thường không có tệp đính kèm và hai là hàm 

$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;
}
0 để gửi thư có đính kèm theo tệp đính kèm. Tất nhiên bạn có thể mở rộng chúng thành một hàm cũng được nhưng mình muốn tách làm hai hàm riêng biệt để chọn rõ ràng. Nội dung file functions. php as after.  

 

Trong chức năng trên mình có để cố định phần AddReplyTo vì theo ý kiến ​​cá nhân của mình chắc chắn phần này thường cố định tuy nhiên nếu hệ thống của bạn bao gồm nhiều bộ phận thì có thể nó sẽ khác bạn từ điều chỉnh cho phù hợp

 

Bây giờ bạn quay lại file

$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;
}
0 viết code như sau để test nhé

 

 

Đối với hàm 

$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;
}
0 bạn sửa lại dòng sau.

 

	$mail = sendMailAttachment[$title, $content, $nTo, $mTo,$diachicc='','filedinhkem.docx','Day la file dinh kem'];

 

Bạn hãy cấu hình các thông số phù hợp sau đó chạy tệp

$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;
}
0 để kiểm tra

4. Cấu hình tài khoản gmail để gửi mail nhận được

Hiện tại google đã bổ sung một số chức năng bảo mật dành cho dịch vụ bên ngoài, tức là các dịch vụ gửi thư thông qua smtp của google, vì vậy bạn phải thực hiện một số bước sau để có thể gửi thư được.

Không sử dụng đăng nhập 2 bước

Đăng nhập 2 bước là chức năng Mỗi khi bạn đăng nhập thì google sẽ gửi mã mã đến sdt của bạn, sau đó bạn nhập mã mã vào thì mới đăng nhập được. Vì bạn đang gửi tự động nên phải tắt chức năng này nhé

Để tắt thì bạn vào liên kết này nhé, nếu bạn chưa bật thì có thẻ bỏ qua

Bật BẬT cho phép các ứng dụng kém an toàn hơn

Bạn vào link này để chuyển sang chế độ ON nhé. Thường khi hoàn thành bước này là bạn có thể gửi thư tại máy chủ cục bộ, nhưng khi tải lên máy chủ lưu trữ thì không sử dụng được, lúc này bạn phải thực hiện bước thứ 3 dưới đây

Bật Hiển thị hình ảnh xác thực mở khóa

Bạn vào link này và click continue để cho phép gửi mail ở bên thứ 3 nhé

Sau khi hoàn thành 3 bước trên là bạn đã gửi được mail rồi đấy. Trường hợp vẫn chưa gửi được thì bạn phải xem lại hosting của bạn có hỗ trợ gửi mail hay không, và có một số hosting bắt buộc gửi mail phải theo tên miền nên không sử dụng được Gmail

5. Kết luận

Ở trên mình và các bạn vừa tìm hiểu về cách gửi mail trong php thông qua thư viện PHPMailer như đã nói ở trên, đây là một thư viện khá cơ bản và rất nhiều người biết đến rồi nhưng mình vẫn muốn giới thiệu lại cho các bạn . Bài viết chỉ ở trạng thái khai báo các bạn có thể vận chuyển sao cho linh hoạt hoạt động để nó trở nên hữu dụng. Và kỹ năng lập trình của mình còn hạn chế, cũng như kiến ​​thức có hạn nên có thể trong bài viết có nhiều chỗ sai, mong nhận được sự góp ý từ phía các bạn để mình củng cố thêm. And also mong các bạn thông cảm.  

Các bạn có gì chưa hiểu hay thắc mắc có thể đặt câu hỏi tại đây hoặc hỏi trong group ĐĐ để được hỗ trợ tốt nhất. Không nên hỏi nhưng câu hỏi khó tránh khỏi tình trạng tác động giá rối không biết câu trả lời. Mình rất khuyến khích các mem nữ hỏi những kiểu như có người yêu chưa?

Chủ Đề