php 邮件发送程序代码(支持附件)(1/2)

*/
require_once ('common/email.class.php');

 代码如下 复制代码

//这里以smiley_face@126.com邮箱为例,使用者需要自己修改

$smtps教程erver = "smtp.126.com";//smtp服务器
$smtpserverport =25;//smtp服务器端口
$smtpusermail = "smiley_face@126.com";//smtp服务器的用户邮箱
$smtpuser = "smiley_face";//smtp服务器的用户帐号
$smtppass = "smileyface1224";//smtp服务器的用户密码,即邮箱密码
$mailtype = "html";//邮件格式(html/txt),txt为文本邮件

$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//true表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息

//common/email.class.php 代码如下
header("content-type: text/html; charset=utf-8");
class smtp
{
/* public variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* private variables */
var $sock;

/* constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = false;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in helo command, localhost
$this->log_file ="";

$this->sock = false;
}

/* main function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = preg_replace("/(^|( ))(\.)/", "\1.\3", $body);
$header .= "mime-version:1.0 ";
if($mailtype=="html"){
$header .= "content-type:text/html ";
}
$header .= "to: ".$to." ";
if ($cc != "") {
$header .= "cc: ".$cc." ";
}
$header .= "from: $from<".$from."> ";
$header .= "subject: ".$subject." ";
$header .= $additional_headers;
$header .= "date: ".date("r")." ";
$header .= "x-mailer:by redhat (php/".phpversion().") ";
list($msec, $sec) = explode(" ", microtime());
$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from."> ";
$to = explode(",", $this->strip_comment($to));

if ($cc != "") {
$to = array_merge($to, explode(",", $this->strip_comment($cc)));
}

if ($bcc != "") {
$to = array_merge($to, explode(",", $this->strip_comment($bcc)));
}

$sent = true;
foreach ($to as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("error: cannot send email to ".$rcpt_to." ");
$sent = false;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("e-mail has been sent to <".$rcpt_to."> ");
} else {
$this->log_write("error: cannot send email to <".$rcpt_to."> ");
$sent = false;
}
fclose($this->sock);
$this->log_write("disconnected from remote host ");
}
echo "<br>";
echo $header;
return $sent;
}

/* private functions */

function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("helo", $helo)) {
return $this->smtp_error("sending helo command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("auth login", base64_encode($this->user))) {
return $this->smtp_error("sending helo command");
}

if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending helo command");
}
}
#
if (!$this->smtp_putcmd("mail", "from:<".$from.">")) {
return $this->smtp_error("sending mail from command");
}

if (!$this->smtp_putcmd("rcpt", "to:<".$to.">")) {
return $this->smtp_error("sending rcpt to command");
}

if (!$this->smtp_putcmd("data")) {
return $this->smtp_error("sending data command");
}

if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}

if (!$this->smtp_eom()) {
return $this->smtp_error("sending <cr><lf>.<cr><lf> [eom]");
}

if (!$this->smtp_putcmd("quit")) {
return $this->smtp_error("sending quit command");
}

return true;
}

function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}

function smtp_sockopen_relay()
{
$this->log_write("trying to ".$this->relay_host.":".$this->smtp_port." ");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("error: cannot connenct to relay host ".$this->relay_host." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
return false;
}
$this->log_write("connected to relay host ".$this->relay_host." ");
return true;;
}

function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $mxhosts)) {
$this->log_write("error: cannot resolve mx "".$domain."" ");
return false;
}
foreach ($mxhosts as $host) {
$this->log_write("trying to ".$host.":".$this->smtp_port." ");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("warning: cannot connect to mx host ".$host." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
continue;
}
$this->log_write("connected to mx host ".$host." ");
return true;
}
$this->log_write("error: cannot connect to any mx hosts (".implode(", ", $mxhosts).") ");
return false;
}

function smtp_message($header, $body)
{
fputs($this->sock, $header." ".$body);
$this->smtp_debug("> ".str_replace(" ", " "."> ", $header." > ".$body." > "));

return true;
}

function smtp_eom()
{
fputs($this->sock, " . ");
$this->smtp_debug(". [eom] ");

return $this->smtp_ok();
}

