参考http://jingyan.baidu.com/album/7f766daf436b9b4101e1d0e2.html
smtp.php
<code><span><?</span>php class smtp { <span>/* Public Variables */</span><span>var</span><span>$smtp_port</span>; <span>var</span><span>$time_out</span>; <span>var</span><span>$host_name</span>; <span>var</span><span>$log_file</span>; <span>var</span><span>$relay_host</span>; <span>var</span><span>$debug</span>; <span>var</span><span>$auth</span>; <span>var</span><span>$user</span>; <span>var</span><span>$pass</span>; <span>/* Private Variables */</span><span>var</span><span>$sock</span>; <span>/* Constractor */</span> function smtp(<span>$relay_host</span><span>=</span><span>""</span>, <span>$smtp_port</span><span>=</span><span>25</span>,<span>$auth</span><span>=</span><span>false</span>,<span>$user</span>,<span>$pass</span>) { <span>$this</span><span>-></span>debug <span>=</span><span>FALSE</span>; <span>$this</span><span>-></span>smtp_port <span>=</span><span>$smtp_port</span>; <span>$this</span><span>-></span>relay_host <span>=</span><span>$relay_host</span>; <span>$this</span><span>-></span>time_out <span>=</span><span>30</span>; <span>//is used in fsockopen() </span><span>$this</span><span>-></span>auth <span>=</span><span>$auth</span>;<span>//auth </span><span>$this</span><span>-></span>user <span>=</span><span>$user</span>; <span>$this</span><span>-></span>pass <span>=</span><span>$pass</span>; <span>$this</span><span>-></span>host_name <span>=</span><span>"localhost"</span>; <span>//is used in HELO command </span><span>$this</span><span>-></span>log_file <span>=</span><span>""</span>; <span>$this</span><span>-></span>sock <span>=</span><span>FALSE</span>; } <span>/* Main Function */</span> function sendmail(<span>$to</span>, <span>$from</span>, <span>$subject</span><span>=</span><span>""</span>, <span>$body</span><span>=</span><span>""</span>, <span>$mailtype</span>, <span>$cc</span><span>=</span><span>""</span>, <span>$bcc</span><span>=</span><span>""</span>, <span>$additional_headers</span><span>=</span><span>""</span>) { <span>$mail_from</span><span>=</span><span>$this</span><span>-></span>get_address(<span>$this</span><span>-></span>strip_comment(<span>$from</span>)); <span>$body</span><span>=</span> ereg_replace(<span>"(^|(\r\n))(\.)"</span>, <span>"\1.\3"</span>, <span>$body</span>); <span>$header</span><span>.</span><span>=</span><span>"MIME-Version:1.0\r\n"</span>; <span>if</span>(<span>$mailtype</span><span>==</span><span>"HTML"</span>) { <span>$header</span><span>.</span><span>=</span><span>"Content-Type:text/html\r\n"</span>; } <span>$header</span><span>.</span><span>=</span><span>"To: "</span><span>.</span><span>$to.</span><span>"\r\n"</span>; <span>if</span> (<span>$cc</span><span>!=</span><span>""</span>) { <span>$header</span><span>.</span><span>=</span><span>"Cc: "</span><span>.</span><span>$cc.</span><span>"\r\n"</span>; } <span>$header</span><span>.</span><span>=</span><span>"From: $from<"</span><span>.</span><span>$from.</span><span>">\r\n"</span>; <span>$header</span><span>.</span><span>=</span><span>"Subject: "</span><span>.</span><span>$subject.</span><span>"\r\n"</span>; <span>$header</span><span>.</span><span>=</span><span>$additional_headers</span>; <span>$header</span><span>.</span><span>=</span><span>"Date: "</span><span>.</span><span>date</span>(<span>"r"</span>)<span>.</span><span>"\r\n"</span>; <span>$header</span><span>.</span><span>=</span><span>"X-Mailer:By Redhat (PHP/"</span><span>.</span>phpversion()<span>.</span><span>")\r\n"</span>; <span>list</span>(<span>$msec</span>, <span>$sec</span>) <span>=</span> explode(<span>" "</span>, microtime()); <span>$header</span><span>.</span><span>=</span><span>"Message-ID: <"</span><span>.</span><span>date</span>(<span>"YmdHis"</span>, <span>$sec</span>)<span>.</span><span>"."</span><span>.</span>(<span>$msec</span><span>*</span><span>1000000</span>)<span>.</span><span>"."</span><span>.</span><span>$mail_from.</span><span>">\r\n"</span>; <span>$TO</span><span>=</span> explode(<span>","</span>, <span>$this</span><span>-></span>strip_comment(<span>$to</span>)); <span>if</span> (<span>$cc</span><span>!=</span><span>""</span>) { <span>$TO</span><span>=</span> array_merge(<span>$TO</span>, explode(<span>","</span>, <span>$this</span><span>-></span>strip_comment(<span>$cc</span>))); } <span>if</span> (<span>$bcc</span><span>!=</span><span>""</span>) { <span>$TO</span><span>=</span> array_merge(<span>$TO</span>, explode(<span>","</span>, <span>$this</span><span>-></span>strip_comment(<span>$bcc</span>))); } <span>$sent</span><span>=</span><span>TRUE</span>; foreach (<span>$TO</span> as <span>$rcpt_to</span>) { <span>$rcpt_to</span><span>=</span><span>$this</span><span>-></span>get_address(<span>$rcpt_to</span>); <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_sockopen(<span>$rcpt_to</span>)) { <span>$this</span><span>-></span>log_write(<span>"Error: Cannot send email to "</span><span>.</span><span>$rcpt_to.</span><span>"\n"</span>); <span>$sent</span><span>=</span><span>FALSE</span>; continue; } <span>if</span> (<span>$this</span><span>-></span>smtp_send(<span>$this</span><span>-></span>host_name, <span>$mail_from</span>, <span>$rcpt_to</span>, <span>$header</span>, <span>$body</span>)) { <span>$this</span><span>-></span>log_write(<span>"E-mail has been sent to <"</span><span>.</span><span>$rcpt_to.</span><span>">\n"</span>); } <span>else</span> { <span>$this</span><span>-></span>log_write(<span>"Error: Cannot send email to <"</span><span>.</span><span>$rcpt_to.</span><span>">\n"</span>); <span>$sent</span><span>=</span><span>FALSE</span>; } fclose(<span>$this</span><span>-></span>sock); <span>$this</span><span>-></span>log_write(<span>"Disconnected from remote host\n"</span>); } <span>return</span><span>$sent</span>; } <span>/* Private Functions */</span> function smtp_send(<span>$helo</span>, <span>$from</span>, <span>$to</span>, <span>$header</span>, <span>$body</span><span>=</span><span>""</span>) { <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>"HELO"</span>, <span>$helo</span>)) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending HELO command"</span>); } <span>#auth</span><span>if</span>(<span>$this</span><span>-></span>auth) { <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>"AUTH LOGIN"</span>, base64_encode(<span>$this</span><span>-></span>user))) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending HELO command"</span>); } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>""</span>, base64_encode(<span>$this</span><span>-></span>pass))) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending HELO command"</span>); } } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>"MAIL"</span>, <span>"FROM:<"</span><span>.</span><span>$from.</span><span>">"</span>)) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending MAIL FROM command"</span>); } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>"RCPT"</span>, <span>"TO:<"</span><span>.</span><span>$to.</span><span>">"</span>)) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending RCPT TO command"</span>); } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>"DATA"</span>)) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending DATA command"</span>); } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_message(<span>$header</span>, <span>$body</span>)) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending message"</span>); } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_eom()) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending <CR><LF>.<CR><LF> [EOM]"</span>); } <span>if</span> (<span>!</span><span>$this</span><span>-></span>smtp_putcmd(<span>"QUIT"</span>)) { <span>return</span><span>$this</span><span>-></span>smtp_error(<span>"sending QUIT command"</span>); } <span>return</span><span>TRUE</span>; } function smtp_sockopen(<span>$address</span>) { <span>if</span> (<span>$this</span><span>-></span>relay_host <span>==</span><span>""</span>) { <span>return</span><span>$this</span><span>-></span>smtp_sockopen_mx(<span>$address</span>); } <span>else</span> { <span>return</span><span>$this</span><span>-></span>smtp_sockopen_relay(); } } function smtp_sockopen_relay() { <span>$this</span><span>-></span>log_write(<span>"Trying to "</span><span>.</span><span>$this</span><span>-></span>relay_host<span>.</span><span>":"</span><span>.</span><span>$this</span><span>-></span>smtp_port<span>.</span><span>"\n"</span>); <span>$this</span><span>-></span>sock <span>=</span> @fsockopen(<span>$this</span><span>-></span>relay_host, <span>$this</span><span>-></span>smtp_port, <span>$errno</span>, <span>$errstr</span>, <span>$this</span><span>-></span>time_out); <span>if</span> (<span>!</span>(<span>$this</span><span>-></span>sock <span>&&</span><span>$this</span><span>-></span>smtp_ok())) { <span>$this</span><span>-></span>log_write(<span>"Error: Cannot connenct to relay host "</span><span>.</span><span>$this</span><span>-></span>relay_host<span>.</span><span>"\n"</span>); <span>$this</span><span>-></span>log_write(<span>"Error: "</span><span>.</span><span>$errstr.</span><span>" ("</span><span>.</span><span>$errno.</span><span>")\n"</span>); <span>return</span><span>FALSE</span>; } <span>$this</span><span>-></span>log_write(<span>"Connected to relay host "</span><span>.</span><span>$this</span><span>-></span>relay_host<span>.</span><span>"\n"</span>); <span>return</span><span>TRUE</span>;; } function smtp_sockopen_mx(<span>$address</span>) { <span>$domain</span><span>=</span> ereg_replace(<span>"^.+@([^@]+)$"</span>, <span>"\1"</span>, <span>$address</span>); <span>if</span> (<span>!</span>@getmxrr(<span>$domain</span>, <span>$MXHOSTS</span>)) { <span>$this</span><span>-></span>log_write(<span>"Error: Cannot resolve MX \""</span><span>.</span><span>$domain.</span><span>"\"\n"</span>); <span>return</span><span>FALSE</span>; } foreach (<span>$MXHOSTS</span> as <span>$host</span>) { <span>$this</span><span>-></span>log_write(<span>"Trying to "</span><span>.</span><span>$host.</span><span>":"</span><span>.</span><span>$this</span><span>-></span>smtp_port<span>.</span><span>"\n"</span>); <span>$this</span><span>-></span>sock <span>=</span> @fsockopen(<span>$host</span>, <span>$this</span><span>-></span>smtp_port, <span>$errno</span>, <span>$errstr</span>, <span>$this</span><span>-></span>time_out); <span>if</span> (<span>!</span>(<span>$this</span><span>-></span>sock <span>&&</span><span>$this</span><span>-></span>smtp_ok())) { <span>$this</span><span>-></span>log_write(<span>"Warning: Cannot connect to mx host "</span><span>.</span><span>$host.</span><span>"\n"</span>); <span>$this</span><span>-></span>log_write(<span>"Error: "</span><span>.</span><span>$errstr.</span><span>" ("</span><span>.</span><span>$errno.</span><span>")\n"</span>); continue; } <span>$this</span><span>-></span>log_write(<span>"Connected to mx host "</span><span>.</span><span>$host.</span><span>"\n"</span>); <span>return</span><span>TRUE</span>; } <span>$this</span><span>-></span>log_write(<span>"Error: Cannot connect to any mx hosts ("</span><span>.</span>implode(<span>", "</span>, <span>$MXHOSTS</span>)<span>.</span><span>")\n"</span>); <span>return</span><span>FALSE</span>; } function smtp_message(<span>$header</span>, <span>$body</span>) { fputs(<span>$this</span><span>-></span>sock, <span>$header.</span><span>"\r\n"</span><span>.</span><span>$body</span>); <span>$this</span><span>-></span>smtp_debug(<span>"> "</span><span>.</span>str_replace(<span>"\r\n"</span>, <span>"\n"</span><span>.</span><span>"> "</span>, <span>$header.</span><span>"\n> "</span><span>.</span><span>$body.</span><span>"\n> "</span>)); <span>return</span><span>TRUE</span>; } function smtp_eom() { fputs(<span>$this</span><span>-></span>sock, <span>"\r\n.\r\n"</span>); <span>$this</span><span>-></span>smtp_debug(<span>". [EOM]\n"</span>); <span>return</span><span>$this</span><span>-></span>smtp_ok(); } function smtp_ok() { <span>$response</span><span>=</span> str_replace(<span>"\r\n"</span>, <span>""</span>, fgets(<span>$this</span><span>-></span>sock, <span>512</span>)); <span>$this</span><span>-></span>smtp_debug(<span>$response.</span><span>"\n"</span>); <span>if</span> (<span>!</span>ereg(<span>"^[23]"</span>, <span>$response</span>)) { fputs(<span>$this</span><span>-></span>sock, <span>"QUIT\r\n"</span>); fgets(<span>$this</span><span>-></span>sock, <span>512</span>); <span>$this</span><span>-></span>log_write(<span>"Error: Remote host returned \""</span><span>.</span><span>$response.</span><span>"\"\n"</span>); <span>return</span><span>FALSE</span>; } <span>return</span><span>TRUE</span>; } function smtp_putcmd(<span>$cmd</span>, <span>$arg</span><span>=</span><span>""</span>) { <span>if</span> (<span>$arg</span><span>!=</span><span>""</span>) { <span>if</span>(<span>$cmd</span><span>==</span><span>""</span>) { <span>$cmd</span><span>=</span><span>$arg</span>; } <span>else</span> { <span>$cmd</span><span>=</span><span>$cmd.</span><span>" "</span><span>.</span><span>$arg</span>; } } fputs(<span>$this</span><span>-></span>sock, <span>$cmd.</span><span>"\r\n"</span>); <span>$this</span><span>-></span>smtp_debug(<span>"> "</span><span>.</span><span>$cmd.</span><span>"\n"</span>); <span>return</span><span>$this</span><span>-></span>smtp_ok(); } function smtp_error(<span>$string</span>) { <span>$this</span><span>-></span>log_write(<span>"Error: Error occurred while "</span><span>.</span><span>$string.</span><span>".\n"</span>); <span>return</span><span>FALSE</span>; } function log_write(<span>$message</span>) { <span>$this</span><span>-></span>smtp_debug(<span>$message</span>); <span>if</span> (<span>$this</span><span>-></span>log_file <span>==</span><span>""</span>) { <span>return</span><span>TRUE</span>; } <span>$message</span><span>=</span><span>date</span>(<span>"M d H:i:s "</span>)<span>.</span>get_current_user()<span>.</span><span>"["</span><span>.</span>getmypid()<span>.</span><span>"]: "</span><span>.</span><span>$message</span>; <span>if</span> (<span>!</span>@file_exists(<span>$this</span><span>-></span>log_file) <span>||</span><span>!</span>(<span>$fp</span><span>=</span> @fopen(<span>$this</span><span>-></span>log_file, <span>"a"</span>))) { <span>$this</span><span>-></span>smtp_debug(<span>"Warning: Cannot open log file \""</span><span>.</span><span>$this</span><span>-></span>log_file<span>.</span><span>"\"\n"</span>); <span>return</span><span>FALSE</span>;; } flock(<span>$fp</span>, LOCK_EX); fputs(<span>$fp</span>, <span>$message</span>); fclose(<span>$fp</span>); <span>return</span><span>TRUE</span>; } function strip_comment(<span>$address</span>) { <span>$comment</span><span>=</span><span>"\([^()]*\)"</span>; <span>while</span> (ereg(<span>$comment</span>, <span>$address</span>)) { <span>$address</span><span>=</span> ereg_replace(<span>$comment</span>, <span>""</span>, <span>$address</span>); } <span>return</span><span>$address</span>; } function get_address(<span>$address</span>) { <span>$address</span><span>=</span> ereg_replace(<span>"([ \t\r\n])+"</span>, <span>""</span>, <span>$address</span>); <span>$address</span><span>=</span> ereg_replace(<span>"^.*<(.+)>.*$"</span>, <span>"\1"</span>, <span>$address</span>); <span>return</span><span>$address</span>; } function smtp_debug(<span>$message</span>) { <span>if</span> (<span>$this</span><span>-></span>debug) { echo <span>$message</span>; } } } <span>?></span><span></span></code>
test.php
<code><span><?</span>php <span>//引入<strong>发送邮件</strong>类</span><span><strong>require</strong></span>(<span>"smtp.php"</span>); <span>//使用163邮箱服务器</span><span>$smtpserver</span><span>=</span><span>"smtp.163.com"</span>; <span>//163邮箱服务器端口</span><span>$smtpserverport</span><span>=</span><span>25</span>; <span>//你的邮箱账号(与你的163服务器邮箱账号相比 去掉@163.com)</span><span>$smtpuser</span><span>=</span><span>"XXX"</span>;<span>//SMTP服务器的用户帐号</span><span>//你的邮箱密码</span><span>$smtppass</span><span>=</span><span>"YYY"</span>; <span>//SMTP服务器的用户密码</span><span>//这里面的一个true是表示使用身份验证,否则不使用身份验证.</span><span>$smtp</span><span>=</span><span>new</span> smtp(<span>$smtpserver</span>, <span>$smtpserverport</span>, <span>true</span>, <span>$smtpuser</span>, <span>$smtppass</span>); <span>//是否显示发送的调试信息</span><span>$smtp</span><span>-></span>debug <span>=</span><span>TRUE</span>; <span>//<strong>发送邮件</strong></span><span>//你的163服务器邮箱账号 $smtpusermail = $smtpuser."@163.com";</span><span>$smtpusermail</span><span>=</span><span>"XXX@163.com"</span>; <span>//收件人邮箱</span><span>$smtpemailto</span><span>=</span><span>"ZZZ@qq.com"</span>; <span>//邮件主题</span><span>$mailsubject</span><span>=</span><span>"subject"</span>; <span>//邮件内容</span><span>$mailbody</span><span>=</span><span>"PHP+MySQL"</span>; <span>//邮件格式(HTML/TXT),TXT为文本邮件</span><span>$mailtype</span><span>=</span><span>"TXT"</span>; <span>$smtp</span><span>-></span>sendmail(<span>$smtpemailto</span>, <span>$smtpusermail</span>, <span>$mailsubject</span>, <span>$mailbody</span>, <span>$mailtype</span>);</code>
以上就介绍了php 发送 email,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。