怎么利用php发送邮件求详细教程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么利用php发送邮件求详细教程相关的知识,希望对你有一定的参考价值。

参考技术A php虽然提供了mail()函数,但并不好用,而PHPMailer是一个不错的邮件发送工具,接下来将详细介绍,需要了解的朋友可以参考下:

本人使用wamp集成开发环境,Apache2.4.4, mysql5.6.12 , php5.4.12.开始的时候使用mail()发送邮件,更改配置始终无法成功,了解到mail()函数使用需要sendmail程序。又下载了sendmail程序扩展包。按照网上的说法也改好了php.ini和sendmail.ini。使用foxmail 7.1创建了自己的qq邮箱账户,开启了POP3/SMTP服务,更改发件服务器为POP3,使用和收件服务器相同的身份验证,结果还是报错:Warning: mail(): SMTP server response: 503 Error: need EHLO and AUTH first ! in F:\PHP\wamp\www\mail.php on line 8。以下是使用mail()函数发送邮件的php代码:

[php] view plain copy
<span style="font-size:14px"><?php

$to = "757006080@qq.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "757006080@qq.com";
$headers = "From: $from";
$send=mail($to,$subject,$message,$headers);
if($send)
echo "Mail Sent";
else
echo "Sorry,mail sent failed!"

?></span>
在CSDN论坛上发现phpmailer可以方便快捷的发送邮件,以下写出详细使用教程:
1.需要下载PHPMailer文件包,(点击打开链接)

2.确认你的服务器已经系统支持socket,通过phpinfo()查看是否支持socket;

3.把文件解压到你的WEB服务器目录下,就可以使用PHPMailer发送邮件了。

以下为前台表单php代码:

[php] view plain copy
<span style="font-size:14px"><html>
<body>
<h3>phpmailer Unit Test</h3>
请你输入<font color="#FF6666">收信</font>的邮箱地址:
<form name="phpmailer" action="testemail.php" method="post">
<input type="hidden" name="submitted" value="1"/>
邮箱地址: <input type="text" size="50" name="to" />
<br/>
<input type="submit" value="发送"/>
</form>
</body>
</html> </span>
以下为后台程序:

[php] view plain copy
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

header("content-type:text/html;charset=utf-8");

ini_set("magic_quotes_runtime",0);

require('class.phpmailer.php');

try
$mail = new PHPMailer(true); //New instance, with exceptions enabled

//$body = file_get_contents('contents.html');
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$to = $_POST['to'];
$mail->CharSet="GB2312";//设置邮件字符编码否则邮件会乱码
$mail->Encoding="base64";
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "smtp.qq.com"; // SMTP server
$mail->Username = "757006080@qq.com"; // SMTP server username
$mail->Password = "000000000000"; // SMTP server password

//$mail->IsSendmail(); // tell the class to use Sendmail

$mail->AddReplyTo("757006080@qq.com","han qing");

$mail->From = "757006080@qq.com";
$mail->FromName = "han qing";

//$to = "hanqing757@gmail.com";

$mail->AddAddress($to);

$mail->Subject =$mail->Subject = "=?utf-8?B?" . base64_encode("First PHPMailer Message") . "?=";

$mail->Body = "<h1>phpmailer演示</h1> 这是用PHPMAILER发的第一份邮件,从QQ邮箱发到Google邮箱.";

$mail->AddAttachment("F:/myloe.jpg");

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap

//$mail->MsgHTML($body);

$mail->IsHTML(true); // send as HTML

$mail->Send();
echo 'Message has been sent.';
catch (phpmailerException $e)
echo $e->errorMessage();

?>
参考技术B 这个还是百度吧,很简单的东西。我写过java版的。追问

你会php的么?

追答

不会,你搜到改改就行了。就是些基础的写法,没什么的.

参考技术C 亲测管用,参考下面的地址
http://www.daixiaorui.com/read/16.html

夹缝中求生存-在一无所有的php虚拟主机环境下利用smtp发送邮件

夹缝中求生存

利用php虚拟主机向指定QQ邮箱账户发送一份电子邮件,以163邮箱为例。

前言:在阿里云虚拟主机php的苛刻环境下,php函数库少的可怜,各种不支持,想要发送一份不被收件邮箱屏蔽的邮件,顿时感觉整个世界都黑暗了,不过还好,在夹缝中求生存,踩过了各种坑,最后还是有解决之道的。

  • 首先得注册163个人邮箱账号,配置SMTP并设置授权密码(此部分不再赘述,如有疑问请自行百度)。
  • 几点提醒:
    • 邮件发送成功却未收到邮件提醒:发送的邮件可能被放到了垃圾箱,尤其是注意账户为QQ邮箱的收件人。
    • 邮件内容不要太多,否则很有可能会被当成垃圾邮件处理(亲测发送整段html代码邮件会被QQ收件邮箱会屏蔽拒收邮件,而163收件邮箱会正常接收邮件),尤其是注意账户为QQ邮箱的收件人。
    • 以下是以163邮箱配置smpt,亲测代码可用,其他邮箱配置smpt未作尝试。
    • 已解决自定义发件人,代码中已写明。
    • 已解决当收件邮箱为QQ邮箱时,邮件会被QQ邮箱当作垃圾邮件处理的问题。
  • 以下是php代码:

