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à password
.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 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.
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:
1 2 3 4 5 6 7 8 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?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"; ?> |
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.
WEB FAQ > Email FAQ > Hỏi đáp Email
web faq | hỏi đáp web | hoi dap web | website faq | hỏi đáp website | hoi dap website | hướng dẫn sử dụng web | huong dan su dung web | huong dan web | hoi dap email