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

Trang chủ Blog LẬP TRÌNH PHP HƯỚNG DẪN TẠO CONTACT FORM PHP, YAHOO MAIL SMTP
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

Bước 1: Bạn hãy tạo 1 folder: testmail trong htdocs

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

Bước 2: tạo file index.php và viết đoạn code phía dưới vào.

<?php
require_once "contact.php";
$m = new gm;

if( isset($_POST['submit']) ) {
	$hoten = trim( strip_tags( $_POST['hoten'] ) );
	$email = trim( strip_tags( $_POST['email'] ));
	$tieude = trim( strip_tags( $_POST['tieude'] ) );
    $thongdiep = trim( strip_tags( $_POST['thongdiep'] ) );

    //điền email nhận tại đây
    $to = "huynhthaihung2765@gmail.com";
	$tieudethu = "Liên hệ từ $hoten";			
    $noidungthu = "
    <strong>Họ tên: </strong> $hoten<br/>
	<strong>Email: </strong> $email<br/>
	<strong>Tiêu đề: </strong> $tieude<br/>
	<strong>Thông điệp: </strong> $thongdiep<br/>
    <i>Thư được gửi từ liên hệ của website https://huynhthaihung.com</i>";
    
    //dùng mail test, đừng dùng mail chính thức
    $from = "your-email@yahoo.com";

    //pass email yahoo
    $p = "password";
	$m -> GuiMail($to, $from, $tennguoigui="Huynh Thai Hung", $tieudethu, $noidungthu, $from, $p, $error);
	if( $error != '' ) {
        $loi['guimail'] = "gửi mail không thành công";
    }else {
        $thanhcong['guimail'] = "gửi mail thành công";
    }
}
?>

<!DOCTYPE html>
<head>
<title>huynhthaihung.com | mail php</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
	<div class="container">
	<div class="heading">
		<h1>LIÊN HỆ</h1>
		<hr>
	</div>
		<h3>Vui lòng gửi cho chúng tôi suy nghĩ của bạn.</h3>
		
        <?php echo isset($loi['guimail']) ? "<div class='loi'>".$loi['guimail']."</div>" : '' ?>
		<?php echo isset($thanhcong['guimail']) ? "<div class='thanhcong'>".$thanhcong['guimail']."</div>" : '' ?>	
		
        <div class="row">  
			<div class="col-xs-12 col-sm-6 col-md-6">
				<form id="form" action="" method="POST">         
					<input type="text" id="hoten" name="hoten"  class="form-control" placeholder="Tên của bạn" >
					<br>                
					<input type="email" name="email" size="40" class="form-control" placeholder="Email">
					<br>                
					<input type="text" name="tieude" size="40" class="form-control" placeholder="Tiêu đề">
					<br>                             
					<textarea name="thongdiep" cols="40" rows="5" class="form-control" placeholder="Thông điệp"></textarea>        
					<br>
					<input type="submit" name="submit" value="submit" class="btn btn-primary">
				</form>
			</div>     
		</div>
	</div>
</body>
</html>

Tiếp Theo, Bước 3: tạo tiếp file contact.php và code phần còn lại vào.

<?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(); }    
	}
}
?>

Lúc này vẫn chưa gửi được đâu nhé!
Tải 2 file này về và để vào thư mục htdocs/testmail, Link tải
Tổng cộng 4 file như hình

4 file của demo

Vào brower: http://localhost/testmail/

Kết quả:

giao diện liên hệ

Khi submit form, bạn sẽ nhận được mail liên hệ.

Kết quả nhận mail:

gửi mail thành công

Chúc bạn thành công!