test.php

 1 <?php
 2 header("Content-Type: text/html; charset=utf-8");
 3 
 4 //引入发送邮件类
 5 require("smtp.php");
 6 //使用163邮箱服务器
 7 $smtpserver = "smtp.163.com";
 8 //163邮箱服务器端口
 9 $smtpserverport = 25;
10 //你的163服务器邮箱账号
11 $smtpusermail = "****@163.com";//
12 //收件人邮箱,***@163.com/***@qq.com
13 $smtpemailto = "[email protected]";
14 //发送人,可随意显示发件人
15 $sender = "邮件发送方";
16 //你的邮箱账号(去掉@163.com)
17 $smtpuser = "****";//你的163邮箱去掉后面的163.com
18 //你的邮箱密码
19 $smtppass = "*****"; //你的163邮箱SMTP的授权码,千万不要填密码!!!
20 
21 //邮件主题
22 $mailsubject = "邮件提醒";
23 //邮件内容
24 $mailbody = "
25 详细信息:</br>
26 <h1>这是一封测试邮件</h1>
27 ";
28 //邮件格式(HTML/TXT),TXT为文本邮件
29 $mailtype = "HTML";
30 //这里面的一个true是表示使用身份验证,否则不使用身份验证.
31 $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
32 //是否显示发送的调试信息
33 $smtp->debug = TRUE;
34 //发送邮件
35 $smtp->sendmail($smtpemailto, $smtpusermail, $sender ,$mailsubject, $mailbody, $mailtype);
36   
37 ?>

