PHP实现发送邮件的方法(基于简单邮件发送类)(2)

<?php //文件全路径名称,文件名称 function send_smtp_mail($file, $fileName) { require ("smtp_mail.php"); date_default_timezone_set ( 'Asia/Shanghai' ); $subject = date ( "Y-m-d" ) . "邮件标题"; //邮件标题 $content = "邮件内容!"; //邮件内容 //$file = "/a/b/c.txt"; //附件 //$fileName = "email_log.log"; //附件名称 $smtpserver = "服务器ip"; //SMTP服务器 $smtpserverport = 25; //SMTP服务器端口 $bcc = ""; //副收件人 //$smtpuser = ""; //SMTP服务器的用户帐号 //$smtppass = ""; //SMTP服务器的用户密码 $smtpmailfrom = "aaa@bbb.com"; //SMTP服务器的用户邮箱,邮件发送者 $smtpemailto = "邮箱1,邮箱2,邮箱3"; //邮件接受者 $cc = ""; //抄送 $mailsubject = $subject; //邮件主题 $mailtype = "HTML"; //邮件格式(HTML/TXT),TXT为文本邮件 $additional_headers = ""; $smtplogfile = ""; //发送邮件的日志文件,如果没有就不记录 // 定义分界线 $boundary = uniqid ( "" ); $headers = "Content-type: multipart/mixed; boundary= $boundary\r\n"; //附件类型 $mimeType = "application/unknown"; // 打开文件 $fp = fopen ( $file, "r" ); // 把整个文件读入一个变量 $read = fread ( $fp, filesize ( $file ) ); //我们用base64方法把它编码 $read = base64_encode ( $read ); //把这个长字符串切成由每行76个字符组成的小块 $read = chunk_split ( $read ); fclose ( $fp ); //关闭文件 /* 邮件发送代码 */ $flag = 1; //判断使用什么样的文件头1或2 if ($fileName == "") { //没有附件 $body = $content; } else { //有附件 $flag = 2; if ($mailtype == "HTML") { $body = "--$boundary\r\n"; //此必须\r\n $body .= "Content-Type:text/html\r\n\r\n"; //此必须\r\n\r\n $body .= "$content\r\n"; $body .= "--$boundary\r\n"; $body .= "Content-type: $mimeType; name=$fileName\r\n"; $body .= "Content-disposition: attachment; filename=$fileName\r\n"; $body .= "Content-Transfer-Encoding: BASE64\r\n\r\n"; $body .= "$read\r\n\r\n"; $body .= "--$boundary--\r\n"; } else { $body = "--$boundary\r\n"; $body .= "Content-type: Content-type: text/plain; charset=iso-8859-1\r\n\r\n"; $body .= "Content-transfer-encoding: 8bit\r\n\r\n"; $body .= "$content\r\n\r\n"; $body .= "--$boundary\r\n"; $body .= "Content-type: $mimeType; name=$fileName\r\n\r\n"; $body .= "Content-disposition: attachment; filename=$fileName\r\n\r\n"; $body .= "Content-transfer-encoding: base64\r\n\r\n"; $body .= "$read\r\n\r\n"; $body .= "--$boundary--\r\n"; } } $mailbody = $body; //邮件内容 //这里面的一个true是表示使用身份验证,否则不使用身份验证. $smtp = new smtp ( $smtpserver, $smtpserverport, false, $smtplogfile ); //$smtp->debug = TRUE;//是否显示发送的调试信息 if ($smtp->sendmail ( $flag, $boundary, $smtpemailto, $smtpmailfrom, $mailsubject, $mailbody, $mailtype, $cc, $bcc, $additional_headers )) { echo ("发送成功"); } else { echo ("发送失败"); } } ?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2784be073f5d2563022d1de795207ba6.html