ارسال ایمیل از طریق SMTP
SMTP Mailer

جایگزین کردن SMTP به جای تابع mail برای ارسال ایمیل‌های شیرترانیکس …

ایمیل‌های ارسالی در شیرترانیکس به صورت پیشفرض از تابع mail استفاده می‌کنند که عموماً به دلیل ارسال ایمیل‌های بسیار از جانب شبکه در سایت‌های بهینه‌سازی نشده و گاهاً به دلیل بسته بودن تابع مذکور روی سرور، مشکلاتی برای مدیران سایت‌ها متصور هست که همواره از جانب بخش پشتیبانی میزبانی وب استفاده از SMTP توصیه میشه …

SMTP پروتکلی استاندارد برای تبادل نامه‌های الکترونیکی (eMail) در اینترنت است.

برای جایگزین کردن SMTP به جای تابع mail قبلاً افزونه‌ای رایگان و همچنین افزونه‌ای تجاری معرفی کردیم، اما امروز قصد داریم نسخه‌ی جدید از افزونه‌ی رایگان رو به اشتراک بزاریم تا هم مشکلات موجود در نسخه قدیمی رو برطرف کرده باشیم، هم این نوید رو بدیم که این نسخه به صورت کامل منطبق با پلتفورم نوشته شده و در اختیار شما قرار گرفته …

راهنمای نصب :

1) بسته پیوست شده رو دانلود کرده و از حالت فشرده خارج کنید
2) فایل‌های class_PHPMailer.php و class_smtp.php رو به system/classes منتقل کنید
3) به فایل conf_system.php در فولدر system رجوع کرده و کد زیر رو اضافه کنید :

$C->SMTP = TRUE; // TRUE or FALSE
    $C->SMTP_HOST = 'mail.sharetronix.ir';
    $C->SMTP_USER = 'info@sharetronix.ir';
    $C->SMTP_PASS = 'PASSWORD';
    $C->SMTP_PORT = 25;

