本文适用于 windows用户,linux版本后续会补上
邮件发送链接去重置密码等操作很常见,之前我使用的phpmailer去送法邮件,phpmailer的特点很明显,简单、易操作,但是有一个值得注意的地方是phpmailer配置中使用的密码不是登录密码,而是 smtp 的独立密码,又称授权码, 下面问题来了,因为公司刚搭建的outlook邮箱不支持smtp授权码,听说原因是因为stmp授权码是要硬件支持,这就导致phpmailer不能使用,不得已只能使用php自带的mail函数去发送邮件,废话不多说了。
第一步:先去下载sendmail 地址:http://download.csdn.net/detail/qq_33237412/9775176
第二步:将sendmail.zip解压,我是解压到 C:\wamp 下
第三步:修改php.ini文件,将SMTP、sendmail_from、smtp_port 这三行注释掉,配置sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
具体修改如下:
[mail function]
; For Win32 only.
; http://php.net/smtp
; SMTP =
; http://php.net/smtp-port
; smtp_port =
; For Win32 only.
; http://php.net/smtp
; SMTP =
; http://php.net/smtp-port
; smtp_port =
; For Win32 only.
; http://php.net/sendmail-from
; sendmail_from =
; http://php.net/sendmail-from
; sendmail_from =
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
; http://php.net/sendmail-path
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On
mail.add_x_header = On
; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on NT, not valid in Windows 95).
;mail.log = syslog
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on NT, not valid in Windows 95).
;mail.log = syslog
第四步:修改sendmail文件中的sendmail.ini文件
smtp_server=smtp.demo.com.cn
; smtp port (normally 25)
smtp_port=25 端口
; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
smtp_ssl=TLS
; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
;default_domain=mydomain.com
; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging
; uncomment to enable logging
error_logfile=error.log
; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging
; uncomment to enable debugging
;debug_logfile=debug.log
; if your smtp server requires authentication, modify the following two lines
auth_username=登录名
auth_password=登录密码
auth_password=登录密码
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines. do not enable unless it is required.
; following three lines. do not enable unless it is required.
pop3_server=
pop3_username=
pop3_password=
pop3_username=
pop3_password=
; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
force_sender= demo@demo.com.cn 这边是发件人的邮箱
下面就可以发送邮件了
<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// 更多报头
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
?>
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// 更多报头
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>