search
HomeBackend DevelopmentPHP Tutorialphp实现在站点里面添加邮件发送的功能_PHP

下面夏日博客来讲下如何在站点里面添加一个邮件发送的功能。
首先需要下载一个smtp 的 php 邮件发送类,代码如下:

<&#63;php
class smtp 
{ 
  /* Public Variables */
  public $smtp_port; 
  public $time_out; 
  public $host_name; 
  public $log_file; 
  public $relay_host; 
  public $debug; 
  public $auth; 
  public $user; 
  public $pass; 
   
  /* Private Variables */
  private $sock; 
 
  /* Constractor */
  function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
  { 
    $this->debug = FALSE; 
    $this->smtp_port = $smtp_port; 
    $this->relay_host = $relay_host; 
    $this->time_out = 30; //is used in fsockopen() 
    # 
    $this->auth = $auth;//auth 
    $this->user = $user; 
    $this->pass = $pass; 
    # 
    $this->host_name = "localhost"; //is used in HELO command 
    $this->log_file =""; 
     
    $this->sock = FALSE; 
  } 
 
  /* Main Function */
  function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
  { 
    $mail_from = $this->get_address($this->strip_comment($from)); 
    $body = preg_replace("/(^|(\r\n))(\\.)/", "\\1.\\3", $body); 
    $header .= "MIME-Version:1.0\r\n"; 
    if($mailtype=="HTML"){ 
      $header .= "Content-Type:text/html\r\n"; 
    } 
      $header .= "To: ".$to."\r\n"; 
    if ($cc != "") { 
      $header .= "Cc: ".$cc."\r\n"; 
    } 
    $header .= "From: $from<".$from.">\r\n"; 
    $header .= "Subject: ".$subject."\r\n"; 
    $header .= $additional_headers; 
    $header .= "Date: ".date("r")."\r\n"; 
    $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 
    list($msec, $sec) = explode(" ", microtime()); 
    $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 
    $TO = explode(",", $this->strip_comment($to)); 
   
    if ($cc != "") { 
    $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
    } 
     
    if ($bcc != "") { 
    $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
    } 
   
    $sent = TRUE; 
    foreach ($TO as $rcpt_to) { 
      $rcpt_to = $this->get_address($rcpt_to); 
      if (!$this->smtp_sockopen($rcpt_to)) { 
        $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 
        $sent = FALSE; 
        continue; 
      } 
      if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
        $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 
      } else { 
        $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 
        $sent = FALSE; 
      } 
      fclose($this->sock); 
      $this->log_write("Disconnected from remote host\n"); 
    } 
    echo "<br>"; 
    //echo $header; 
    return $sent; 
  } 
 
  /* Private Functions */
   
  function smtp_send($helo, $from, $to, $header, $body = "") 
  { 
    if (!$this->smtp_putcmd("HELO", $helo)) { 
      return $this->smtp_error("sending HELO command"); 
    } 
    #auth 
    if($this->auth){ 
      if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 
        return $this->smtp_error("sending HELO command"); 
      } 
      if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
        return $this->smtp_error("sending HELO command"); 
      } 
    } 
    # 
    if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 
      return $this->smtp_error("sending MAIL FROM command"); 
    } 
     
    if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 
      return $this->smtp_error("sending RCPT TO command"); 
    } 
 
    if (!$this->smtp_putcmd("DATA")) { 
      return $this->smtp_error("sending DATA command"); 
    } 
     
    if (!$this->smtp_message($header, $body)) { 
      return $this->smtp_error("sending message"); 
    } 
 
    if (!$this->smtp_eom()) { 
      return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
    } 
     
    if (!$this->smtp_putcmd("QUIT")) { 
      return $this->smtp_error("sending QUIT command"); 
    } 
 
    return TRUE; 
  } 
 
  function smtp_sockopen($address) 
  { 
    if ($this->relay_host == "") { 
      return $this->smtp_sockopen_mx($address); 
    } else { 
      return $this->smtp_sockopen_relay(); 
    } 
  } 
 
  function smtp_sockopen_relay() 
  { 
    $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 
    $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 
    if (!($this->sock && $this->smtp_ok())) { 
      $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 
      $this->log_write("Error: ".$errstr." (".$errno.")\n"); 
      return FALSE; 
    } 
    $this->log_write("Connected to relay host ".$this->relay_host."\n"); 
    return TRUE;; 
  } 
 
  function smtp_sockopen_mx($address) 
  { 
    $domain = preg_replace("/^.+@([^@]+)$/", "\\1", $address); 
    if (!@getmxrr($domain, $MXHOSTS)) { 
      $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 
      return FALSE; 
    } 
    foreach ($MXHOSTS as $host) { 
      $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 
      $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 
      if (!($this->sock && $this->smtp_ok())) { 
        $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 
        $this->log_write("Error: ".$errstr." (".$errno.")\n"); 
        continue; 
      } 
      $this->log_write("Connected to mx host ".$host."\n"); 
      return TRUE; 
    } 
    $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 
    return FALSE; 
  } 
 
  function smtp_message($header, $body) 
  { 
    fputs($this->sock, $header."\r\n".$body); 
    $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 
     
    return TRUE; 
  } 
 
  function smtp_eom() 
  { 
    fputs($this->sock, "\r\n.\r\n"); 
    $this->smtp_debug(". [EOM]\n"); 
     
    return $this->smtp_ok(); 
  } 
 
  function smtp_ok() 
  { 
    $response = str_replace("\r\n", "", fgets($this->sock, 512)); 
    $this->smtp_debug($response."\n"); 
     
    if (!preg_match("/^[23]/", $response)) { 
      fputs($this->sock, "QUIT\r\n"); 
      fgets($this->sock, 512); 
      $this->log_write("Error: Remote host returned \"".$response."\"\n"); 
      return FALSE; 
    } 
    return TRUE; 
  } 
 
  function smtp_putcmd($cmd, $arg = "") 
  { 
    if ($arg != "") { 
      if($cmd=="") $cmd = $arg; 
      else $cmd = $cmd." ".$arg; 
    } 
 
    fputs($this->sock, $cmd."\r\n"); 
    $this->smtp_debug("> ".$cmd."\n"); 
 
    return $this->smtp_ok(); 
  } 
 
  function smtp_error($string) 
  { 
    $this->log_write("Error: Error occurred while ".$string.".\n"); 
    return FALSE; 
  } 
 
  function log_write($message) 
  { 
    $this->smtp_debug($message); 
     
    if ($this->log_file == "") { 
      return TRUE; 
    } 
 
    $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 
    if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 
      $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 
      return FALSE; 
    } 
    flock($fp, LOCK_EX); 
    fputs($fp, $message); 
    fclose($fp); 
     
    return TRUE; 
  } 
 
  function strip_comment($address) 
  { 
    $comment = "/\\([^()]*\\)/"; 
    while (preg_match($comment, $address)) { 
      $address = preg_replace($comment, "", $address); 
    } 
     
    return $address; 
  } 
 
  function get_address($address) 
  { 
    $address = preg_replace("/([ \t\r\n])+/", "", $address); 
    $address = preg_replace("/^.*<(.+)>.*$/", "\\1", $address); 
     
    return $address; 
  } 
 
  function smtp_debug($message) 
  { 
    if ($this->debug) { 
      echo $message."<br>"; 
    } 
  } 
 
function get_attach_type($image_tag) { // 
 
  $filedata = array(); 
   
  $img_file_con=fopen($image_tag,"r"); 
  unset($image_data); 
  while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) 
  $image_data.=$tem_buffer; 
  fclose($img_file_con); 
 
  $filedata['context'] = $image_data; 
  $filedata['filename']= basename($image_tag); 
  $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); 
  switch($extension){ 
    case ".gif": 
      $filedata['type'] = "image/gif"; 
      break; 
    case ".gz": 
      $filedata['type'] = "application/x-gzip"; 
      break; 
    case ".htm": 
      $filedata['type'] = "text/html"; 
      break; 
    case ".html": 
      $filedata['type'] = "text/html"; 
      break; 
    case ".jpg": 
      $filedata['type'] = "image/jpeg"; 
      break; 
    case ".tar": 
      $filedata['type'] = "application/x-tar"; 
      break; 
    case ".txt": 
      $filedata['type'] = "text/plain"; 
      break; 
    case ".zip": 
      $filedata['type'] = "application/zip"; 
      break; 
    default: 
      $filedata['type'] = "application/octet-stream"; 
      break; 
  } 
  return $filedata; 
  } 
 
} // end class 
&#63;>

这类的邮件发送类网上有许多,可以挑一款自己喜欢的就行,将以上的代码保存为 ZC_Email.class.php 文件,然后在使用的时候直接 include_once("ZC_Email.class.php") 就可以了,代码如下:

