📜 ⬆️ ⬇️

Friendship OkayCMS and module for SMTP

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

As a result, we have compiled some algorithm for connecting this case to the Okay system.

So, step number one: 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 class Notify.php 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:

 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. A regulator has been added here in the form of an if construct, which allows the admin to switch the sending method himself, this is either the mail function or the SMTP protocol.

After that, “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:

 <div class="block layer"> <h2> SMTP </h2> <ul> <li> <label class="property" >SMTP </label> <select name="use_smtp"> <option value="1" {if $settings->use_smtp == 1}selected=""{/if}></option> <option value="0" {if $settings->use_smtp == 0}selected=""{/if}></option> </select> <label class="property" >SMTP Server</label> <input name="smtp_server" class="okay_inp" value="{$settings->smtp_server}" type="text" /> <label class="property" >SMTP Port</label> <input name="smtp_port" class=" okay_inp " value="{$settings->smtp_port}" type="text" /> <label class="property" >SMTP </label> <input name="smtp_user" class=" okay_inp " value="{$settings->smtp_user}" type="text" /> <label class="property" >SMTP </label> <input name="smtp_pass" class=" okay_inp " value="{$settings->smtp_pass}" type="text" /> <label class="property" > </label> <input name="user_pseudo" class=" okay_inp " value="{$settings->user_pseudo}" type="text" /> </li> </ul> </div> 

After that, we get this kind of appearance:

And adds a data handler of fields 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'); 


On this, the software part is over, 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 a domain (for example, on Yandex)
2. Switch MX mail service records on hosting
3. Ask the hosting provider whether the port 465 is open on the server, otherwise it will not make sense to send letters
4. Specify login and password in admin panel
5. Use a signed mailbox, and send letters from the "signed" sender

Source: https://habr.com/ru/post/319454/


All Articles