php专区

 首页 > php专区 > PHP应用 > 常用功能 > php fsockopen邮箱发送实例代码 - php高级应用

php fsockopen邮箱发送实例代码 - php高级应用

分享到:
【字体:
导读:
          ? ok的邮箱发送。includesmtp class php; $smtpserver=smtp 163 com; 您的smtp服务器的地址$smtpserver=smtp 163 com;$port=25...

php fsockopen邮箱发送实例代码

  1. //ok的邮箱发送。  
  2. include "smtp.class.php";  
  3. //$smtpserver = "smtp.163.com"; //您的smtp服务器的地址  
  4. $smtpserver="smtp.163.com";  
  5. $port =25; //smtp服务器的端口,一般是 25  
  6. $smtpuser = "你的邮箱@163.com"//您登录smtp服务器的用户名  
  7. $smtppwd = "你邮箱的密码"//您登录smtp服务器的密码  
  8. $mailtype = "txt"//邮件的类型,可选值是 txt 或 html ,txt 表示是纯文本的邮件,html 表示是 html格式的邮件  
  9. $sender = "你的邮箱@163.com";  
  10. //发件人,一般要与您登录smtp服务器的用户名($smtpuser)相同,否则可能会因为smtp服务器的设置导致发送失败  
  11. $smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);  
  12. $smtp->debug = true; //是否开启调试,只在测试程序时使用,正式使用时请将此行注释  
  13. $to = "你要发给的那个人的邮箱地址"//收件人  
  14. $subject = "你好";  
  15. $body = "你发送的内容 ";  
  16. $send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype); 
  17. if($send==1){  
  18. echo "邮件发送成功";  
  19. }else{  
  20. echo "邮件发送失败
    "
    ;  
  21. //echo "原因:".$this->smtp->logs;  
  22. }  
  23. ?> 

