php 发送邮件,支持文本信息和附件

系统需要安装 sendmail,并启动 sendmail 服务

开启httpd发送邮件支持:

setsebool -P httpd_can_sendmail 1

代码如下:

<?php
// email.class.php, 发送邮件,支持文本信息和附件

class CMailMessage
{
  var $subject;
  var $addr_to;
  var $text_msg;
  var $text_encoded_files;
  var $text_tail;
  var $mime_headers;
  var $mime_boundary = "";
  var $smtp_headers;

  function CMailMessage($from, $to, $subject, $msg)
  {
    $this->mime_boundary = $this->getRandomBoundary(1);
    $this->subject = "=?utf-8?B?" . base64_encode($subject) . "?=";
    $this->addr_to = $to;
    $this->smtp_headers = $this->write_smtpheaders($from);
    $this->text_msg = $this->write_msg($msg);
    $this->mime_headers = $this->write_mimeheaders();
    $this->text_tail = $this->write_tail();
    $this->text_encoded_files = "";
  }
  function getRandomBoundary($offset=0)
  {
    srand(time()+$offset);
    return("----==".strtoupper(md5(rand()))) . "==_";
  }

  function attach_content($contents, $downfilename, $mimetype)
  {
    $encoded = chunk_split(base64_encode($contents));
    $out = "--" . $this->mime_boundary . "\n";
    $out .= "Content-type: " . $mimetype . ";\n";
    $out .= "	name=\"$downfilename\";\n";
    $out .= "Content-Transfer-Encoding: base64\n";
    $out .= "Content-disposition: attachment;\n";
    $out .= "	filename=\"$downfilename\"\n\n";
    $out .= $encoded . "\n";
    $this->text_encoded_files .= $out;
  }

  function attach_file($sourcefile,$downfilename,$mimetype)
  {
    if (is_readable($sourcefile)) {
      $fd = fopen($sourcefile, "r");
      $contents = fread($fd, filesize($sourcefile));
      fclose($fd);
      $out = $this->attach_content($contents, $downfilename, $mimetype);
      $this->text_encoded_files .= $out;
    }
  }

  function write_smtpheaders($addr_from)
  {
    $out = "From: $addr_from\n";
    $out .= "Reply-To: $addr_from\n";
    $out .= "X-Mailer: PHP3\n";
    $out .= "X-Sender: $addr_from\n";
    return $out;
  }

  function write_mimeheaders()
  {
    $out = "MIME-version: 1.0\n";
    $out .= "Content-type: multipart/alternative;\n";
    $out .= "	boundary=\"$this->mime_boundary\"\n";
    $out .= "Content-Transfer-Encoding: 8Bit\n";
    $out .= "This is a multi-part message in MIME format.\n";
    return $out;
  }

  function write_msg($msgtext)
  {
    $out = "--" . $this->mime_boundary . "\n";
    $out .= "Content-Type: text/plain;\n";
    $out .= "	charset=\"utf-8\"\n";
    $out .= "Content-Transfer-Encoding: base64\n\n";
    
    $encoded .= chunk_split(base64_encode($msgtext));
    $out .= $encoded . "\n";
    return $out;
  }
  function write_tail()
  {
    $out = "--" . $this->mime_boundary . "--\n";
    return $out;
  }

  function sendMessage()
  {
    $headers = $this->smtp_headers . $this->mime_headers;
    $message = $this->text_msg . $this->text_encoded_files . $this->text_tail;
    var_dump($this->addr_to);
    var_dump($this->subject);
    var_dump($headers);
    var_dump($message);

    mail($this->addr_to, $this->subject, $message, $headers);
  }
}
?>

使用方法如下:

第1种情况,只发送消息

<?php 
require_once('email.class.php');

//发件人
$from = 'support@aqwu.net';
//收件人
$to = 'zhangxj@aqwu.net';
//主題
$subject = "test1";


$message = "这是一条中文信息,this is a english message.";

$mailMsg = new CMailMessage($from,$to,$subject,$message);
$mailMsg->sendMessage();

?>

第2种情况,发送消息和文件,文件以附件形式来发送

<?php 
require_once('email.class.php');

//发件人
$from = 'support@aqwu.net';
//收件人
$to = 'zhangxj@aqwu.net';
//主題
$subject = "test2";


$message = "这是一条中文信息,this is a english message.";
$sourcefile = 'test.exe';
$downfilename = "test.exe";
$mimetype = "application/octet-stream";

$mailMsg = new CMailMessage($from,$to,$subject,$message);
$mailMsg->attach_file($sourcefile,$downfilename,$mimetype);
$mailMsg->sendMessage();

?>

第3种情况,发送消息和内容,内容以附件形式来发送

<?php 
require_once('email.class.php');

//发件人
$from = 'support@aqwu.net';
//收件人
$to = 'zhangxj@aqwu.net';
//主題
$subject = "test3";


$message = "这是一条中文信息,this is a english message.";
$contents = 'this is a test file info';
$downfilename = "test.txt";
$mimetype = "application/octet-stream";

$mailMsg = new CMailMessage($from,$to,$subject,$message);
$mailMsg->attach_content($contents,$downfilename,$mimetype);
$mailMsg->sendMessage();

?>

第4种情况,发送消息和多个文件和多个内容,文件和内容均以附件形式来发送

<?php 
require_once('email.class.php');

//发件人
$from = 'support@aqwu.net';
//收件人
$to = 'zhangxj@aqwu.net';
//主題
$subject = "test4";


$message = "这是一条中文信息,this is a english message.";
$contents = 'this is a test file info';
$downfilename = "test.txt";
$mimetype = "application/octet-stream";

$mailMsg = new CMailMessage($from,$to,$subject,$message);
$mailMsg->attach_content($contents,$downfilename,$mimetype);

$sourcefile = 'test.exe';
$downfilename = "test.exe";
$mimetype = "application/octet-stream";
$mailMsg->attach_file($sourcefile,$downfilename,$mimetype);

$mailMsg->sendMessage();

?>

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注