Home > php教程 > php手册 > body text

PHP mail,phpmail

WBOY
Release: 2016-06-13 08:40:41
Original
1671 people have browsed it

PHP mail,phpmail

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

PHP mail() 函数

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

语法

mail(to,subject,message,headers,parameters)
Copy after login
参数描述
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>php
</span><span>//</span><span> 何问起 hovertree.com</span>
<span>$to</span> = "hello@hovertree.net"<span>;
</span><span>$subject</span> = "Test mail"<span>;
</span><span>$message</span> = "Hello! This is a simple email message."<span>;
</span><span>$from</span> = "hello@hovertree.net"<span>;
</span><span>$headers</span> = "From: <span>$from</span>"<span>;
</span><span>mail</span>(<span>$to</span>,<span>$subject</span>,<span>$message</span>,<span>$headers</span><span>);
</span><span>echo</span> "Mail Sent."<span>;

</span>?>
Copy after login

PHP Mail Form

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

<html>
<body>
<!-- 何问起 hovertree.com -->
<?<span>php
</span><span>if</span> (<span>isset</span>(<span>$_REQUEST</span>['email'<span>]))
</span><span>//</span><span>if "email" is filled out, send email</span>
<span>  {
  </span><span>//</span><span>send email</span>
  <span>$email</span> = <span>$_REQUEST</span>['email'<span>] ; 
  </span><span>$subject</span> = <span>$_REQUEST</span>['subject'<span>] ;
  </span><span>$message</span> = <span>$_REQUEST</span>['message'<span>] ;
  </span><span>mail</span>( "someone@example.com", "Subject: <span>$subject</span>",
  <span>$message</span>, "From: <span>$email</span>"<span> );
  </span><span>echo</span> "Thank you for using our mail form"<span>;
  }
</span><span>else</span>
<span>//</span><span>if "email" is not filled out, display the form</span>
<span>  {
  </span><span>echo</span> "<span><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>;
  }
</span>?>

</body>
</html>
Copy after login

 

例子解释:

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!