function smtp_ok()
{
$response = str_replace(" ", "", fgets($this->sock, 512));
$this->smtp_debug($response." ");

if (!preg_match("/^[23]/", $response)) {
fputs($this->sock, "quit ");
fgets($this->sock, 512);
$this->log_write("error: remote host returned "".$response."" ");
return false;
}
return true;
}

 

首页 1 2 末页

时间: 2024-11-10 07:44:47

php 邮件发送程序代码(支持附件)(1/2)的相关文章

JavaMail实现email邮件发送程序代码

JavaMail 的基础知识使用JavaMail 是发送电子邮件所需要的组件. JavaMail 的机构使处理电子邮件非常容易.下面列出了一些我们需要的类: 1.Properties JavaMail 需要Properties 来创建一个session 对象.它将寻找字符串"mail.smtp.host",属性值就是发送邮件的主机,如: Properties props = new Properties (); props.put("mail.smtp.host",

asp 邮件发送程序代码

'**************************************************** '函数名:SendMail '作  用:用Jmail组件发送邮件 '参  数:ServerAddress  ----服务器地址 '        AddRecipient  ----收信人地址 '        Subject       ----主题 '        Body          ----信件内容 '        Sender        ----发信人地址 '***

用JavaMail API编写可带附件的邮件发送程序

程序 利用Sun公司提供的JavaMail API可以很方便的开发邮件发送程序.也许你已经可以利用它来发送一段简单的文本了,但想不想使你的程序像OUTLOOK一样也能发送附件呢?本文在简单介绍了JavaMail之后,详细讲解了一段完整的送信的JavaBean及一个十分轻巧的servlet. (没有装载JavaMail API的读者,可以到此站点下载,并按照Readme.txt设置好ClassPath) 一.JavaMail中一些我们需要的类 1.Properties JavaMail需要Prop

Java邮件发送程序(可以同时发给多个地址、可以带附件)_java

发送邮件的主程序 import java.util.Properties; import common.util.Email_Autherticatorbean; import javax.mail.Authenticator; import javax.mail.internet.InternetAddress; import org.apache.commons.lang.StringUtils; import javax.mail.internet.MimeBodyPart; import

jsp 邮件群发 程序代码

jsp 邮件群发 程序代码 public static boolean sendMail(String subject,String body,String to,String content,Address[] ValidUnsentAddresses,boolean isReSend) {   logger.info("始发送邮件*");   boolean result=false;   try {    Security.addProvider(new com.sun.net.

Rails测试《十一》添加邮件发送程序及测试邮件发送程序

讲到测试邮件发送程序,我们首先要让系统可以发送邮件.我们先来给系统添加发送邮件的功能. Action Mailer in Rails 3是一个不错的视频教程,大家可以参考. 还有就是http://guides.rubyonrails.org/action_mailer_basics.html中更加详细的介绍. 添加邮件发送程序 发送邮件要保证我们有目标邮箱,所以呢,我们的用户要有一个属性:email. 还是拿我的blog项目作为练习项目. 我们在用户注册的时候给他发一封邮件. 首先我们创建一个初

为什么我写的邮件发送程序中只能发送英文字符串,而发送的汉字用邮箱接收显示的是乱码,

问题描述 我写的邮件发送程序,是把邮件内容用UTF8转换成字节序列,然后通过NetworkStram发送出去.可我用邮箱接收发送的邮件时,英文字符串可以正确显示,汉字就显示的是乱码.帮解决下,谢谢~ 解决方案 解决方案二:那你再换成别的格式试试看,不要先转换!解决方案三:编码有问题,,换成GB2132试试解决方案四:用gb2312..简体中文一般用这个.

C#-MailSender邮件发送组件源代码(支持ESMTP, 附件)

smtp|源代码 //============================================================ // File: MailSender.cs // 邮件发送组件 // 支持ESMTP, 多附件 //============================================================ namespace JcPersonal.Utility { using System;: using System.Collect

基于JAVAMAIL包的邮件发送程序----kangkang

程序      JavaMail API 是一个用于阅读.编写和发送电子消息的可选包(标准扩展),可以用来建立基于标准的电子邮件客户机,它配置了各种因特网邮件协,包括SMTP .POP .IMAP和 MIME ,还包括相关的 NNTP .S/MIME 及其它协议.通常开发JavaMail 程序还需要有Sun 的JavaBeans Activation Framework (JAF ).JavaBeans Activation Framework 的运行很复杂,这里简单的说就是JavaMail 的