免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2108 | 回复: 8
打印 上一主题 下一主题

[函数]各位帮帮我吧,关于php的mail()函数问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-21 21:00 |只看该作者 |倒序浏览
我的server是linux,装有邮件系统是qmail,安装qmail之后,qmail可以正常启动,于是我写了个test.php文件代码如下:

  1. <?php
  2. if(mail("22good@sohu.com", "My Subject", "Line 1\nLine 2\nLine 3"))
  3. echo "1";

  4. ?>;
复制代码

我用IE运行这个test.php,运行之后页面没有抱错误,正常显示输出的“1”
可是我到邮箱里去查看,却没有收到这封邮件

请高手指点一下,我万分感谢了

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
2 [报告]
发表于 2005-09-22 09:44 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

http://www.chinaunix.net/search.html
关键词:邮件
搜索分区:PHP

论坛徽章:
0
3 [报告]
发表于 2005-09-22 21:00 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

首先,在Linux下使用:

mail test@163.com

看看能否发信.

如果不能,说明Qmail配置有误.

另外,再配置好PHP的Sendmail的位置.

安装Qmail,sendmail是指赂Qmail的一个符号链接.

论坛徽章:
0
4 [报告]
发表于 2005-09-23 17:23 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

我用的是win2000,怎么也出现发不出去的问题?

<?php
$address="yxmwxl@163.com";
$subj="this is test";
$mesg="this is test";
$headers="content-type:text/html;charset=gb2312\n";
mail($address,$sub,$msg,$headers);
echo "ok:";
?>;

论坛徽章:
0
5 [报告]
发表于 2005-09-24 11:08 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

mail()函数默认需要有SMTP服务的支持才能发出去。
我记得有一个php类库,可以不需要smtp服务直接就可以发出邮件去。
你可以找找。我记得好像是在这个站看到的。www.phpe.net

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
6 [报告]
发表于 2005-09-24 11:54 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

我常用的一个Class.smtp.php

  1. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  2. $smtp->;debug = true; //是否显示发送的调试信息
  3. $emailtype = "HTML"; //信件类型,文本:text;网页:HTML
  4. $smtp->;sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