<&#63;php 
include_once("ZC_Email.class.php");
&#63;>

这是将邮件的发送类已经嵌入到页面中了,下一步就要在页面进行内容的发送了,我这里的系统是用户购买成功后进行发送,实例代码如下:

//空间购买成功发送邮件
    $smtpserver = "smtp.163.com";//SMTP服务器 
    $smtpserverport =25;//SMTP服务器端口 
    $smtpusermail = "xiariboke@163.com";//SMTP服务器的用户邮箱 
    $smtpemailto = "xiariboke@qq.com";//发送给谁 
    $smtpuser = "xiariboke@163.com";//SMTP服务器的用户帐号 
    $smtppass = "xiariboke";//SMTP服务器的用户密码 
    $mailsubject = "精品门业网香港空间购买";//邮件主题 
    $mailbody = "成功购买香港空间 $size M空间,购买域名为:$domain 购买时间为:".format_date(time(),2)." 到期时间为: $lasttime ".format_date(time(),5);//邮件内容 
    $mailtype = "TXT";//邮件格式(HTML/TXT),TXT为文本邮件 
    ########################################## 
    $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
    $smtp->debug = false;//是否显示发送的调试信息 
    $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 
    //邮件发送结束
 
    showmsg('购买成功!','ZC_Link_List.php&#63;sort=2');

其中 SMTP 服务器的用户邮箱和密码自己要设置好,这是发给指定的一个邮箱,当然可以自定义改成自己的,这里面的变量都是我站点内使用的,如果不需要可以自己稍微修改一下,这里不多讲了。
希望本文所述对大家学习php程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP实现邮件发送及接收的方法PHP实现邮件发送及接收的方法Jun 18, 2023 am 08:38 AM

PHP是一种广泛使用的服务器端脚本语言,在开发Web应用程序时经常用到。它可以轻易地发送和接收电子邮件,这让开发者可以快速构建自己的邮件系统。在本文中,我们将探讨如何使用PHP实现邮件发送和接收的方法。一、发送电子邮件PHP提供了发送电子邮件的许多函数,最常用的是使用SMTP服务器发送电子邮件的PHPMailer类。这个类是使用PHP编写的开源库,具有广泛的

PHP邮件发送方法及常见问题汇总PHP邮件发送方法及常见问题汇总Jun 08, 2023 pm 10:57 PM

在互联网时代,邮件已经成为人们生活、工作中不可或缺的一个部分。PHP作为一种广泛应用于Web开发领域的语言,邮件发送在Web应用中也是必不可少的。本文将详细介绍PHP邮件发送的相关内容和常见问题汇总。一、PHP邮件发送方法PHPmailer库PHPmailer是一种功能强大的PHP邮件发送类库,它可以轻松地发送HTML格式和纯文本格式的邮件。使用PHPmai

