HƯỚNG DẪN TẠO CONTACT FORM PHP, YAHOO MAIL SMTP

Trong bài viết này, mình sẽ hướng dẫn các bạn code php cho trang liên hệ của website bằng php, sau khi form submit thì nội dung từ form sẽ gửi về email của bạn. Mình sẽ sử dụng smtp mail yahoo để gửi, trước tiên hãy tạo email yahoo nếu chưa có và tạo mật khẩu cho bên thứ ba trong phần cài đặt tài khoản, sử dụng mật khẩu này thay thế cho mật khẩu của bạn và điền vào trong file index.php

(lưu ý: Máy tính của bạn đã cài đặt Xampp)

<?php
class gm{
	function GuiMail($to, $from, $from_name, $subject, $body, $username, $password, &$error){
	   $error = "";
	   require_once "class.phpmailer.php";      
	   require_once "class.smtp.php";      
	   try {
            $mail = new PHPMailer();  
            $mail -> IsSMTP(); 
            $mail -> SMTPDebug = 0;  //  1=errors and messages, 2=messages only
            $mail -> SMTPAuth = true;  
            $mail -> SMTPSecure = 'ssl'; 
            $mail -> Host = 'smtp.mail.yahoo.com';
            $mail -> Port = 465; 
            $mail -> Username = $username;
            $mail -> Password = $password;           
            $mail -> SetFrom($from, $from_name);
            $mail -> Subject = $subject;
            $mail -> MsgHTML($body);// noi dung chinh cua mail
            $mail -> AddAddress($to);
            $mail -> CharSet="utf-8";
            $mail -> IsHTML(true);
            if( !$mail->Send() ) {
              echo $error = 'Loi:'.$mail->ErrorInfo;
            } else { 
                $error = '';
            }
	   } 
	   catch (phpmailerException $e) { echo "<pre>".$e->errorMessage(); }    
	}
}
?>