PHP基础 专题
您的位置:php > PHP基础 专题 > PHP发送电子邮件(Email)
PHP发送电子邮件(Email)
作者:--    发布时间:2019-11-20

php mail()函数用于在php中发送电子邮件。 您可以使用php mail()函数来发送短信,html消息和附件与消息。

php mail()函数

语法

注意:消息的每一行应使用crlf(\r\n)分隔,并且行不应大于70个字符。

php邮件示例

文件:mailer.php

<?php  
   ini_set("sendmail_from", "maxsujaiswal@h3.com");  
   $to = "maxsujaiswal1987@gmail.com";//change receiver address  
   $subject = "this is subject";  
   $message = "this is simple text message.";  
   $header = "from:maxsujaiswal@h3.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

<?php  
   $to = "abc@example.com";//change receiver address  
   $subject = "this is subject";  
   $message = "<h1>this is html heading</h1>";  

   $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 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: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 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...";  
   }  
?>

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册