PHP邮件发送指南:如何使用mail函数发送邮件PHP邮件发送指南:如何使用mail函数发送邮件Jul 30, 2023 pm 10:13 PM

PHP邮件发送指南:如何使用mail函数发送邮件在Web开发中,经常会遇到需要发送邮件的情况,例如注册成功后自动发送欢迎邮件,或者忘记密码后重置密码邮件等。而在PHP中,我们可以使用mail函数来实现邮件的发送功能。本篇文章将教你如何使用mail函数发送邮件。一、准备工作在使用mail函数发送邮件之前,我们需要确保服务器已经配置好了SMTP服务,并且安装了s

如何使用PHP通过邮箱发送电子邮件?如何使用PHP通过邮箱发送电子邮件?Sep 19, 2023 am 09:46 AM

如何使用PHP通过邮箱发送电子邮件?随着互联网的发展,电子邮件已经成为了人们日常生活和工作中不可或缺的一部分。而通过编程语言实现自动发送电子邮件的功能,则能极大地提高工作效率和便捷性。在PHP中,我们可以使用SMTP协议通过邮箱发送电子邮件。接下来,我将为大家介绍如何在PHP中实现通过邮箱发送电子邮件的具体方法,并给出代码示例。步骤一:安装必要的库在PHP中

如何处理PHP表单中的邮件发送和接收如何处理PHP表单中的邮件发送和接收Aug 11, 2023 am 08:30 AM

如何处理PHP表单中的邮件发送和接收邮件是现代通讯的重要方式之一,通过在网站的表单中添加邮件发送和接收功能,可以使网站更加实用和互动。本文将介绍如何使用PHP处理表单中的邮件发送和接收。邮件发送在处理邮件发送前,首先确保服务器已经配置好了邮件发送功能。一般来说,邮件发送涉及到SMTP服务器的设置,可以从网络服务提供商或者网络管理员处获取SMTP服务器的地址、

PHP邮件发送函数详细解析:mail、smtp、PHPMailer等函数的邮件发送操作指南PHP邮件发送函数详细解析:mail、smtp、PHPMailer等函数的邮件发送操作指南Nov 18, 2023 pm 05:20 PM

PHP邮件发送函数详细解析:mail、smtp、PHPMailer等函数的邮件发送操作指南,需要具体代码示例一、引言在现代社会中,电子邮件已经成为人们沟通、交流信息的重要工具之一。在Web开发中,我们经常会遇到发送邮件的需求,无论是用户注册验证、密码重置,还是系统通知和营销活动,都需要用到邮件发送功能。PHP作为一种强大的脚本语言,提供了多种发送邮件的函数和

如何使用PHP实现邮件到达和读取功能?如何使用PHP实现邮件到达和读取功能?Sep 19, 2023 pm 01:29 PM

如何使用PHP实现邮件到达和读取功能?随着互联网的迅速发展,电子邮件已经成为人们日常生活和工作中不可缺少的一部分。使用PHP语言来实现邮件到达和读取功能,可以帮助我们更加高效地管理和处理邮件。下面我将详细介绍如何使用PHP来实现邮件到达和读取功能,包括配置SMTP、发送邮件和读取邮件。配置SMTP要发送和读取邮件,首先需要配置SMTP参数。SMTP(Simp

使用PHP来发送电子邮件使用PHP来发送电子邮件Jun 11, 2023 am 08:46 AM

随着互联网和电子邮件的普及,越来越多的人开始使用电子邮件作为主要的沟通工具。PHP是一种流行的服务器端编程语言,也可以用来发送电子邮件。在本文中,我们将介绍如何使用PHP来发送电子邮件。配置SMTP服务器首先,我们需要配置SMTP服务器。SMTP(SimpleMailTransferProtocol)是电子邮件传输的标准协议。大多数邮件服务提供商都会提

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),