首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 发送HTML邮件, [已结贴,结贴人:xyhs2008]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xyhs2008
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    • 揭帖率:
    发表于:2008-04-27 13:49:57 楼主
    我试用了下面的类,可以发送邮件,但是怎么设置就是不能发布HTML格式的邮件,在网上找了很久,也没找到合适的发送程序,请各位指点一二。

    PHP code
    <?php define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE); define('SMTP_STATUS_CONNECTED', 2, TRUE); class smtp{ var $connection; var $recipients; var $headers; var $timeout; var $errors; var $status; var $body; var $from; var $host; var $port; var $helo; var $auth; var $user; var $pass; function smtp($params = array()){ if(!defined('CRLF')) define('CRLF', "\r\n", TRUE); $this->timeout = 5; $this->status = SMTP_STATUS_NOT_CONNECTED; $this->host = 'localhost'; $this->port = 25; $this->helo = 'localhost'; $this->auth = FALSE; $this->user = ''; $this->pass = ''; $this->errors = array(); foreach($params as $key => $value){ $this->$key = $value; } } function connect($params = array()){ if(!isset($this->status)){ $obj = new smtp($params); if($obj->connect()){ $obj->status = SMTP_STATUS_CONNECTED; } return $obj; }else{ $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); socket_set_timeout($this->connection, 0, 250000); $greeting = $this->get_data(); if(is_resource($this->connection)){ return $this->auth ? $this->ehlo() : $this->helo(); }else{ $this->errors[] = 'Failed to connect to server: '.$errstr; return FALSE; } } } /*************************************** ** Function which handles sending the mail. ** Arguments: ** $params - Optional assoc array of parameters. ** Can contain: ** recipients - Indexed array of recipients ** from - The from address. (used in MAIL FROM:), ** this will be the return path ** headers - Indexed array of headers, one header per array entry ** body - The body of the email ** It can also contain any of the parameters from the connect() ** function ***************************************/ function send($params = array()){ foreach($params as $key => $value){ $this->set($key, $value); } if($this->is_connected()){ // Do we auth or not? Note the distinction between the auth variable and auth() function if($this->auth){ if(!$this->auth()) return FALSE; } $this->mail($this->from); if(is_array($this->recipients)) foreach($this->recipients as $value) $this->rcpt($value); else $this->rcpt($this->recipients); if(!$this->data()) return FALSE; // Transparency $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers))); $body = str_replace(CRLF.'.', CRLF.'..', $this->body); $body = $body[0] == '.' ? '.'.$body : $body; $this->send_data($headers); $this->send_data(''); $this->send_data($body); $this->send_data('.'); return (substr(trim($this->get_data()), 0, 3) === '250'); }else{ $this->errors[] = 'Not connected!'; return FALSE; } } /*************************************** ** Function to implement HELO cmd ***************************************/ function helo(){ if(is_resource($this->connection) AND $this->send_data('HELO '.$this->helo) AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{ $this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function to implement EHLO cmd ***************************************/ function ehlo(){ if(is_resource($this->connection) AND $this->send_data('EHLO '.$this->helo) AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{ $this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function to implement AUTH cmd ***************************************/ function auth(){ if(is_resource($this->connection) AND $this->send_data('AUTH LOGIN') AND substr(trim($error = $this->get_data()), 0, 3) === '334' AND $this->send_data(base64_encode($this->user)) // Send username AND substr(trim($error = $this->get_data()),0,3) === '334' AND $this->send_data(base64_encode($this->pass)) // Send password AND substr(trim($error = $this->get_data()),0,3) === '235' ){ return TRUE; }else{ $this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3)); return FALSE; } } function mail($from){ if($this->is_connected() AND $this->send_data('MAIL FROM:<'.$from.'>') AND substr(trim($this->get_data()), 0, 2) === '250' ){ return TRUE; }else return FALSE; } function rcpt($to){ if($this->is_connected() AND $this->send_data('RCPT TO:<'.$to.'>') AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){ return TRUE; }else{ $this->errors[] = trim(substr(trim($error), 3)); return FALSE; } } function data(){ if($this->is_connected() AND $this->send_data('DATA') AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){ return TRUE; }else{ $this->errors[] = trim(substr(trim($error), 3)); return FALSE; } } function is_connected(){ return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED)); } function send_data($data){ if(is_resource($this->connection)){ return fwrite($this->connection, $data.CRLF, strlen($data)+2); }else return FALSE; } function &get_data(){ $return = ''; $line = ''; if(is_resource($this->connection)){ while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' '){ $line = fgets($this->connection, 512); $return .= $line; } return $return; }else return FALSE; } function set($var, $value){ $this->$var = $value; return TRUE; } } // End of class ?> <?php //send_mail("XXX@XXX.com","新下的订单","购物"); function send_mail($rec,$tit,$cont){ include('class.smtp.php'); header('Content-Type: text/plain'); $params['host'] = 'mail.XXX.com'; // The smtp server host/ip $params['port'] = 25; // The smtp server port $params['helo'] = exec('mail.XXX.com'); // What to use when sending the helo command. Typically, your domain/hostname $params['auth'] = TRUE; // Whether to use basic authentication or not $params['user'] = 'sales@XXX.com'; // Username for authentication $params['pass'] = 'XXXXXX'; // Password for authentication $send_params['recipients'] = array(''.$rec.''); // The recipients (can be multiple) $send_params['headers'] = array( 'From: "易麦购"<sales@XXX.com>', // Headers 'To: '.$rec.'', 'Subject:' .$tit.'' ); $send_params['from'] = 'sales@XXX.com'; // This is used as in the MAIL FROM: cmd // It should end up as the Return-Path: header $send_params['body'] = ''.$cont.''; // The body of the email if(is_object($smtp = smtp::connect($params)) AND $smtp->send($send_params)){ //echo 'Email sent successfully!'."\r\n\r\n"; //Any recipients that failed (relaying denied for example) will be logged in the errors variable. //print_r($smtp->errors); // }else{ //echo 'Error sending mail'."\r\n\r\n"; //The reason for failure should be in the errors variable //print_r($smtp->errors); } } ?>
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • shengcha
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-04-27 14:18:531楼 得分:0
    可以使用pear包的,http://pear.php.net/package/Mail
    或者是Zend Framework的Zend_Mail类http://framework.zend.com/manual/zh/zend.mail.html
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • syre
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    • 3

      2

    发表于:2008-04-27 16:00:062楼 得分:0
    恩,用Pear_Mail包会方便很多
    html邮件要使用mime头。不是直接作为body就可以的。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xyhs2008
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-04-28 11:04:043楼 得分:0
    Pear_Mail包 我没接触过,能否推荐相关的资料学习学习,

    上面的类我用的很好就是不支持HTML,能否帮忙指点修改某处?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • jy_1201
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-04-28 11:14:454楼 得分:20
    试用一下PHPMailer这个包呢,我觉得不错的。

    ------------------------------------------
    酷得组件仓库 - 第三方软件开发组件下载\试用
    www.cookcode.net
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xyhs2008
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-04-28 12:06:045楼 得分:0
    非常感谢,这个包非常的好用。
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved