Trong PHP, chức năng gửi mail thực sự rất dễ dàng. Trong bài này, chúng tôi sẽ hướng dẫn các bạn tạo một trang send mail đơn giản. Ngoài ra các bạn có thể phát triển code trong bài này thành những chức năng send mail phức tạp hơn, đa năng hơn.
Để send mail trong PHP, chúng ta cần sử dụng tới hàm mail().
Cú pháp:
Gửi mail có sử dụng Form.
Để send mail trong PHP, chúng ta cần sử dụng tới hàm mail().
Cú pháp:
- Code:
mail(to,subject,message,headers,parameters)
- Code:
<?php
$to = " [You must be registered and logged in to see this link.]@gmail.com Địa chỉ email này đã được bảo vệ từ spam bots, bạn cần kích hoạt Javascript để xem nó. ";
$subject = "Test mail";
$message = "Email này được gửi từ website: http://hotrolaptrinh.com";
$from = " [You must be registered and logged in to see this link.]@hotrolaptrinh.com Địa chỉ email này đã được bảo vệ từ spam bots, bạn cần kích hoạt Javascript để xem nó. ";
$headers = "";
mail($to,$subject,$message,$headers);
echo "Email đã được gửi đi thành công.";
?>
Gửi mail có sử dụng Form.
- Code:
<?php
if (isset($_POST['sendmail']) && $_POST['sendmail']=="send"){
//send email
$email = $_POST['email'] ;
$subject = $_POST['subject'] ;
$message = $_POST['message'] ;
$to=" [You must be registered and logged in to see this link.]@gmail.com Địa chỉ email này đã được bảo vệ từ spam bots, bạn cần kích hoạt Javascript để xem nó. ";
mail( $to, "Subject: $subject",$message, "From: $email" );
echo "Gửi thành công";
}
?>
<form method="post">
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type="submit" name="sendmail" value="send" />
</form>