4) به func_main.php در system/helpers رجوع کرده و کد زیر رو پیدا کنید :

   function do_send_mail($email, $subject, $message, $from=FALSE)
    {
        global $C;
        if( ! $from ) {
            $from   = $C->SITE_TITLE.' <'.$C->SYSTEM_EMAIL.'>';
        }
        $crlf   = "\n";
        preg_match('/^(.*)(\<(.*)\>)?$/iuU', $from, $m);
        $from_mail  = trim($m[3]);
        $from_name  = trim($m[1]);
        $tmp    = empty($from_name) ? $from_mail : ( '=?UTF-8?B?'.base64_encode($from_name).'?= <'.$from_mail.'>' );
        $headers    = '';
        $headers    .= 'From: '.$tmp.$crlf;
        $headers    .= 'Reply-To: '.$tmp.$crlf;
        $headers    .= 'Return-Path: '.$tmp.$crlf;
        $headers    .= 'Message-ID: <'.time().rand(1000,9999).'@'.$C->DOMAIN.'>'.$crlf;
        $headers    .= 'X-Mailer: PHP/'.PHP_VERSION.$crlf;
        $headers    .= 'MIME-Version: 1.0'.$crlf;
        $headers    .= 'Content-Type: text/plain; charset=UTF-8'.$crlf;
        $headers    .= 'Content-Transfer-Encoding: 8bit'.$crlf;
        $subject    = '=?UTF-8?B?'.base64_encode($subject).'?=';
        return mail( $email, $subject, $message, $headers );
    }
    
    function do_send_mail_html($email, $subject, $message_txt, $message_html, $from=FALSE)
    {
        global $C;
        if( ! $from ) {
            $from   = $C->SITE_TITLE.' <'.$C->SYSTEM_EMAIL.'>';
        }
        /*  (DELETE_THIS_LINE) This is a fix for everybody with mail issues (2 types)             (DELETE_THIS_LINE) 1. Your script send mails with blank body             (DELETE_THIS_LINE) 2. Your script send mails with missing text in the mail body                         (DELETE_THIS_LINE) To activate the fix delete all the lines which contains (DELETE_THIS_LINE).                 do_send_mail($email, $subject, $message_txt, $from);         exit;                     (DELETE_THIS_LINE)*/
        
        $crlf   = "\n";
        $boundary   = '=_Part_'.md5(time().rand(0,9999999999));
        preg_match('/^(.*)(\<(.*)\>)?$/iuU', $from, $m);
        $from_mail  = trim($m[3]);
        $from_name  = trim($m[1]);
        $tmp    = empty($from_name) ? $from_mail : ( '=?UTF-8?B?'.base64_encode($from_name).'?= <'.$from_mail.'>' );
        $headers    = '';
        $headers    .= 'From: '.$tmp.$crlf;
        $headers    .= 'Reply-To: '.$tmp.$crlf;
        $headers    .= 'Return-Path: '.$tmp.$crlf;
        $headers    .= 'Message-ID: <'.time().rand(1000,9999).'@'.$C->DOMAIN.'>'.$crlf;
        $headers    .= 'X-Mailer: PHP/'.PHP_VERSION.$crlf;
        $headers    .= 'MIME-Version: 1.0'.$crlf;
        $headers    .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'.$crlf;
        $headers    .= '--'.$boundary.$crlf;
        $headers    .= 'Content-Type: text/plain; charset=UTF-8'.$crlf;
        $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
        $headers    .= 'Content-Disposition: inline'.$crlf;
        $headers    .= $crlf;
        $headers    .= chunk_split(base64_encode($message_txt));
        $headers    .= '--'.$boundary.$crlf;
        $headers    .= 'Content-Type: text/html; charset=UTF-8'.$crlf;
        $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
        $headers    .= 'Content-Disposition: inline'.$crlf;
        $headers    .= $crlf;
        $headers    .= chunk_split(base64_encode($message_html), 76, $crlf);
        $subject    = '=?UTF-8?B?'.base64_encode($subject).'?=';
        $result = @mail( $email, $subject, '', $headers );
        if( ! $result ) {
            // if mail is not accepted for delivery by the MTA, try something else:
            $headers    = '';
            $headers    .= 'From: '.$tmp.$crlf;
            $headers    .= 'Reply-To: '.$tmp.$crlf;
            $headers    .= 'Return-Path: '.$tmp.$crlf;
            $headers    .= 'Message-ID: <'.time().rand(1000,9999).'@'.$C->DOMAIN.'>'.$crlf;
            $headers    .= 'X-Mailer: PHP/'.PHP_VERSION.$crlf;
            $headers    .= 'MIME-Version: 1.0'.$crlf;
            $headers    .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'.$crlf;
            $headers    .= '--'.$boundary.$crlf;
            $headers    .= 'Content-Type: text/plain; charset=UTF-8'.$crlf;
            $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
            $headers    .= 'Content-Disposition: inline'.$crlf;
            $headers    .= chunk_split(base64_encode($message_txt));
            $headers    .= '--'.$boundary.$crlf;
            $headers    .= 'Content-Type: text/html; charset=UTF-8'.$crlf;
            $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
            $headers    .= 'Content-Disposition: inline'.$crlf;
            $headers    .= chunk_split(base64_encode($message_html), 76, $crlf);
            $result = @mail( $email, $subject, '', $headers );
        }
        return $result;
    }

5) کد ذکر شده رو حذف کرده، کد زیر رو جایگزین کنید :

