search
Homephp教程php手册PHP mail - roucheng

PHP 允许您从脚本直接发送电子邮件。

PHP mail() 函数

PHP mail() 函数用于从脚本中发送电子邮件。

语法

mail(to,subject,message,headers,parameters)
参数 描述
to 必需。规定 email 接收者。
subject 必需。规定 email 的主题。注释:该参数不能包含任何新行字符。
message 必需。定义要发送的消息。应使用 LF (\n) 来分隔各行。
headers

可选。规定附加的标题,比如 From、Cc 以及 Bcc。

应当使用 CRLF (\r\n) 分隔附加的标题。

parameters 可选。对邮件发送程序规定额外的参数。

注释:PHP 需要一个已安装且正在运行的邮件系统,以便使邮件函数可用。所用的程序通过在 php.ini 文件中的配置设置进行定义。请在我们的 PHP Mail 参考手册阅读更多内容。

PHP 简易 E-Mail

通过 PHP 发送电子邮件的最简单的方式是发送一封文本 email。

在下面的例子中,我们首先声明变量($to, $subject, $message, $from, $headers),然后我们在 mail() 函数中使用这些变量来发送了一封 e-mail:

<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 何问起 hovertree.com</span>
<span style="color: #800080;">$to</span> = "hello@hovertree.net"<span style="color: #000000;">;
</span><span style="color: #800080;">$subject</span> = "Test mail"<span style="color: #000000;">;
</span><span style="color: #800080;">$message</span> = "Hello! This is a simple email message."<span style="color: #000000;">;
</span><span style="color: #800080;">$from</span> = "hello@hovertree.net"<span style="color: #000000;">;
</span><span style="color: #800080;">$headers</span> = "From: <span style="color: #800080;">$from</span>"<span style="color: #000000;">;
</span><span style="color: #008080;">mail</span>(<span style="color: #800080;">$to</span>,<span style="color: #800080;">$subject</span>,<span style="color: #800080;">$message</span>,<span style="color: #800080;">$headers</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "Mail Sent."<span style="color: #000000;">;

</span>?>

 

PHP Mail Form

通过 PHP,您能够在自己的站点制作一个反馈表单。下面的例子向指定的 e-mail 地址发送了一条文本消息:


<!-- 何问起 hovertree.com -->
<span style="color: #000000;">php
</span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_REQUEST</span>['email'<span style="color: #000000;">]))
</span><span style="color: #008000;">//</span><span style="color: #008000;">if "email" is filled out, send email</span>
<span style="color: #000000;">  {
  </span><span style="color: #008000;">//</span><span style="color: #008000;">send email</span>
  <span style="color: #800080;">$email</span> = <span style="color: #800080;">$_REQUEST</span>['email'<span style="color: #000000;">] ; 
  </span><span style="color: #800080;">$subject</span> = <span style="color: #800080;">$_REQUEST</span>['subject'<span style="color: #000000;">] ;
  </span><span style="color: #800080;">$message</span> = <span style="color: #800080;">$_REQUEST</span>['message'<span style="color: #000000;">] ;
  </span><span style="color: #008080;">mail</span>( "someone@example.com", "Subject: <span style="color: #800080;">$subject</span>",
  <span style="color: #800080;">$message</span>, "From: <span style="color: #800080;">$email</span>"<span style="color: #000000;"> );
  </span><span style="color: #0000ff;">echo</span> "Thank you for using our mail form"<span style="color: #000000;">;
  }
</span><span style="color: #0000ff;">else</span>
<span style="color: #008000;">//</span><span style="color: #008000;">if "email" is not filled out, display the form</span>
<span style="color: #000000;">  {
  </span><span style="color: #0000ff;">echo</span> "<span style="color: #000000;"><form method="post" action="mailform.php">
  Email: <input name="email" type="text"><br>
  Subject: <input name="subject" type="text"><br>
  Message:<br>
  <textarea name="message" rows="15" cols="40">
  </textarea><br>
  <input type="submit">
  </form></span>"<span style="color: #000000;">;
  }
</span>?>


 

例子解释:

  1. 首先,检查是否填写了邮件输入框
  2. 如果未填写(比如在页面被首次访问时),输出 HTML 表单
  3. 如果已填写(在表单被填写后),从表单发送邮件
  4. 当点击提交按钮后,重新载入页面,显示邮件发送成功的消息

1)windows需要配置IIS的SMTP;linux自带sendmail组件,无需设置,直接支持mail函数发送功能

   2)php.ini中声明SMTP各项参数

   3)mail("接收地址","邮件主题","邮件内容")函数的使用

 

例1:配置本地SMTP服务器

第一步: php.ini的设置:
     SMTP = localhost
     smtp_port = 25
     sendmail_from=你的设定值
第二步:需要安装IIS自带的SMTP,在SMTP虚拟服务器上点击右键,在弹出的属性窗口里进行如下设置:
     点击“访问”选项卡,再点击“中继”,在弹出的窗口出点击“添加”,然后选“单台计算机”,添加IP地址为“127.0.0.1”,然后一

     路确定返回。

第三步:
               mail("123456@qq.com","Test mail function of PHP.","hello world! hovertree.com");
      ?>

推荐:http://www.cnblogs.com/roucheng/p/3528396.html

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

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!