smtp.php

  1 <?php
  2 header("Content-Type: text/html; charset=utf-8");
  3   
  4 class smtp
  5 {
  6  /* Public Variables */
  7  var $smtp_port;
  8  var $time_out;
  9  var $host_name;
 10  var $log_file;
 11  var $relay_host;
 12  var $debug;
 13  var $auth;
 14  var $user;
 15  var $pass;
 16   
 17  /* Private Variables */
 18  var $sock;
 19   
 20  /* Constractor */
 21  function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
 22  {
 23   $this->debug = FALSE;
 24   $this->smtp_port = $smtp_port;
 25   $this->relay_host = $relay_host;
 26   $this->time_out = 30; //is used in fsockopen()
 27   $this->auth = $auth;//auth
 28   $this->user = $user;
 29   $this->pass = $pass;
 30   $this->host_name = "localhost"; //is used in HELO command
 31   $this->log_file = "";
 32   $this->sock = FALSE;
 33 }
 34   
 35  /* Main Function */
 36  function sendmail($to, $from, $sender, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
 37  {
 38   $mail_from = $this->get_address($this->strip_comment($from));
 39   $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
 40   $header = "MIME-Version:1.0\r\n";
 41   if($mailtype=="HTML")
 42   {
 43    $header .= "Content-Type:text/html\r\n";
 44   }
 45   $header .= "To: ".$to."\r\n";
 46   if ($cc != "")
 47   {
 48    $header .= "Cc: ".$cc."\r\n";
 49   }
 50   $header .= "From: ".$sender."<".$from.">\r\n";
 51   $header .= "Subject: ".$subject."\r\n";
 52   $header .= $additional_headers;
 53   $header .= "Date: ".date("r")."\r\n";
 54   $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
 55   list($msec, $sec) = explode(" ", microtime());
 56   $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
 57   $TO = explode(",", $this->strip_comment($to));
 58   
 59   if ($cc != "")
 60   {
 61    $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
 62    }
 63   if ($bcc != "")
 64   {
 65    $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
 66   }
 67   $sent = TRUE;
 68   foreach ($TO as $rcpt_to)
 69   {
 70    $rcpt_to = $this->get_address($rcpt_to);
 71    if (!$this->smtp_sockopen($rcpt_to))
 72    {
 73     $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
 74     $sent = FALSE;
 75     continue;
 76    }
 77    if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
 78    {
 79     $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
 80    }
 81    else
 82    {
 83     $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
 84     $sent = FALSE;
 85    }
 86    fclose($this->sock);
 87    $this->log_write("Disconnected from remote host\n");
 88   }
 89   return $sent;
 90  }
 91   
 92  /* Private Functions */
 93  function smtp_send($helo, $from, $to, $header, $body = "")
 94  {
 95   if (!$this->smtp_putcmd("HELO", $helo))
 96   {
 97    return $this->smtp_error("sending HELO command");
 98   }
 99   
100   #auth
101   if($this->auth)
102   {
103    if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user)))
104    {
105     return $this->smtp_error("sending HELO command");
106    }
107    if (!$this->smtp_putcmd("", base64_encode($this->pass)))
108    {
109     return $this->smtp_error("sending HELO command");
110    }
111   }
112   if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
113   {
114    return $this->smtp_error("sending MAIL FROM command");
115   }
116   if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
117   {
118    return $this->smtp_error("sending RCPT TO command");
119   }
120   if (!$this->smtp_putcmd("DATA"))
121   {
122    return $this->smtp_error("sending DATA command");
123   }
124   if (!$this->smtp_message($header, $body))
125   {
126    return $this->smtp_error("sending message");
127   }
128   if (!$this->smtp_eom())
129   {
130    return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
131   }
132   if (!$this->smtp_putcmd("QUIT"))
133   {
134    return $this->smtp_error("sending QUIT command");
135   }
136   return TRUE;
137  }
138   
139  function smtp_sockopen($address)
140  {
141   if ($this->relay_host == "")
142   {
143    return $this->smtp_sockopen_mx($address);
144   }
145   else
146   {
147    return $this->smtp_sockopen_relay();
148   }
149  }
150   
151  function smtp_sockopen_relay()
152  {
153   $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
154   $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
155   if (!($this->sock && $this->smtp_ok()))
156   {
157    $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
158    $this->log_write("Error: ".$errstr." (".$errno.")\n");
159    return FALSE;
160   }
161   $this->log_write("Connected to relay host ".$this->relay_host."\n");
162   return TRUE;;
163  }
164   
165  function smtp_sockopen_mx($address)
166  {
167   $domain = preg_replace("^[email protected]([^@]+)$", "\1", $address);
168   if ([email protected]($domain, $MXHOSTS))
169   {
170    $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
171    return FALSE;
172   }
173   foreach ($MXHOSTS as $host)
174   {
175    $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
176    $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
177    if (!($this->sock && $this->smtp_ok()))
178    {
179     $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
180     $this->log_write("Error: ".$errstr." (".$errno.")\n");
181     continue;
182    }
183    $this->log_write("Connected to mx host ".$host."\n");
184    return TRUE;
185   }
186   $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
187   return FALSE;
188  }
189   
190  function smtp_message($header, $body)
191  {
192   fputs($this->sock, $header."\r\n".$body);
193   $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
194   return TRUE;
195  }
196   
197  function smtp_eom()
198  {
199   fputs($this->sock, "\r\n.\r\n");
200   $this->smtp_debug(". [EOM]\n");
201   return $this->smtp_ok();
202  }
203   
204  function smtp_ok()
205  {
206   $response = str_replace("\r\n", "", fgets($this->sock, 512));
207   $this->smtp_debug($response."\n");
208   if (!preg_match("/^[23]/", $response))
209   {
210    fputs($this->sock, "QUIT\r\n");
211    fgets($this->sock, 512);
212    $this->log_write("Error: Remote host returned \"".$response."\"\n");
213    return FALSE;
214   }
215   return TRUE;
216  }
217   
218  function smtp_putcmd($cmd, $arg = "")
219  {
220   if ($arg != "")
221   {
222    if($cmd=="")
223    {
224     $cmd = $arg;
225    }
226    else
227    {
228     $cmd = $cmd." ".$arg;
229    }
230   }
231   fputs($this->sock, $cmd."\r\n");
232   $this->smtp_debug("> ".$cmd."\n");
233   return $this->smtp_ok();
234  }
235   
236  function smtp_error($string)
237  {
238   $this->log_write("Error: Error occurred while ".$string.".\n");
239   return FALSE;
240  }
241   
242  function log_write($message)
243  {
244   $this->smtp_debug($message);
245   if ($this->log_file == "")
246   {
247    return TRUE;
248   }
249   $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
250   if ([email protected]file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
251   {
252    $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
253    return FALSE;;
254   }
255   flock($fp, LOCK_EX);
256   fputs($fp, $message);
257   fclose($fp);
258   return TRUE;
259  }
260   
261  function strip_comment($address)
262  {
263   $comment = "/\([^()]*\)/";
264   while (preg_match($comment, $address))
265   {
266    $address = preg_replace($comment, "", $address);
267   }
268   return $address;
269  }
270   
271  function get_address($address)
272  {
273   $address = preg_replace("/([ \t\r\n])+/", "", $address);
274   $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
275   return $address;
276  }
277   
278  function smtp_debug($message)
279  {
280   if ($this->debug)
281   {
282    echo $message;
283   }
284  }
285   
286 }
287 ?>

 

以上是关于怎么利用php发送邮件求详细教程的主要内容,如果未能解决你的问题,请参考以下文章

用python自动发送邮件详细教程

夹缝中求生存-在一无所有的php虚拟主机环境下利用smtp发送邮件

夹缝中求生存-在一无所有的php虚拟主机环境下利用smtp发送邮件

outlook怎么发送邮件

TP5实现邮件发送(PHP 利用QQ邮箱发送邮件「PHPMailer」)

PHP 利用 QQ 邮箱发送邮件「PHPMailer」