邮箱发送类smtp.class.php

  1. class smtp  
  2. {  
  3. /* public variables */  
  4. var $smtp_port;  
  5. var $time_out;  
  6. var $host_name;  
  7. var $log_file;  
  8. var $relay_host;  
  9. var $debug;  
  10. var $auth;  
  11. var $user;  
  12. var $pass;  
  13. /* private variables */  
  14. var $sock;  
  15. /* constractor */  
  16. function smtp($relay_host = ""$smtp_port = 25,$auth = false,$user,$pass)  
  17. {  
  18. $this->debug = false;  
  19. $this->smtp_port = $smtp_port;  
  20. $this->relay_host = $relay_host;  
  21. $this->time_out = 30; //is used in fsockopen()  
  22. #  
  23. $this->auth = $auth;//auth  
  24. $this->user = $user;  
  25. $this->pass = $pass;  
  26. #  
  27. $this->host_name = "localhost"//is used in helo command  
  28. $this->log_file = "";  
  29. $this->sock = false;  
  30. }  
  31. /* main function */  
  32. function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = "")  
  33. {  
  34. $mail_from = $this->get_address($this->strip_comment($from));  
  35. $body = ereg_replace("(^|(rn))(.)""1.3"$body);  
  36. $header .= "mime-version:1.0rn";  
  37. if($mailtype=="html"){  
  38. $header .= "content-type:text/htmlrn";  
  39. }  
  40. $header .= "to: ".$to."rn";  
  41. if ($cc != "") {  
  42. $header .= "cc: ".$cc."rn";  
  43. }  
  44. $header .= "from: $from<".$from.">;rn";  
  45. $header .= "subject: ".$subject."rn";  
  46. $header .= $additional_headers;  
  47. $header .= "date: ".date("r")."rn";  
  48. $header .= "x-mailer:by redhat (php/".phpversion().")rn";  
  49. list($msec$sec) = explode(" ", microtime());  
  50. $header .= "message-id: <".date("ymdhis"$sec).".".($msec*1000000).".".$mail_from.">;rn";  
  51. $to = explode(","$this->strip_comment($to));  
  52. if ($cc != "") {  
  53. $to = array_merge($toexplode(","$this->strip_comment($cc)));  
  54. }  
  55. if ($bcc != "") {  
  56. $to = array_merge($toexplode(","$this->strip_comment($bcc)));  
  57. }  
  58. $sent = true;  
  59. foreach ($to as $rcpt_to) {  
  60. $rcpt_to = $this->get_address($rcpt_to);  
  61. if (!$this->smtp_sockopen($rcpt_to)) {  
  62. $this->log_write("error: cannot send email to ".$rcpt_to."n");  
  63. $sent = false;  
  64. continue;  
  65. }  
  66. if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) {  
  67. $this->log_write("e-mail has been sent to <".$rcpt_to.">;n");  
  68. else {  
  69. $this->log_write("error: cannot send email to <".$rcpt_to.">;n");  
  70. $sent = false;  
  71. }  
  72. fclose($this->sock);  
  73. $this->log_write("disconnected from remote hostn");  
  74. }  
  75. return $sent;  
  76. }  
  77. /* private functions */  
  78. function smtp_send($helo$from$to$header$body = "")  
  79. {  
  80. if (!$this->smtp_putcmd("helo"$helo)) {  
  81. return $this->smtp_error("sending helo command");  
  82. }  
  83. #auth  
  84. if($this->auth){  
  85. if (!$this->smtp_putcmd("auth login"base64_encode($this->user))) {  
  86. return $this->smtp_error("sending helo command");  
  87. }  
  88. if (!$this->smtp_putcmd(""base64_encode($this->pass))) {  
  89. return $this->smtp_error("sending helo command");  
  90. }  
  91. }  
  92. #  
  93. if (!$this->smtp_putcmd("mail""from:<".$from.">;")) {  
  94. return $this->smtp_error("sending mail from command");  
  95. }  
  96. if (!$this->smtp_putcmd("rcpt""to:<".$to.">;")) {  
  97. return $this->smtp_error("sending rcpt to command");  
  98. }  
  99. if (!$this->smtp_putcmd("data")) {  
  100. return $this->smtp_error("sending data command");  
  101. }  
  102. if (!$this->smtp_message($header$body)) {  
  103. return $this->smtp_error("sending message");  
  104. }  
  105. if (!$this->smtp_eom()) {  
  106. return $this->smtp_error("sending ;;.;; [eom]");  
  107. }  
  108. if (!$this->smtp_putcmd("quit")) {  
  109. return $this->smtp_error("sending quit command");  
  110. }  
  111. return true;  
  112. }  
  113. function smtp_sockopen($address)  
  114. {  
  115. if ($this->relay_host == "") {  
  116. return $this->smtp_sockopen_mx($address);  
  117. else {  
  118. return $this->smtp_sockopen_relay();  
  119. }  
  120. }  
  121. function smtp_sockopen_relay()  
  122. {  
  123. $this->log_write("trying to ".$this->relay_host.":".$this->smtp_port."n");  
  124. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out);  
  125. if (!($this->sock && $this->smtp_ok())) {  
  126. $this->log_write("error: cannot connenct to relay host ".$this->relay_host."n");  
  127. $this->log_write("error: ".$errstr." (".$errno.")n");  
  128. return false;  
  129. }  
  130. $this->log_write("connected to relay host ".$this->relay_host."n");  
  131. return true;  
  132. }  
  133. function smtp_sockopen_mx($address)  
  134. {  
  135. $domain = ereg_replace("^.+@([^@]+)$""1"$address);  
  136. if (!@getmxrr($domain$mxhosts)) {  
  137. $this->log_write("error: cannot resolve mx "".$domain.""n");  
  138. return false;  
  139. }  
  140. foreach ($mxhosts as $host) {  
  141. $this->log_write("trying to ".$host.":".$this->smtp_port."n");  
  142. $this->sock = @fsockopen($host$this->smtp_port, $errno$errstr$this->time_out);  
  143. if (!($this->sock && $this->smtp_ok())) {  
  144. $this->log_write("warning: cannot connect to mx host ".$host."n");  
  145. $this->log_write("error: ".$errstr." (".$errno.")n");  
  146. continue;  
  147. }  
  148. $this->log_write("connected to mx host ".$host."n");  
  149. return true;  
  150. }  
  151. $this->log_write("error: cannot connect to any mx hosts (".implode(", "$mxhosts).")n");  
  152. return false;  
  153. }  
  154. function smtp_message($header$body)  
  155. {  
  156. fputs($this->sock, $header."rn".$body);  
  157. $this->smtp_debug(">; ".str_replace("rn""n".">; "$header."n>; ".$body."n>; "));  
  158. return true;  
  159. }  
  160. function smtp_eom()  
  161. {  
  162. fputs($this->sock, "rn.rn");  
  163. $this->smtp_debug(". [eom]n");  
  164. return $this->smtp_ok();  
  165. }  
  166. function smtp_ok()  
  167. {  
  168. $response = str_replace("rn"""fgets($this->sock, 512));  
  169. $this->smtp_debug($response."n");  
  170. if (!ereg("^[23]"$response)) {  
  171. fputs($this->sock, "quitrn");  
  172. fgets($this->sock, 512);  
  173. $this->log_write("error: remote host returned "".$response.""n");  
  174. return false;  
  175. }  
  176. return true;  
  177. }  
  178. function smtp_putcmd($cmd$arg = "")  
  179. {  
  180. if ($arg != "") {  
  181. if($cmd==""$cmd = $arg;  
  182. else $cmd = $cmd." ".$arg;  
  183. }  
  184. fputs($this->sock, $cmd."rn");  
  185. $this->smtp_debug(">; ".$cmd."n");  
  186. return $this->smtp_ok();  
  187. }  
  188. function smtp_error($string)  
  189. {  
  190. $this->log_write("error: error occurred while ".$string.".n");  
  191. return false;  
  192. }  
  193. function log_write($message)  
  194. {  
  195. $this->smtp_debug($message);  
  196. if ($this->log_file == "") {  
  197. return true;  
  198. }  
  199. $message = date("m d h:i:s ").get_current_user()."[".getmypid()."]: ".$message;  
  200. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {  
  201. $this->smtp_debug("warning: cannot open log file "".$this->log_file.""n");  
  202. return false;  
  203. }  
  204. flock($fp, lock_ex);  
  205. fputs($fp$message);  
  206. fclose($fp);  
  207. return true;  
  208. }  
  209. function strip_comment($address)  
  210. {  
  211. $comment = "([^()]*)";  
  212. while (ereg($comment$address)) {  
  213. $address = ereg_replace($comment""$address);  
  214. }  
  215. return $address;  
  216. }  
  217. function get_address($address)  
  218. {  
  219. $address = ereg_replace("([ trn])+"""$address);  
  220. $address = ereg_replace("^.*<(.+)>;.*$""1"$address);  
  221. return $address;  
  222. }  
  223. function smtp_debug($message)  
  224. {  
  225. if ($this->debug) {  
  226. echo $message;  
  227. }  
  228. }  
  229. }  
  230. function sendmail($smtpserver,$smtpuser,$smtppass,$smtpemailto,$smtpusermail$mailsubject$mailbody){  
  231. $smtp = new smtp($smtpserver,25,true,$smtpuser,$smtppass);  
  232. //$smtp->debug = true;  
  233. $smtp->sendmail($smtpemailto$smtpusermail$mailsubject$mailbody"html");  
  234. }  
  235. //such as  
  236. //sendmail("smtp.126.com","test@126.com","password","1034555083@qq.com","test@126.com","title","body"); 
  237. ?> 
分享到:
php mail()邮件发送配置方法 - php高级应...
php mail()邮件发送配置方法 这两天写了个方法用到php mail()本以为php提供的这个方法非常简单、方便、易用,其实不然! php mail()函数需要配置才可以发邮件的哦,下面配置方法: 如果你没有使用windows,那么sendmail_path指令就是你唯一要担心的东西.如果你正在使用Windows,你就需要看看最...
php socket 使用smtp服务器发送邮件 - ph...
php socket 使用smtp服务器发送邮件 /*邮件发送类  *功能:php socket 使用smtp服务器发送邮件  *作者:longlong  *时间:2007-11-26  */  class smtp  {  /* 全局变量 */  var $smtp_port;  var $time_out;  var $host_name;  var $log_file;...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……