function do_send_mail($email, $subject, $message, $from=FALSE)
    {
        global $C;
        if ( $C->SMTP ) {
            send_email_via_smtp($email, $subject, '', $message, $from);
            return TRUE;
        }
        if( ! $from ) {
            $from   = $C->SITE_TITLE.' <'.$C->SYSTEM_EMAIL.'>';
        }
        $crlf   = "\n";
        preg_match('/^(.*)(\<(.*)\>)?$/iuU', $from, $m);
        $from_mail  = trim($m[3]);
        $from_name  = trim($m[1]);
        $tmp    = empty($from_name) ? $from_mail : ( '=?UTF-8?B?'.base64_encode($from_name).'?= <'.$from_mail.'>' );
        $headers    = '';
        $headers    .= 'From: '.$tmp.$crlf;
        $headers    .= 'Reply-To: '.$tmp.$crlf;
        $headers    .= 'Return-Path: '.$tmp.$crlf;
        $headers    .= 'Message-ID: <'.time().rand(1000,9999).'@'.$C->DOMAIN.'>'.$crlf;
        $headers    .= 'X-Mailer: PHP/'.PHP_VERSION.$crlf;
        $headers    .= 'MIME-Version: 1.0'.$crlf;
        $headers    .= 'Content-Type: text/plain; charset=UTF-8'.$crlf;
        $headers    .= 'Content-Transfer-Encoding: 8bit'.$crlf;
        $subject    = '=?UTF-8?B?'.base64_encode($subject).'?=';
        return mail( $email, $subject, $message, $headers );
    }
    
    function do_send_mail_html($email, $subject, $message_txt, $message_html, $from=FALSE)
    {
        global $C;
        if ( $C->SMTP ) {
            send_email_via_smtp($email, $subject, $message_txt, $message_html, $from);
            return TRUE;
        }
        if( ! $from ) {
            $from   = $C->SITE_TITLE.' <'.$C->SYSTEM_EMAIL.'>';
        }
        $crlf   = "\n";
        $boundary   = '=_Part_'.md5(time().rand(0,9999999999));
        preg_match('/^(.*)(\<(.*)\>)?$/iuU', $from, $m);
        $from_mail  = trim($m[3]);
        $from_name  = trim($m[1]);
        $tmp    = empty($from_name) ? $from_mail : ( '=?UTF-8?B?'.base64_encode($from_name).'?= <'.$from_mail.'>' );
        $headers    = '';
        $headers    .= 'From: '.$tmp.$crlf;
        $headers    .= 'Reply-To: '.$tmp.$crlf;
        $headers    .= 'Return-Path: '.$tmp.$crlf;
        $headers    .= 'Message-ID: <'.time().rand(1000,9999).'@'.$C->DOMAIN.'>'.$crlf;
        $headers    .= 'X-Mailer: PHP/'.PHP_VERSION.$crlf;
        $headers    .= 'MIME-Version: 1.0'.$crlf;
        $headers    .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'.$crlf;
        $headers    .= '--'.$boundary.$crlf;
        $headers    .= 'Content-Type: text/plain; charset=UTF-8'.$crlf;
        $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
        $headers    .= 'Content-Disposition: inline'.$crlf;
        $headers    .= $crlf;
        $headers    .= chunk_split(base64_encode($message_txt));
        $headers    .= '--'.$boundary.$crlf;
        $headers    .= 'Content-Type: text/html; charset=UTF-8'.$crlf;
        $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
        $headers    .= 'Content-Disposition: inline'.$crlf;
        $headers    .= $crlf;
        $headers    .= chunk_split(base64_encode($message_html), 76, $crlf);
        $subject    = '=?UTF-8?B?'.base64_encode($subject).'?=';
        $result = @mail( $email, $subject, '', $headers );
        if( ! $result ) {
            $headers    = '';
            $headers    .= 'From: '.$tmp.$crlf;
            $headers    .= 'Reply-To: '.$tmp.$crlf;
            $headers    .= 'Return-Path: '.$tmp.$crlf;
            $headers    .= 'Message-ID: <'.time().rand(1000,9999).'@'.$C->DOMAIN.'>'.$crlf;
            $headers    .= 'X-Mailer: PHP/'.PHP_VERSION.$crlf;
            $headers    .= 'MIME-Version: 1.0'.$crlf;
            $headers    .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'.$crlf;
            $headers    .= '--'.$boundary.$crlf;
            $headers    .= 'Content-Type: text/plain; charset=UTF-8'.$crlf;
            $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
            $headers    .= 'Content-Disposition: inline'.$crlf;
            $headers    .= chunk_split(base64_encode($message_txt));
            $headers    .= '--'.$boundary.$crlf;
            $headers    .= 'Content-Type: text/html; charset=UTF-8'.$crlf;
            $headers    .= 'Content-Transfer-Encoding: base64'.$crlf;
            $headers    .= 'Content-Disposition: inline'.$crlf;
            $headers    .= chunk_split(base64_encode($message_html), 76, $crlf);
            $result = @mail( $email, $subject, '', $headers );
        }
        return $result;
    }
    
    function send_email_via_smtp($email, $subject, $message_txt='', $message_html, $from='') {
        global $C;
        if ( ! $C->SMTP ) {
            return FALSE;
        }
        $mail = new PHPMailer();
        if ( !$mail->ValidateAddress($email) ) {
            return FALSE;
        }
        $mail->isSMTP();
        $mail->Mailer = "smtp";
        $mail->SMTPAuth = TRUE;
        $mail->SMTPDebug = FALSE;
        $type = (empty($message_txt) ? FALSE : TRUE);
        $mail->IsHTML($type);
        $mail->Host = $C->SMTP_HOST;
        $mail->Port = $C->SMTP_PORT;
        $mail->Username = $C->SMTP_USER;
        $mail->Password = $C->SMTP_PASS;
        $mail->AddReplyTo($C->SYSTEM_EMAIL,  $C->SITE_TITLE);
        $mail->From =  (empty($from) ? $C->SYSTEM_EMAIL : $from);
        $mail->FromName = $C->SITE_TITLE;
        $mail->AddAddress($email);  
        $mail->Subject = $subject;
        $mail->msgHTML($message_html);
        $mail->AltBody = $message_txt;
        if(!$mail->Send()){
            return $mail->ErrorInfo;
        } else {
            return TRUE;
        }
    }

