CÔNG TY TNHH CÔNG NGHỆ & TƯ VẤN Điểm Nhấn

Câu hỏi thường gặp

Sử dụng phpMailer để gởi Email mã nguồn PHP

o hàm mail() của PHP bị khóa vì lý do bảo mật, nếu mã nguồn PHP của bạn gặp phải vấn đề sử dụng hàm này, bạn nên chuyển sang dùng PHPMailer để hoạt động được và không lệ thuộc hàm mail() nữa. Tham khảo: Website PHP gặp lỗi khi dùng hàm mail() để gởi Email.

Khi gởi email bằng PHPMailer, bạn sẽ gởi email có địa chỉ người gởi là một địa chỉ có thực và là địa chỉ của bạn. Nếu bạn chưa có tài khoản email dành cho việc này, bạn cần vào Control Panel để tạo một hộp thư. Trong ví dụ này, bạn sẽ dùng địa chỉ là[email protected] và mật khẩu là passw0rd.

Download mã nguồn PHPMailer

Đầu tiên, bạn cần tải mã nguồn PHPMailer tại địa chỉ: https://github.com/Synchro/PHPMailer (nhấn vào nút Download ZIP)

Sau khi tải về, bạn giải nén vào thư mục public_html. Bạn sẽ nhìn thấy thư mục này có dạng: public_html/PHPMailer-master. Tiếp theo, bạn điều chỉnh mã nguồn website để sử dụng. 

Tạo một Form để nhập nội dung Email 

Bạn tạo một trang đặt tên là email.php với nội dung như bên dưới:

 
<form method="post" action="email.php">
  Email: <input name="email" id="email" type="text" />
 
  Message:
  <textarea name="message" id="message" rows="15" cols="40"></textarea>
 
  <input type="submit" value="Submit" />
</form>

 

Bổ sung PHPMailer vào mã nguồn

 

 
<?php
 
// $email and $message are the data that is being
// posted to this page from our html contact form
$email $_REQUEST['email'] ;
$message $_REQUEST['message'] ;
 
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer-master
require("PHPMailer-master/class.PHPMailer.php");
 
$mail new PHPMailer();
 
// set mailer to use SMTP
$mail->IsSMTP();
 
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server
 
$mail->SMTPAuth = true;     // turn on SMTP authentication
 
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// pass: password
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "password"// SMTP password
 
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
 
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("[email protected]""Brad Markle");
 
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
 
$mail->Subject = "You have received feedback from your website!";
 
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;
 
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " $mail->ErrorInfo;
   exit;
}
 
echo "Message has been sent";
?>

 

Phpmailer gởi mail SMTP bằng Gmail/Google Apps

Quý khách tạo 1 filen send.php với nội dung sau

<?
include "class.phpmailer.php"; 
include "class.smtp.php"; 
 
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Username = "[email protected]"; // your SMTP username or your gmail username
$mail->Password = "passwordhere"; // your SMTP password or your gmail password
$from = "[email protected]"; // Reply to this email
$to="[email protected]"; // Recipients email ID
$name="Ky Thuat PA"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Your From Name"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Ky Thuat PA");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Gui mail script from www.pavietnam.vn";
$mail->Body = "<b>Mail nay duoc gui bang SMTP Gmail dung phpmailer class. - <a href='http://www.pavietnam.vn'>www.pavietnam.vn</a></b>"; //HTML Body
$mail->AltBody = "Mail nay duoc gui bang SMTP Gmail dung phpmailer class. - www.pavietnam.vn"; //Text Body
//$mail->SMTPDebug = 2;
if(!$mail->Send())
{
    echo "<h1>Loi khi goi mail: " . $mail->ErrorInfo . '</h1>';
}
else
{
    echo "<h1>Send mail thanh cong</h1>";
}
?>

Để gửi tài khoản bằng gmail chúng ta cần làm những điều sau nếu bạn dùng localhost
-    Extension php_openssl cầi được cài đặt
-    Gmail sử dụng SMTPSecure (ssl) để gửi email
-    Khi sử dụng ssl của gmail chúng ta phải sử dụng port 465


Lưu ý cuối cùng: Đa số Web Hosting và Email Server không cho phép bạn gởi email có địa chỉ người gởi (FROM) khác với tên miền bạn đang dùng. Ví dụ nếu bạn đã tạo account dùng để gởi email là [email protected] nhưng lại để địa chỉ FROM là[email protected] là không hợp lệ. Máy chủ sẽ từ chối và báo lỗi.

 



Đánh giá?

Ghi nhớ Ghi nhớ

In In

Xem thêm

Powered by WHMCompleteSolution



Truy cập nhanh

Đăng nhập

Email

Mật mã

Ghi nhớ thông tin

Tìm kiếm