复制代码



  1. // #########################################
  2. $smtpserver     = "localhost"; //SMTP服务器
  3. $smtpserverport = 25; //SMTP服务器端口
  4. $smtpusermail   = "postmaster@test.com"; //SMTP服务器的用户邮箱
  5. $smtpuser       = "postmaster@test.com"; //SMTP服务器的用户帐号
  6. $smtppass       = "password"; //SMTP服务器的用户密码

  7. // bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

  8. if (!function_exists('mail')) {

  9.         $smtp = new smtp($smtpserver, $smtpserverport, false, $smtpuser, $smtppass);
  10.                 //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  11.         $smtp->;debug = false;
  12.                 //是否显示发送的调试信息

  13.         function mail($to, $subject, $message, $headers)
  14.         {
  15.                 global $smtp;

  16.                 $smtpemailto = $to;
  17.                 if (preg_match('/From:(.+?)\\n/sim', $headers, $regs)) {
  18.                         $smtpusermail = $regs[0];
  19.                 } else {
  20.                         $smtpusermail = $headers;
  21.                 }
  22.                 $mailsubject = $subject;
  23.                 $mailbody = $message;
  24.                 return $smtp->;sendmail($smtpemailto, $smtpusermail, $mailsubject, $maibody, $mailtype);
  25.         }
  26. }



  27. class smtp {
  28.     /* Public Variables */

  29.     var $smtp_port;

  30.     var $time_out;

  31.     var $host_name;

  32.     var $log_file;

  33.     var $relay_host;

  34.     var $debug;

  35.     var $auth;

  36.     var $user;

  37.     var $pass;

  38.     /* Private Variables */
  39.     var $sock;

  40.     /* Constractor */

  41.     function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass)
  42.     {
  43.         $this->;debug = false;

  44.         $this->;smtp_port = $smtp_port;

  45.         $this->;relay_host = $relay_host;

  46.         $this->;time_out = 30; //is used in fsockopen()

  47.         $this->;auth = $auth; //auth

  48.         $this->;user = $user;

  49.         $this->;pass = $pass;

  50.         $this->;host_name = "localhost"; //is used in HELO command
  51.         $this->;log_file = "";

  52.         $this->;sock = false;
  53.     }

  54.     /* Main Function */

  55.     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  56.     {
  57.         $mail_from = $this->;get_address($this->;strip_comment($from));

  58.         $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);

  59.         $header .= "MIME-Version:1.0\r\n";

  60.         if ($mailtype == "HTML") {
  61.             $header .= "Content-Type:text/html\r\n";
  62.         }

  63.         $header .= "To: " . $to . "\r\n";

  64.         if ($cc != "") {
  65.             $header .= "Cc: " . $cc . "\r\n";
  66.         }

  67.         $header .= "From: $from<" . $from . ">;\r\n";

  68.         $header .= "Subject: " . $subject . "\r\n";

  69.         $header .= $additional_headers;

  70.         $header .= "Date: " . date("r") . "\r\n";

  71.         $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")\r\n";

  72.         list($msec, $sec) = explode(" ", microtime());

  73.         $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">;\r\n";

  74.         $TO = explode(",", $this->;strip_comment($to));

  75.         if ($cc != "") {
  76.             $TO = array_merge($TO, explode(",", $this->;strip_comment($cc)));
  77.         }

  78.         if ($bcc != "") {
  79.             $TO = array_merge($TO, explode(",", $this->;strip_comment($bcc)));
  80.         }

  81.         $sent = true;

  82.         foreach ($TO as $rcpt_to) {
  83.             $rcpt_to = $this->;get_address($rcpt_to);

  84.             if (!$this->;smtp_sockopen($rcpt_to)) {
  85.                 $this->;log_write("Error: Cannot send email to " . $rcpt_to . "\n");

  86.                 $sent = false;

  87.                 continue;
  88.             }

  89.             if ($this->;smtp_send($this->;host_name, $mail_from, $rcpt_to, $header, $body)) {
  90.                 $this->;log_write("E-mail has been sent to <" . $rcpt_to . ">;\n");
  91.             } else {
  92.                 $this->;log_write("Error: Cannot send email to <" . $rcpt_to . ">;\n");

  93.                 $sent = false;
  94.             }

  95.             fclose($this->;sock);

  96.             $this->;log_write("Disconnected from remote host\n");
  97.         }

  98.         return $sent;
  99.     }

  100.     /* Private Functions */

  101.     function smtp_send($helo, $from, $to, $header, $body = "")
  102.     {
  103.         if (!$this->;smtp_putcmd("HELO", $helo)) {
  104.             return $this->;smtp_error("sending HELO command");
  105.         }
  106.         // auth
  107.         if ($this->;auth) {
  108.             if (!$this->;smtp_putcmd("AUTH LOGIN", base64_encode($this->;user))) {
  109.                 return $this->;smtp_error("sending HELO command");
  110.             }

  111.             if (!$this->;smtp_putcmd("", base64_encode($this->;pass))) {
  112.                 return $this->;smtp_error("sending HELO command");
  113.             }
  114.         }

  115.         if (!$this->;smtp_putcmd("MAIL", "FROM:<" . $from . ">;")) {
  116.             return $this->;smtp_error("sending MAIL FROM command");
  117.         }

  118.         if (!$this->;smtp_putcmd("RCPT", "TO:<" . $to . ">;")) {
  119.             return $this->;smtp_error("sending RCPT TO command");
  120.         }

  121.         if (!$this->;smtp_putcmd("DATA")) {
  122.             return $this->;smtp_error("sending DATA command");
  123.         }

  124.         if (!$this->;smtp_message($header, $body)) {
  125.             return $this->;smtp_error("sending message");
  126.         }

  127.         if (!$this->;smtp_eom()) {
  128.             return $this->;smtp_error("sending <CR>;<LF>;.<CR>;<LF>; [EOM]");
  129.         }

  130.         if (!$this->;smtp_putcmd("QUIT")) {
  131.             return $this->;smtp_error("sending QUIT command");
  132.         }

  133.         return true;
  134.     }

  135.     function smtp_sockopen($address)
  136.     {
  137.         if ($this->;relay_host == "") {
  138.             return $this->;smtp_sockopen_mx($address);
  139.         } else {
  140.             return $this->;smtp_sockopen_relay();
  141.         }
  142.     }

  143.     function smtp_sockopen_relay()
  144.     {
  145.         $this->;log_write("Trying to " . $this->;relay_host . ":" . $this->;smtp_port . "\n");

  146.         $this->;sock = @fsockopen($this->;relay_host, $this->;smtp_port, $errno, $errstr, $this->;time_out);

  147.         if (!($this->;sock && $this->;smtp_ok())) {
  148.             $this->;log_write("Error: Cannot connenct to relay host " . $this->;relay_host . "\n");

  149.             $this->;log_write("Error: " . $errstr . " (" . $errno . ")\n");

  150.             return false;
  151.         }

  152.         $this->;log_write("Connected to relay host " . $this->;relay_host . "\n");

  153.         return true;;
  154.     }

  155.     function smtp_sockopen_mx($address)
  156.     {
  157.         $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);

  158.         if (!@getmxrr($domain, $MXHOSTS)) {
  159.             $this->;log_write("Error: Cannot resolve MX \"" . $domain . "\"\n");

  160.             return false;
  161.         }

  162.         foreach ($MXHOSTS as $host) {
  163.             $this->;log_write("Trying to " . $host . ":" . $this->;smtp_port . "\n");

  164.             $this->;sock = @fsockopen($host, $this->;smtp_port, $errno, $errstr, $this->;time_out);

  165.             if (!($this->;sock && $this->;smtp_ok())) {
  166.                 $this->;log_write("Warning: Cannot connect to mx host " . $host . "\n");

  167.                 $this->;log_write("Error: " . $errstr . " (" . $errno . ")\n");

  168.                 continue;
  169.             }

  170.             $this->;log_write("Connected to mx host " . $host . "\n");

  171.             return true;
  172.         }

  173.         $this->;log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")\n");

  174.         return false;
  175.     }

  176.     function smtp_message($header, $body)
  177.     {
  178.         fputs($this->;sock, $header . "\r\n" . $body);

  179.         $this->;smtp_debug(">; " . str_replace("\r\n", "\n" . ">; ", $header . "\n>; " . $body . "\n>; "));

  180.         return true;
  181.     }

  182.     function smtp_eom()
  183.     {
  184.         fputs($this->;sock, "\r\n.\r\n");

  185.         $this->;smtp_debug(". [EOM]\n");

  186.         return $this->;smtp_ok();
  187.     }

  188.     function smtp_ok()
  189.     {
  190.         $response = str_replace("\r\n", "", fgets($this->;sock, 512));

  191.         $this->;smtp_debug($response . "\n");

  192.         if (!ereg("^[23]", $response)) {
  193.             fputs($this->;sock, "QUIT\r\n");

  194.             fgets($this->;sock, 512);

  195.             $this->;log_write("Error: Remote host returned \"" . $response . "\"\n");

  196.             return false;
  197.         }

  198.         return true;
  199.     }

  200.     function smtp_putcmd($cmd, $arg = "")
  201.     {
  202.         if ($arg != "") {
  203.             if ($cmd == "") $cmd = $arg;

  204.             else $cmd = $cmd . " " . $arg;
  205.         }

  206.         fputs($this->;sock, $cmd . "\r\n");

  207.         $this->;smtp_debug(">; " . $cmd . "\n");

  208.         return $this->;smtp_ok();
  209.     }

  210.     function smtp_error($string)
  211.     {
  212.         $this->;log_write("Error: Error occurred while " . $string . ".\n");

  213.         return false;
  214.     }

  215.     function log_write($message)
  216.     {
  217.         $this->;smtp_debug($message);

  218.         if ($this->;log_file == "") {
  219.             return true;
  220.         }

  221.         $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;

  222.         if (!@file_exists($this->;log_file) || !($fp = @fopen($this->;log_file, "a"))) {
  223.             $this->;smtp_debug("Warning: Cannot open log file \"" . $this->;log_file . "\"\n");

  224.             return false;;
  225.         }

  226.         flock($fp, LOCK_EX);

  227.         fputs($fp, $message);

  228.         fclose($fp);

  229.         return true;
  230.     }

  231.     function strip_comment($address)
  232.     {
  233.         $comment = "\([^()]*\)";

  234.         while (ereg($comment, $address)) {
  235.             $address = ereg_replace($comment, "", $address);
  236.         }

  237.         return $address;
  238.     }

  239.     function get_address($address)
  240.     {
  241.         $address = ereg_replace("([ \t\r\n])+", "", $address);

  242.         $address = ereg_replace("^.*<(.+)>;.*$", "\1", $address);

  243.         return $address;
  244.     }

  245.     function smtp_debug($message)
  246.     {
  247.         if ($this->;debug) {
  248.             echo $message . "
  249. ;";
  250.         }
  251.     }
  252. }


