由于php没有提供现成的smtp函数,却提供了一个功能不甚灵活的mail()函数,这个函数需要服务器配置上的支持,并且不支持smtp验证,在很多场合无法正常的工作,因此不建议使用。本文的目的在于为新手指明方向,并没有涉及那些高级的内容,一来本身水平有限,二来也担心不能准确的讲述相关的概念,进而对各位造成误导,还请自行深入学习。本文撰写日期是2004年7月2日,阅读时请注意时效性。
“使用php发送mail”最近已经成为继“register_globals”以后本版第二个新手陷阱,今天特地写这篇文章为新手解惑,希望可以为迷茫的人指明方向。
让我们先从以下这个例子开始说起:
| 引用: |
| [root@server~/]# te.net localhost 25 trying 127.0.0.1... connected to localhost. escape character is '^]'. 220 server.domain.com.br esmtp postfix (2.1.0) mail from: teste@dominio.com.br 250 ok rcpt to: teste@dominio.com.br 250 ok data 354 end data with <cr><lf>.<cr><lf> teste . 250 ok: queued as 7b41f4665a quit 221 bye connection closed by foreign host. 注:以上来.netkiller的postfix文档,偷懒,直接用现成的。 |
| 引用: |
| trying 127.0.0.1... connected to localhost. escape character is '^]'. 220 server.domain.com.br esmtp postfix (2.1.0) |
| 引用: |
| data 354 end data with <cr><lf>.<cr><lf> teste . |
| 代码: |
| <?php require_once 'mail.php'; $conf['mail'] = array( 'host' => 'xx.xx.xx.xx', //smtp服务器地址,可以用ip地址或者域名 'auth' => true, //true表示smtp服务器需要验证,false代码不需要 'username' => 'tester', //用户名 'password' => 'retset' //密码 ); /*** * 使用$headers数组,可以定义邮件头的内容,比如使用$headers['reply-to']可以定义回复地址 * 通过这种方式,可以很方便的定制待发送邮件的邮件头 ***/ $headers['from'] = 'tester@domain.com'; //发信地址 $headers['to'] = 'tester@domain.com'; //收信地址 $headers['subject'] = 'test mail send by php'; //邮件标题 $mail_object = &mail::factory('smtp', $conf['mail']); $body = <<< msg //邮件正文 hello world!!! msg; $mail_res = $mail_object->send($headers['to'], $headers, $body); //发送 if( mail::iserror($mail_res) ){ //检测错误 die($mail_res->getmessage()); } ?> |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器