phpemail
㈠ php发邮件需要什么东西
PHP mail() 函数
PHP mail() 函数用于从脚本中发送电子邮件。
语法
mail(to,subject,message,headers,parameters)
参数 描述
to 必需。规定 email 接收者。
subject 必需。规定 email 的主题。注释:该参数不能包含任何新行字符。
message 必需。定义要发送的消息。应使用 LF (\n) 来分隔各行。
headers
可选。规定附加的标题,比如 From、Cc 以及 Bcc。
应当使用 CRLF (\r\n) 分隔附加的标题。
parameters 可选。对邮件发送程序规定额外的参数。
注释:PHP 需要一个已安装且正在运行的邮件系统,以便使邮件函数可用。所用的程序通过在 php.ini 文件中的配置设置进行定义。请在我们的 PHP Mail 参考手册阅读更多内容。
PHP 简易 E-Mail
通过 PHP 发送电子邮件的最简单的方式是发送一封文本 email。
㈡ 怎么利用php发送邮件求详细教程
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
<span style="font-size:14px"><?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$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
<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
<?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 = "[email protected]"; // SMTP server username
$mail->Password = "000000000000"; // SMTP server password
//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("[email protected]","han qing");
$mail->From = "[email protected]";
$mail->FromName = "han qing";
//$to = "[email protected]";
$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();
}
?>
㈢ 如何通过php发送邮件php的mail函数不能用!
支持mail的服务器 一般都是linux的 国内的好像不多
自己的电脑安装mail服务器不能往外发的 呵呵 可以自己测试用
现在很多管理系统都是用fsocketopen方式连接邮件服务器并发送邮件的 可以使用163 126的邮箱 网上有一些模型的 就像是好多管理系统后台让填入用户名和密码 就能群发一样 如果你不介意的话 给你转发一个以前我自己改过的可以利用fsocketopen方式群发或者单发email的一共三个文件
MailClass.php 》》》》》》
<?php
class Smtp
{
var $host; //主机
var $port; //端口 一般为25
var $user; //SMTP认证的帐号
var $pass; //认证密码
var $debug = false; //是否显示和服务器会话信息?
var $conn;
var $result_str; //结果
var $in; //客户机发送的命令
var $from; //收件人收到邮件显示的源信箱
var $email; //真实的地址
var $to; //目标信箱
var $subject; //主题
var $body; //内容
var $error;
var $All;
function Smtp($array)
{
$this->host = $array['host'];
$this->port = $array['port'];
$this->email= $array['trueemail'];
$this->from = $array['from'];
$this->user = base64_encode($array['username']);
$this->pass = base64_encode($array['password']);
$this->debug = $array['debug'];
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if($this->socket){
$this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
die("初始化失败,请检查您的网络连接和参数");
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn){
$this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
die("初始化失败,请检查您的网络连接和参数");
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
function debug_show($str)
{
if($this->debug)
{
echo $str."<p>\r\n";
}
}
function setmail($to,$subject,$body){
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
$All ="Content-type:text/html;charset=gb2312\r\n"; //邮件的编码方式可以根据自己的需要改
$All .= "From:".$this->from."\r\n";
$All .= "To:".$this->to."\r\n";
$All .= "Subject:".$this->subject."\r\n\r\n";
$All .= $this->body;
$this->All = $All;
}
/**
* 发送邮件部分
* 接收邮箱数组
*/
function send($toarray,$subject,$body)
{
//以下是和服务器会话
$this->in = "EHLO HELO\r\n";
$this->docommand();
$this->in = "AUTH LOGIN\r\n";
$this->docommand();
$this->in = $this->user."\r\n";
$this->docommand();
$this->in = $this->pass."\r\n";
$this->docommand();
foreach( $toarray as $to ) {
$this -> setmail($to,$subject,$body);
$this->in = "RSET\r\n";
$this->docommand();
$this->in = "MAIL FROM:<".$this->email.">\r\n";
$this->docommand();
$this->in = "RCPT TO:<".$this->to.">\r\n";
$this->docommand();
$this->in = "DATA\r\n";
$this->docommand();
$this->in = $this->All."\r\n.\r\n";
$this->docommand();
}
$this->in = "QUIT\r\n";
$this->docommand();
//结束,关闭连接
}
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("Client Action:".$this->in);
$this->result_str = "Server:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
}
?>
MailConfig.inc.php 》》》》》》
<?php
$mailconfig['host'] = "smtp.126.com"; //主机
$mailconfig['port'] = "25"; //端口 一般为25
$mailconfig['trueemail'] = "[email protected]"; //真实的地址
$mailconfig['username'] = "mhz1600"; //SMTP认证的帐号
$mailconfig['password'] = "*****"; //改成自己的
$mailconfig['debug'] = false; //是否显示和服务器会话信息?
$mailconfig['from'] = "[email protected]"; //显示给用户的发件人
include_once "MailClass.php";
set_time_limit(180);
?>
SendDemo.php 》》》》》》
<?php
include_once "MailConfig.inc.php";
//简单的临时码验证 当前时间(到小时)的验证码
//if( empty($_GET['s']) || $_GET['s'] != md5(date('Y-m-d-H',time())) ) {header("http/1.1 404"); die('');}
//发送email
if( isset($_POST['sendmail']) ) {
if( isset($_POST['from']) ) $mailconfig['from'] = $_POST['from'];
$smtp = new Smtp($mailconfig);
$title = $_POST['title'];
//获取post的email正文
if( get_magic_quotes_gpc() ) $message = $_POST['message'];
else $message = addslashes($_POST['message']);
//从email列表/文档中分离出所有的email地址
$pregstr = "@[a-zA-Z0-9\_][0-9a-zA-Z\.\-\_]+\@[0-aA-Za-z\-\_]+\.[0-9a-zA-Z\.\-\_]+@is";
$temp = array();
preg_match_all($pregstr,$_POST['emails'],$temp);
$toarray = $temp[0];
//var_mp($toarray);
$smtp->send($toarray,$title,$message);
die("操作完成!<A href=".$_SERVER['PHP_SELF']."?s=".md5(date('Y-m-d-H',time())).">继续发送其他</a> <a href=# onclick=window.close()>关闭</a>");
}
else {
if( isset($_POST['emails']) ) {
if( is_array($_POST['emails']) )
$emails = implode("\t",$_POST['emails']);
else
$emails = $_POST['emails'];
}
else $emails = "";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
-->
</style></head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="600" border="1" align="center" cellpadding="3" cellspacing="0" bordercolordark="#FFFFFF" bordercolorlight="#eeeeee">
<tr>
<td width="66">发件人:</td>
<td width="516"><input name="from" type="text" value="<?php echo $mailconfig['from']; ?>"> 可以直接修改mailconfig文件中的email</td>
</tr>
<tr>
<td>邮件标题:</td>
<td><input name="title" type="text" value="邮件群发测试标题!" size="60"></td>
</tr>
<tr>
<td>收件人:<br></td>
<td><textarea name="emails" cols="60" rows="5"><?php echo $emails; ?></textarea></td>
</tr>
<tr>
<td>邮件正文:<br>
【html】</td>
<td><textarea name="message" cols="60" rows="10">邮件群发测试!谢谢~!</textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sendmail" value=" 发送邮件 "> </td>
</tr>
</table>
</form>
<?
}
?>
</body>
</html>
使用方式 运行senddemo.php就行 确定本地或者服务器开启了fsocketopen支持 在输入框可以多种格式的的输入很多email 程序用正则表达式匹配出所有的email地址 通过服务器循环对话的方式不断的发送邮件 看看那个demo的流程就明白了
【郑重声明:mailclass修改自网上的模型 其他本人原创,版权不究 欢迎分享】
+---------------------广告-------------------------+
那一天:回忆,让生活更美好
独享人生中那个特别的日子,记录从那一天开始的幸福
http://www.nayitian.net
期待您的加入,欢迎提供宝贵的意见建议
+--------------------------------------------------+
+--------------------补充--------------------+
发送邮件的服务器(smtp)并不是网址 126发送邮件的服务器是 smtp.126.com 网易163的发送邮件服务器是 smtp.163.com 所有邮箱对于这个都有说明的 还有一个就是能够使用这个功能的好象新注册的邮箱不太好用 因为网易在2006年10对邮箱进行过调整 在此之前注册的都没问题 在这之后注册的好像开通一些其他的功能并且使用了一段时间才行的
smtp服务器的链接可以在命令提示行下测试 就是使用上面的命令:
首先 telnet smtp.126.com 25
因为smtp使用的25端口提供服务的 然后就会看到
220 126.com Anti-spam GT for Coremail System (126com[071018])
输入 EHLO HELO
服务器返回
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
输入 AUTH LOGIN
服务器返回
334 dXNlcm5hbWU6
然后再输入通过base64加密的用户名和密码 就能通过命令来和服务器对话 包括发送邮件等功能
具体的如果有兴趣更多命令自己查一下
这个php的程序就是模拟这个功能来实现的
如果你用telnet直接连不上的话 说明服务器是错误的 。。
㈣ php如何发送邮件
你好,用这个邮件类,需要在调用时,填写一个smtp服务器和你的用户名密码。
<?php
set_time_limit(600);
/*
* 邮件发送类
*/
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $smtp = "";
//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
var $check = 1;
//您的email帐号名称
var $username = "";
//您的email密码
var $password = "";
//此email 必需是发信服务器上的email
var $s_from = "";
/*
* 功能:发信初始化设置
* $from 你的发信服务器上的邮箱
* $password 你的邮箱密码
* $smtp 您的SMTP 服务器供应商,可以是域名或IP地址
* $check SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证
*/
function smail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/", $from ) ) {
$this->username = substr( $from, 0, strpos( $from , "@" ) );
$this->password = $password;
$this->smtp = $smtp ? $smtp : $this->smtp;
$this->check = $check;
$this->s_from = $from;
}
}
/*
* 功能:发送邮件
* $to 目标邮箱
* $from 来源邮箱
* $subject 邮件标题
* $message 邮件内容
*/
function send ( $to, $from, $subject, $message ) {
//连接服务器
$fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "联接服务器失败".__LINE__;
set_socket_blocking($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__;
//HELO
$yourname = "YOURNAME";
if($this->check == "1") $lastact="EHLO ".$yourname."\r\n";
else $lastact="HELO ".$yourname."\r\n";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) )
break;
}
//身份验证
if ($this->check=="1") {
//验证开始
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($this->username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($this->password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: <". $to ."> \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理From头
$head="From: $from\r\n";
$message = $head.$message;
//处理To头
$head="To: $to\r\n";
$message = $head.$message;
//加上结束串
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="QUIT\r\n";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
// 发送示例
// 只需要把这部分改成你的信息就行
$sm = new smail( "用户名", "密码", "发件smtp服务器" );
$end = $sm->send( "收件人", "发件人(可以伪造哦)", "标题", "内容" );
if( $end ) echo $end;
else echo "发送成功!$x";
?>
㈤ php如何接收发件箱邮件
php接收邮件举例:
<?php
$server = "{bjmail.*.com/pop3}"; //邮件服务器
$mailbox = "inbox"; //收件箱
$mailaccount="zhao**";//用户名
$mailpasswd=" "; //密码
$stream = @imap_open($server.$mailbox,$mailaccount,$mailpasswd);//打开IMAP 连结
$mail_number = imap_num_msg($stream);//信件的个数
if($mail_number < 1) { echo "No Message for $email"; }//如果信件数为0,显示信息
for($i=$mail_number;$i>=$mail_number;$i--)
{
$headers = @imap_header($stream, $i);
$mail_header= imap_headerinfo($stream, $i);//邮件头部
//var_mp ($mail_header);
$subject = $mail_header->subject;//邮件标题
$subject=decode_mime($subject);
echo $subject;
//编码为简体中文的标题的处理方法
// if(stristr($subject, "=?gb2312"))
// { //编码为简体中文的标题
// $subject=substr($subject,11);
// $subject=substr($subject,0,-2);
// $subject = base64_decode($subject);
// }
echo $from = $mail_header->fromaddress;//发件人
echo $date = $mail_header->date;//日期
$body = imap_fetchbody($stream, $i, 1);
$body = imap_base64($body);
$body = nl2br($body);
echo $body;
// $body = imap_qprint($body);
// echo $body;
// $body = imap_binary($body);
// $body = imap_base64($body);
//echo $body;
}
//对mime解码
function decode_mime($string)
{
$pos = strpos($string,'=?');
if (!is_int($pos)) {
return $string;
}
$preceding = substr($string, 0, $pos); // save any preceding text
$search = substr($string, $pos+2); /* the mime header spec says this is the longest a single encoded Word can be */
$d1 = strpos($search, '?');
if (!is_int($d1)) {
return $string;
}
$charset = substr($string, $pos+2, $d1); //取出字符集的定义部分
$search = substr($search, $d1+1); //字符集定义以后的部分=>$search;
$d2 = strpos($search, '?');
if (!is_int($d2)) {
return $string;
}
$encoding = substr($search, 0, $d2); ////两个?之间的部分编码方式:q或b
$search = substr($search, $d2+1);
$end = strpos($search, '?='); //$d2+1 与 $end 之间是编码了的内容:=> $endcoded_text;
if (!is_int($end)) {
return $string;
}
$encoded_text = substr($search, 0, $end);
$rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6)); //+6 是前面去掉的=????=六个字符
switch ($encoding) {
case 'Q':
case 'q':
//$encoded_text = str_replace('_', '%20', $encoded_text);
//$encoded_text = str_replace('=', '%', $encoded_text);
//$decoded = urldecode($encoded_text);
$decoded=quoted_printable_decode($encoded_text);
if (strtolower($charset) == 'windows-1251') {
$decoded = convert_cyr_string($decoded, 'w', 'k');
}
break;
case 'B':
case 'b':
$decoded = base64_decode($encoded_text);
if (strtolower($charset) == 'windows-1251') {
$decoded = convert_cyr_string($decoded, 'w', 'k');
}
break;
default:
$decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?=';
break;
}
return $preceding . $decoded .decode_mime($rest);
//return $preceding . $decoded . $this->decode_mime($rest);
}
㈥ 使用php 怎么发送邮件
你这个是连接的邮件服务器出错了。
估计你本地应该没有装邮件服务器吧,一般都会用第三方的邮件服务器,如smtp.163.com,
去下载个phpmailer,从里面把class.phpmailer.php提取出来,用很好用的。
㈦ php编程---email功能
*/
set_time_limit(120);
class smtp_mail
{
var $host; //主机
var $port; //端口 一般为25
var $user; //SMTP认证的帐号
var $pass; //认证密码
var $debug = false; //是否显示和服务器会话信息?
var $conn;
var $result_str; //结果
var $in; //客户机发送的命令
var $from; //源信箱
var $to; //目标信箱
var $subject; //主题
var $body; //内容
function smtp_mail($host,$port,$user,$pass,$debug=false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->debug = $debug;
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //具体用法请参考手册
if($this->socket)
{
$this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("初始化失败,请检查您的网络连接和参数");
}
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn)
{
$this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("初始化失败,请检查您的网络连接和参数");
}
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
function debug_show($str)
{
if($this->debug)
{
echo $str."<p>\r\n";
}
}
function send($from,$to,$subject,$body)
{
if($from == "" ¦ ¦ $to == "")
{
exit("请输入信箱地址");
}
if($subject == "") $sebject = "无标题";
if($body == "") $body = "无内容";
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
$All = "From:".$this->from."\n";
$All .= "To:".$this->to."\n";
$All .= "Subject:".$this->subject."\n";
$All .= $this->body;
/*
如过把$All的内容再加处理,就可以实现发送MIME邮件了
不过还需要加很多程序
*/
//以下是和服务器会话
$this->in = "EHLO HELO\r\n";
$this->docommand();
$this->in = "AUTH LOGIN\r\n";
$this->docommand();
$this->in = $this->user."\r\n";
$this->docommand();
$this->in = $this->pass."\r\n";
$this->docommand();
$this->in = "MAIL FROM:".$this->from."\r\n";
$this->docommand();
$this->in = "RCPT TO:".$this->to."\r\n";
$this->docommand();
$this->in = "DATA\r\n";
$this->docommand();
$this->in = $All."\r\n.\r\n";
$this->docommand();
$this->in = "QUIT\r\n";
$this->docommand();
//结束,关闭连接
}
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("客户机命令:".$this->in);
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
}
}
//这个是我做的测试,我用的是smtp.163.com,那你的信箱也必须是163.com的,要不人家不让你发!!
//你用这个类的时候你修改成你自己的信箱就可以了
$smtp = new smtp_mail("smtp.163.com","25","你的163.com的帐号","你的密码");
//如果你需要显示会话信息,请将上面的修改成
//$smtp = new smtp_mail("smtp.163.com","25","你的163.com的帐号","你的密码",true);
$smtp->send("你的163.com的帐号@163.com","目标地址","你好","你好");
㈧ 如何在PHP中使用PHPMailer发送邮件
<?php
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require'class.phpmailer.php';
try{
$mail=newPHPMailer(true);
$mail->IsSMTP();
$mail->CharSet='UTF-8';//设置邮件的字符编码,这很重要,不然中文乱码
$mail->SMTPAuth=true;//开启认证
$mail->Port=25;
$mail->Host="smtp.163.com";
$mail->Username="[email protected]";
$mail->Password="这是密码";
//$mail->IsSendmail();//如果没有sendmail组件就注释掉,否则出现“Couldnotexecute:/var/qmail/bin/sendmail”的错误提示
$mail->AddReplyTo("[email protected]","mckee");//回复地址
$mail->From="[email protected]";
$mail->FromName="www.phpddt.com";
$to="[email protected]";
$mail->AddAddress($to);
$mail->Subject="phpmailer测试标题";
$mail->Body="<h1>phpmail演示</h1>这是php点点通(<fontcolor=red>www.phpddt.com</font>)对phpmailer的测试内容";
$mail->AltBody="Toviewthemessage,!";//当邮件不支持html时备用显示,可以省略
$mail->WordWrap=80;//设置每行字符串的长度
//$mail->AddAttachment("f:/test.png");//可以添加附件
$mail->IsHTML(true);
$mail->Send();
echo'邮件已发送';
}catch(phpmailerException$e){
echo"邮件发送失败:".$e->errorMessage();
}
?>
㈨ 用PHP发送电子邮件的方法
<?
/*
* 邮件发送类
*/
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $smtp = "";
//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
var $check = 1;
//您的email帐号名称
var $username = "";
//您的email密码
var $password = "";
//此email 必需是发信服务器上的email
var $s_from = "";
/*
* 功能:发信初始化设置
* $from 你的发信服务器上的邮箱
* $password 你的邮箱密码
* $smtp 您的SMTP 服务器供应商,可以是域名或IP地址
* $check SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证
*/
function smail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/", $from ) ) {
$this->username = substr( $from, 0, strpos( $from , "@" ) );
$this->password = $password;
$this->smtp = $smtp ? $smtp : $this->smtp;
$this->check = $check;
$this->s_from = $from;
}
}
/*
* 功能:发送邮件
* $to 目标邮箱
* $from 来源邮箱
* $subject 邮件标题
* $message 邮件内容
*/
function send ( $to, $from, $subject, $message ) {
//连接服务器
$fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "联接服务器失败".__LINE__;
set_socket_blocking($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__;
//HELO
$yourname = "YOURNAME";
if($this->check == "1") $lastact="EHLO ".$yourname."\r\n";
else $lastact="HELO ".$yourname."\r\n";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) )
break;
}
//身份验证
if ($this->check=="1") {
//验证开始
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($this->username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($this->password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: <". $to ."> \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理From头
$head="From: $from\r\n";
$message = $head.$message;
//处理To头
$head="To: $to\r\n";
$message = $head.$message;
//加上结束串
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="QUIT\r\n";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
/*发送示例
$sm = new smail( "用户名@163.com", "密码", "smtp.163.com" );
$end = $sm->send( "目标邮箱", "来源邮箱", "这是标题", "这是邮件内容" );
if( $end ) echo $end;
else echo "发送成功!";
*/
?>
㈩ 如何在PHP网页中发送邮件
先建一个邮件发送的类 sendEmail.class.php页面:
<?
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $smtp = "";
//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
var $check = 1;
//您的email帐号名称
var $username = "";
//您的email密码
var $password = "";
//此email 必需是发信服务器上的email
var $s_from = "";
function smail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/", $from ) ) {
$this->username = substr( $from, 0, strpos( $from , "@" ) );
$this->password = $password;
$this->smtp = $smtp ? $smtp : $this->smtp;
$this->check = $check;
$this->s_from = $from;
}
}
function send ( $to, $from, $subject, $message ) {
//连接服务器
$fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "联接服务器失败".__LINE__;
set_socket_blocking($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__;
//HELO
$yourname = "YOURNAME";
if($this->check == "1") $lastact="EHLO ".$yourname."
";
else $lastact="HELO ".$yourname."
";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) )
break;
}
//身份验证
if ($this->check=="1") {
//验证开始
$lastact="AUTH LOGIN"."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($this->username)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($this->password)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: <". $to .">
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
//DATA
$lastact="DATA
";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject
";
$message = $head."
".$message;
//处理From头
$head="From: $from
";
$message = $head.$message;
//处理To头
$head="To: $to
";
$message = $head.$message;
//加上结束串
$message .= "
.
";
//发送信息
fputs($fp, $message);
$lastact="QUIT
";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
?>
然后建立一个发送页面:sendEmail.php
<?php
require_once('sendemail.class.php');
if((isset($_POST["send"]))&&($_POST["send"]=="form1"))
{
//获取收件人地址
$sendto=$_POST['txt1'];
$sendfrom="发件人邮箱";
$mailpass="发件人邮箱密码";
$mailserver="邮箱服务器";
$subject=$_POST['txt3'];
$message =$_POST['textarea'];
$sm = new smail( $sendfrom, $mailpass, $mailserver);
$end = $sm->send( $sendto, $sendfrom, $subject, $message );
if( $end ) echo $end;
else echo "<script>alert('发送成功');</script>";
}
?>
<center>
<form style="width:524px; height:107px;" method="post" name="form1" id="form1" action="">
<table width="524" height="107" border="0" align="center">
<!--DWLayoutTable-->
<tr>
<td height="60" colspan="2" align="center" valign="middle" bgcolor="#999999"><font color="#339933" size="+4" face="隶书"><strong>发送电子邮件</strong></font></td>
</tr>
<tr>
<td width="109" height="44" align="left" valign="middle" bgcolor="#CCCC99"><strong><font size="5">收件人:</font></strong></td>
<td width="405" align="left" valign="middle" bgcolor="#CCCC99"><input type="text" name="txt1"/></td>
</tr>
<tr>
<td height="44" align="left" valign="middle" bgcolor="#99CCFF"><strong><font size="5">主题:</font></strong></td>
<td valign="middle" bgcolor="#99CCFF"><input type="text" name="txt3""/></td>
</tr>
<tr>
<td height="163" align="left" valign="middle" bgcolor="#66CCFF"><strong><font size="5">内容:</font></strong></td>
<td valign="top" bgcolor="#66CCFF"><textarea name="textarea" style="width:405px; height:163px"></textarea></td>
</tr>
<tr>
<td height="37" colspan="2" align="center" valign="middle" bgcolor="#6699CC"><input type="submit" value="发送"/></td>
</tr>
</table>
<input type="hidden" name="send" value="form1"/>
</form>
</center>