در پایان، اطلاعات مربوط به SMTP هاست یا SMTP گوگل رو در متغیرها (مرحله 3) درج کنید، اما توجه داشته باشید که استفاده از SMTP گوگل یا هاست هم با محدودیت‌هایی همراهه و همواره احتمال بلاک شدن IP شما و قرارگیری در بلک‌لیست وجود داره ؛ بنابراین توصیه می‌کنیم حتماً از سرویس‌های SMTP تخصصی استفاده کنید …


برای اطلاع از آخرین اخبار و تخفیف‌ها کانال تلگرام شیرترانیکس رو دنبال کنید.

پرسش و پاسخ

6 دیدگاه افزودن دیدگاه

  • در مرحله 3 به جای mail.sharetrinix.ir چه گزینه ای جایگزین کنم؟

    ارسال شده توسط Reza Bihoviat در 2015/09/06 - 01:12
  • SMTP مربوط به هاست خودتون یا سرویس SMTP ای که قصد استفاده ازش دارید رو درج کنید

    به عنوان مثال وقتی از طریق DirectAdmin ایمیل شخصی میسازید، SMTP شما میشه :
    mail.yoursite.ir

    ارسال شده توسط نیما صابری در 2015/09/06 - 08:47
  • تمام مراحل رو انجام دادم اما باز دعوتنامه ارسال نمیشه

    ارسال شده توسط Reza Bihoviat در 2015/09/06 - 18:35
    • مشکل عدم ارسال ایمیل از طریق SMTP رو با پشتیبانی هاستتون در میون بزارید.

      مطلب مرتبط :
      http://sharetronix.ir/?p=1496

      ارسال شده توسط نیما صابری در 2015/09/06 - 20:43
  • مطلب مرتبط رو چک کردم ارسال میشه حتی ایمیل مربوط به یاداوری رمز عبور هم ارسال میشه اما دعوتنامه نه

    ارسال شده توسط Reza Bihoviat در 2015/09/06 - 22:02
    • بنابراین باید کنترلر مربوط به صفحه دعوتنامه بررسی بشه

      در انجمن پیگیری کنید.

      ارسال شده توسط نیما صابری در 2015/09/07 - 08:37

برای ارسال دیدگاه باید لاگین نمایید. [donbaler-oauth]

جزئیات

عنوان : ارسال ایمیل از طریق SMTP
لینک کوتاه :
نسخه : 2.0
سازگار : نسخه 1x
نسخه 2x
دانلود : 1093 دفعه
موضوع : افزونه ها
آموزش و مقاله
توسط : نیما صابری
ارسال : سه‌شنبه، 23 ژوئن 2015

رتبه

4.25

خبرنامه

برای دریافت جدیدترین اخبار و مقالات ایمیل خود را درج کنید.

اشتراک

cloobDonbalerLinkPadTwitterFacebookyahoo
Support