sm.php
<?php
فئة smtp
{
/* المتغيرات العامة */
فار $smtp_port;
فار $time_out;
فار $host_name;
فار $log_file;
فار $relay_host;
فار $debug;
فار $auth;
فار $user;
فار $pass;
/* المتغيرات الخاصة */
فار $ جورب؛
/* المقاول */
الدالة 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; // يستخدم في fsockopen ()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; // يستخدم في أمر HELO
$this->log_file = "";
$this->sock = FALSE;
}
/* الوظيفة الرئيسية */
الدالة sendmail($to, $from, $subject = ""، $body = ""، $mailtype، $cc = ""، $bcc = ""، $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(\.)"، " \1.\3 "، $body);
$header .= "MIME-Version:1.0rn";
إذا($mailtype==HTML){
$header .= "نوع المحتوى:نص/htmlrn";
}
$header .= "إلى: ".$to."rn";
إذا ($cc!= "") {
$header .= "نسخة إلى: ".$cc."rn";
}
$header .= "من: $from<".$from.">;rn";
$header .= "الموضوع: ".$subject."rn";
$header .= $additional_headers;
$header .= "التاريخ: ".date("r")."rn";
$header .= "X-Mailer:بواسطة Redhat (PHP/".phpversion().")rn";
list($msec, $sec) = Explosion(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".($msec*1000000).".".$mail_from.">;rn";
$TO = تنفجر("،، $this->strip_comment($to));
إذا ($cc!= "") {
$TO = array_merge($TO,تفجير("،, $this->strip_comment($cc)));
}
إذا ($bcc != "") {
$TO = array_merge($TO,تفجير("،, $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO كـ $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
إذا (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("خطأ: لا يمكن إرسال بريد إلكتروني إلى ".$rcpt_to."n");
$sent = FALSE;
يكمل؛
}
إذا ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("تم إرسال البريد الإلكتروني إلى <".$rcpt_to.">;n");
} آخر {
$this->log_write("خطأ: لا يمكن إرسال بريد إلكتروني إلى <".$rcpt_to.">;n");
$sent = FALSE;
}
fClose($this->sock);
$this->log_write("تم قطع الاتصال بالمضيف البعيدn");
}
إرجاع $المرسل؛
}
/* الوظائف الخاصة */
الدالة smtp_send($helo, $from, $to, $header, $body = "")
{
إذا (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("إرسال أمر HELO");
}
#auth
إذا($هذا->المصادقة){
إذا (!$this->smtp_putcmd("تسجيل الدخول التلقائي"، base64_encode($this->user))) {
return $this->smtp_error("إرسال أمر HELO");
}
إذا (!$this->smtp_putcmd(""، base64_encode($this->pass))) {
return $this->smtp_error("إرسال أمر HELO");
}
}
#
إذا (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">;")) {
return $this->smtp_error("إرسال أمر البريد من الأمر");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">;")) {
إرجاع $this->smtp_error("إرسال أمر RCPT إلى");
}
إذا (!$this->smtp_putcmd("DATA")) {
إرجاع $this->smtp_error("إرسال أمر البيانات");
}
إذا (!$this->smtp_message($header, $body)) {
إرجاع $this->smtp_error("إرسال رسالة");
}
إذا (!$this->smtp_eom()) {
return $this->smtp_error("sending <CR>;<LF>;.<CR>;<LF>; [EOM]");
}
إذا (!$this->smtp_putcmd("QUIT")) {
إرجاع $this->smtp_error("إرسال أمر إنهاء");
}
إرجاع صحيح؛
}
الدالة smtp_sockopen($address)
{
إذا ($this->relay_host == "") {
إرجاع $this->smtp_sockopen_mx($address);
} آخر {
إرجاع $this->smtp_sockopen_relay();
}
}
الدالة smtp_sockopen_relay()
{
$this->log_write("محاولة ".$this->relay_host.":":$this->smtp_port."n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
إذا (!($this->sock && $this->smtp_ok())) {
$this->log_write("خطأ: لا يمكن الاتصال بمضيف الترحيل ".$this->relay_host."n");
$this->log_write("خطأ: ".$errstr." (".$errno.")n");
إرجاع خطأ؛
}
$this->log_write("متصل بمضيف الترحيل ".$this->relay_host."n");
إرجاع صحيح؛
}
الدالة smtp_sockopen_mx($address)
{
$domain = ereg_replace(" ^.+@([^@]+)$ ", " \1 ", $address);
إذا ( !@getmxrr($domain , $MXHOSTS)) {
$this->log_write("خطأ: لا يمكن حل MX "".$domain.""n");
إرجاع خطأ؛
}
foreach ($MXHOSTS كمضيف $) {
$this->log_write("محاولة ".$host.":":$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
إذا (!($this->sock && $this->smtp_ok())) {
$this->log_write("تحذير: لا يمكن الاتصال بمضيف mx ".$host."n");
$this->log_write("خطأ: ".$errstr." (".$errno.")n");
يكمل؛
}
$this->log_write("متصل بمضيف mx ".$host."n");
إرجاع صحيح؛
}
$this->log_write("خطأ: لا يمكن الاتصال بأي مضيفين mx (".implode(", ", $MXHOSTS).")n");
إرجاع خطأ؛
}
الدالة smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug(">; ".str_replace("rn", "n".">; ", $header."n>; ".$body."n>; ") );
إرجاع صحيح؛
}
الدالة smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(".[EOM]n");
إرجاع $this->smtp_ok();
}
الدالة smtp_ok()
{
$response = str_replace("rn"، ""، fgets($this->sock, 512));
$this->smtp_debug($response."n");
إذا (!ereg("^[23]"، $response)) {
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("خطأ: قام المضيف البعيد بإرجاع "".$response.""n");
إرجاع خطأ؛
}
إرجاع صحيح؛
}
الدالة smtp_putcmd($cmd, $arg = "")
{
إذا ($arg != "") {
إذا($cmd=="") $cmd = $arg;
آخر $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug(">; ".$cmd."n");
إرجاع $this->smtp_ok();
}
الدالة smtp_error($string)
{
$this->log_write("خطأ: حدث خطأ أثناء ".$string.".n");
إرجاع خطأ؛
}
الدالة log_write($message)
{
$this->smtp_debug($message);
إذا ($this->log_file == "") {
إرجاع صحيح؛
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
إذا ( !@file_exists($this->log_file ) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("تحذير: لا يمكن فتح ملف السجل "".$this->log_file.""n");
إرجاع خطأ؛
}
قطيع($fp, LOCK_EX);
fputs($fp, $message);
fClose($fp);
إرجاع صحيح؛
}
وظيفة strip_comment(عنوان $)
{
$comment = " \([^()]*\ )";
بينما (ereg(تعليق $، عنوان $)) {
$address = ereg_replace($comment, ""، $address);
}
إرجاع عنوان $;
}
وظيفة get_address(عنوان $)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>;.*$", " \1 ", $address);
إرجاع عنوان $؛
}
الدالة smtp_debug($message)
{
إذا ($هذا->تصحيح) {
صدى رسالة $؛
}
}
}
?>
mail.php
<?php
تتطلب("sm.php");
####################################################################################
$smtpserver = "mail.asdf.com";//الاتصال عبر SMTP
$smtpserverport =25;//استلام SMTP
$smtpusermail = " [email protected]";// إرسال بريد SMTP
$smtpemailto = " [email protected]";// التسجيل في الموقع
$smtpuser = " [email protected]";//الاتصال بـ SMTP
$smtppass = "asdf";// إرسال رسالة SMTP
$mailsubject = "موضوع الاختبار";//الموضوع الرئيسي
$mailbody = "<h1>;هذا بريد تجريبي</h1>;";//邮件内容
$mailtype = "HTML";//اكتب النص (HTML/TXT)، TXT 为文本邮件
####################################################################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass); .
$smtp->debug = TRUE;// قم بتصحيح الخطأ
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);