PHP 邮件

PHP 邮件

PHP的mail()函数用于发送电子邮件。您可以使用PHP的mail()函数发送纯文本消息、HTML消息和带有附件的消息。

PHP的mail()函数

语法

bool mail ( string to , stringsubject , string message [, stringadditional_headers [, string $additional_parameters ]] )

$to :指定邮件的收件人或收件人。收件人必须指定以下形式之一。

$subject :表示邮件的主题。

$message :表示要发送的邮件内容。

注意:消息的每一行都应使用CRLF(\r\n)分隔,行的长度不应超过70个字符。

$additional_headers (可选) :指定其他邮件头,如发件人,抄送,密送等。额外的邮件头也应使用CRLF(\r\n)分隔。

PHP邮件示例

<?php  
   ini_set("sendmail_from", "sonoojaiswal@javatpoint.com");  
   to = "sonoojaiswal1987@gmail.com";//change receiver addresssubject = "This is subject";  
   message = "This is simple text message.";header = "From:sonoojaiswal@javatpoint.com \r\n";  

   result = mail (to,subject,message,header);  

   if(result == true ){  
      echo "Message sent successfully...";  
   }else{  
      echo "Sorry, unable to send mail...";  
   }  
?>

如果在实时服务器上运行此代码,它将向指定的接收者发送一封电子邮件。 PHP邮件:发送HTML消息 要发送HTML消息,您需要在消息头中提到Content-type text/html。

This is HTML heading";

   header = "From:xyz@example.com \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...";
   }
?>

PHP邮件:发送带附件的邮件

要发送带附件的消息,您需要提及许多在下面给出的示例中使用的头信息。

<?php  
  to = "abc@example.com";subject = "This is subject";  
  message = "This is a text message.";   # Open a filefile = fopen("/tmp/test.txt", "r" );//change your file location  
  if( file == false )   {      echo "Error in opening file";      exit();   }   # Read the file into a variablesize = 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:xyz@example.com\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 sectionheader .= "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 sectionheader .= "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...";  
   }  
?>  

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程