PHP Mail邮件

PHP邮件 PHPmail()函数用于以PHP发送电子邮件。您可以使用PHPmail()函数发送文本消息,ht…

PHP邮件

PHPmail()函数用于以PHP发送电子邮件。您可以使用PHPmail()函数发送文本消息,html消息和带有消息的附件。

PHP mail() 函数

句法

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

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

  • [email protected]
  • [email protected],另一个[email protected]
  • 用户<[email protected]>
  • 用户<[email protected]>,另一个用户<[email protected]>

$subject:代表邮件的主题。

$message:表示要发送的邮件的消息。

注意:消息的每一行都应以CRLF( r n)分隔,并且行数不得超过70个字符。

$additional_headers(可选):指定其他标头,例如From,CC,BCC等。其他附加标头也应使用CRLF(rn)分隔。

PHP邮件示例

<?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] rn";

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

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

如果在实时服务器上运行此代码,它将向指定的收件人发送电子邮件。

PHP邮件:发送HTML消息

要发送HTML消息,您需要在消息标题中提及Content-typetext/html。

<?php
   $to = "[email protected]";//change receiver address
   $subject = "This is subject";
   $message = "<h2>This is HTML heading</h2>";

   $header = "From:[email protected] rn";
   $header .= "MIME-Version: 1.0 rn";
   $header .= "Content-type: text/html;charset=UTF-8 rn";

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

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

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

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

<?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 rn 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]rn";
  $header .= "MIME-Version: 1.0rn";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$numrn";
  $header .= "--$numrn";

  # Define the message section
  $header .= "Content-Type: text/plainrn";
  $header .= "Content-Transfer-Encoding:8bitrnn";
  $header .= "$messagern";
  $header .= "--$numrn";

  # Define the attachment section
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name="test.txt"rn";
  $header .= "Content-Transfer-Encoding:base64rn";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename="test.txt"rnn";
  $header .= "$encoded_contentrn";
  $header .= "--$num--";

  # Send email now
  $result = mail ( $to, $subject, "", $header );
  if( $result == true ){
      echo "Message sent successfully...";
   }else{
      echo "Sorry, unable to send mail...";
   }
?>

类别:PHP 技巧

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册