Back to Home

Friendship OkayCMS and module for SMTP

CMS · php · smtp

Friendship OkayCMS and module for SMTP

After the mail services switched to their strict policies, many customers began to complain that the letters from the online store fall at least in spam, and in the worst case, the hosting simply blocks the letter and does not send it to the client. In this regard, we began to fasten the popular PHPMailer library to sites, which allows you to flexibly configure sending mail. As it turned out, in the framework of a simple CMS, this library looks like a “mini-monster" as it contained more files than a folder with all the system controllers.

As a result, we made up some algorithm for connecting this business to the Okay system.

So, step number one time: download the PHPMailer library from the gita .
Step number two: load the library folder into the api folder of the system itself and connect it to it.

To do this, we need to do the following manipulations:

In the Notify.php class, we connect the library like this:

require('PHPMailer/class.phpmailer.php');
include('PHPMailer/class.smtp.php');

After that, we change the standard mail function to this one:

public function SMTP($to, $subject, $message,$headers){
        $mail = new PHPMailer();
        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->Host       = ''.$this->settings->smtp_server.''; // SMTP server
        $mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->Port       = $this->settings->smtp_port;                    // set the SMTP port for the GMAIL server
        $mail->Username   = ''.$this->settings->smtp_user.''; // SMTP account username
        $mail->Password   = ''.$this->settings->smtp_pass.'';        // SMTP account password
        $mail->SetFrom($this->settings->smtp_user, $this->settings->user_pseudo);
        $mail->AddReplyTo($this->settings->smtp_user,$this->settings->user_pseudo);
        $mail->Subject    = $subject;
        $mail->MsgHTML($message);
        $mail->addCustomHeader("MIME-Version: 1.0\n");
        $recipients = explode(',',$to);
        if(!empty($recipients)){
            foreach($recipients as $i=>$r){
                $mail->AddAddress($r);
            }
        }
        else{
            $mail->AddAddress($to);
        }
        if(!$mail->Send()) {
            @file_put_contents('error_log.txt',$mail->ErrorInfo);
        }
    }
    function email($to, $subject, $message, $from = '', $reply_to = '')
    {
    	$headers = "MIME-Version: 1.0\n" ;
    	$headers .= "Content-type: text/html; charset=UTF-8; \r\n";
    	$headers .= "From: $from\r\n";
    	if(!empty($reply_to))
	    	$headers .= "reply-to: $reply_to\r\n";
    	$subject = "=?utf-8?B?".base64_encode($subject)."?=";
        if($this->settings->use_smtp){
            $this->SMTP($to, $subject, $message, $headers);
        }
        else{
            @mail($to, $subject, $message, $headers);
        }
    }

And in fact, the performing part is ready for you. Here a regulator is added in the form of an if construct, which allows the administrator to switch the sending method himself, this is either the mail function or the SMTP protocol.

After that we’ll “finish” the administrative part, and add the fields:

Go to the backend / design / html folder and open the settings.tpl file, at the end of which adds this piece of code:

Настройки SMTP почты


After which we get this kind of appearance externally:

And adds a field data handler to the backend / SettingsAdmin.php file

   $this->settings->smtp_server = $this->request->post('smtp_server');
            $this->settings->smtp_port = $this->request->post('smtp_port');
            $this->settings->smtp_user = $this->request->post('smtp_user');
            $this->settings->smtp_pass = $this->request->post('smtp_pass');
            $this->settings->use_smtp = $this->request->post('use_smtp');
            $this->settings->user_pseudo = $this->request->post('user_pseudo');


The software part has ended on this, and everything would be fine, but setting up this business sometimes requires nervous efforts.

A short list of actions will be as follows:

1. Register mail for the domain (for example, on Yandex)
2. Switch MX records of the mail service on the hosting
3. Check with the hosting whether the 465th port is open on the server, otherwise it will not make sense to send letters
4. Indicate username and password in the admin panel
5. Use the signed mailbox and send letters from the “signed” sender

Read Next