Bạn Tìm Gì Hôm Nay ...?
Tất cả đều có chỉ trong 1 nốt nhạc !
Nếu cần hỗ trợ chi tiết gọi 1900 9477
Do 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.
contact@domain.com
và mật khẩu là passw0rd
.
Nội dung
Đầu tiên, bạn cần tải mã nguồn PHPMailer tại địa chỉ: https://github.com/PHPMailer/PHPMailer (nhấn vào nút 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_5.2.0
. Tiếp theo, bạn điều chỉnh mã nguồn website để sử dụng.
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 > |
<?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_5.2.0 require ( "PHPMailer_5.2.0/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:
// email: contact@domain.com
// pass: password
$mail ->Username = "contact@domain.com" ; // 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( "webmaster-abcd@gmail.com" , "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 = “your_email@gmail.com”; // your SMTP username or your gmail username
$mail->Password = “passwordhere”; // your SMTP password or your gmail password
$from = “your_email@gmail.com”; // Reply to this email
$to=”email_nguoinhan@domain.com”; // 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à contact@domain.com
nhưng lại để địa chỉ FROM là visitor@yahoo.com
là không hợp lệ. Máy chủ sẽ từ chối và báo lỗi.