复制代码

论坛徽章:
0
7 [报告]
发表于 2005-09-24 14:21 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

首先十分的感谢楼上的几位大哥,还有版主!

情况是这样的,公司让我做一个邮件杂志订阅功能,通过在web页面html的form表单上提交邮箱地址,就可以收到一封确认的邮件,公司有台server是linux red hat9,上面装有qmail,我写了个文件test.php测试一下代码如下
[code]
<?php
if(mail("22good@sohu.com", "My Subject", "Line 1\nLine 2\nLine 3")
echo "1";
?>;
[code]
我用IE执行这个文件,我的邮箱就收到了邮件。由于其他原因有人把服务器重启过,qmail也重启了,再次用IE执行test.php这个文件,邮箱里就收不到邮件了,页面也没有错误显示,页面就显示输出的‘1’。
我在另外一台服务器(也是red hat linux9)也试着装上qmail,磕磕绊绊的,按照网上找的文档安装的,装完之后启动qmail,执行同样的test.php文件,我的邮箱里还是收不到邮件。

    
我看过好多文档了,由于自己水平低,一团雾水
我哭呀~~~哪位大哥能给我讲的具体一些呀,我请吃饭
问题问的不好,还请大家原谅

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
8 [报告]
发表于 2005-09-24 14:35 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

请你做如下测试:
1、你的qmail可以通过foemail来发送电子邮件么?如果不可以,先解决这个问题。
2、使用我给你的这个代码,直接mail测试

论坛徽章:
0
9 [报告]
发表于 2005-09-25 05:38 |只看该作者

[函数]各位帮帮我吧,关于php的mail()函数问题

问题解决了,谢谢HonestQiao老大!

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP