Tự học PHP | Làm việc với mail trong PHP » Cafedev.vn

Hàm mail() trong PHP được sử dụng để gửi email bằng PHP. Bạn có thể gửi tin nhắn văn bản, tin nhắn html và tệp đính kèm với tin nhắn bằng cách sử dụng hàm mail() PHP.

1. Hàm mail() trong PHP

Cú pháp

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )  

$to : chỉ định người nhận hoặc người nhận thư. Người nhận phải được chỉ định một trong các hình thức sau.

$subject : đại diện cho chủ đề của thư.

$message : đại diện cho thông điệp của thư sẽ được gửi đi.

Lưu ý: Mỗi dòng của thư phải được phân tách bằng CRLF (\ r \ n) và các dòng không được lớn hơn 70 ký tự.

$added_headers (tùy chọn) : chỉ định các tiêu đề bổ sung như Từ, CC, BCC, v.v. Các tiêu đề bổ sung bổ sung cũng phải được phân tách bằng CRLF (\ r \ n).

2. Ví dụ về mail trong PHP

Tệp: mailer.php

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Group: https://www.facebook.com/groups/cafedev.vn/
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
Pinterest: https://www.pinterest.com/cafedevvn/
YouTube: https://www.youtube.com/channel/UCE7zpY_SlHGEgo67pHxqIoA/
*/

<?php  
   ini_set("sendmail_from", "[email protected]");  
   $to = "[email protected]";//change receiver address  
   $subject = "This is subject";  
   $message = "This is simple text message.";  
   $header = "From:[email protected] \r\n";  
  
   $result = mail ($to,$subject,$message,$header);  
  
   if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?> 

Nếu bạn chạy code này trên máy chủ trực tiếp, nó sẽ gửi một email đến người nhận được chỉ định.

3. Thư PHP: Gửi tin nhắn HTML

Để gửi tin nhắn HTML, bạn cần phải đề cập đến Content-type text / html trong tiêu đề tin nhắn.

/*
Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
@author cafedevn
Contact: [email protected]
Fanpage: https://www.facebook.com/cafedevn
Group: https://www.facebook.com/groups/cafedev.vn/
Instagram: https://instagram.com/cafedevn
Twitter: https://twitter.com/CafedeVn
Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
Pinterest: https://www.pinterest.com/cafedevvn/
YouTube: https://www.youtube.com/channel/UCE7zpY_SlHGEgo67pHxqIoA/
*/

<?php  
   $to = "[email protected]";//change receiver address  
   $subject = "This is subject";  
   $message = "<h1>This is HTML heading</h1>";  
  
   $header = "From:[email protected] \r\n";  
   $header .= "MIME-Version: 1.0 \r\n";  
   $header .= "Content-type: text/html;charset=UTF-8 \r\n";  
  
   $result = mail ($to,$subject,$message,$header);  
  
   if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>  

4. Thư trong PHP: Gửi thư với file đính kèm

Để gửi thư có file đính kèm, bạn cần đề cập đến nhiều thông tin tiêu đề được sử dụng trong ví dụ dưới đây.

<?php  
  $to = "[email protected]";  
  $subject = "This is subject";  
  $message = "This is a text message.";  
  # Open a file  
  $file = fopen("/tmp/test.txt", "r" );//change your file location  
  if( $file == false )  
  {  
     echo "Error in opening file";  
     exit();  
  }  
  # Read the file into a variable  
  $size = filesize("/tmp/test.txt");  
  $content = fread( $file, $size);  
  
  # encode the data for safe transit  
  # and insert \r\n after every 76 chars.  
  $encoded_content = chunk_split( base64_encode($content));  
    
  # Get a random 32 bit number using time() as seed.  
  $num = md5( time() );  
  
  # Define the main headers.  
  $header = "From:[email protected]\r\n";  
  $header .= "MIME-Version: 1.0\r\n";  
  $header .= "Content-Type: multipart/mixed; ";  
  $header .= "boundary=$num\r\n";  
  $header .= "--$num\r\n";  
  
  # Define the message section  
  $header .= "Content-Type: text/plain\r\n";  
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";  
  $header .= "$message\r\n";  
  $header .= "--$num\r\n";  
  
  # Define the attachment section  
  $header .= "Content-Type:  multipart/mixed; ";  
  $header .= "name=\"test.txt\"\r\n";  
  $header .= "Content-Transfer-Encoding:base64\r\n";  
  $header .= "Content-Disposition:attachment; ";  
  $header .= "filename=\"test.txt\"\r\n\n";  
  $header .= "$encoded_content\r\n";  
  $header .= "--$num--";  
  
  # Send email now  
  $result = mail ( $to, $subject, "", $header );  
  if( $result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>

Cài ứng dụng cafedev để dễ dàng cập nhật tin và học lập trình mọi lúc mọi nơi tại đây.

Tài liệu từ cafedev:

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!