Sending letters to Bitrix on D7, the old core and via phpMailer
Rus
Eng
Отправка писем в Битрикс на D7, старом ядре и через phpMailer

All English-language materials have been translated fully automatically using the Google service

Sending letters to D7 in Bitrix

Sending a simple message on the "USER_INFO" mail event for the site "s1" on a schedule:

  \ Bitrix \ Main \ Mail \ Event :: send (array (
    "EVENT_NAME" => "USER_INFO",
    "LID" => "s1",
    "C_FIELDS" => array (
        "EMAIL" => "test@mysite.ru",
        "USER_ID" => 235
    ),
));
 

Sending a simple message on the "USER_INFO" mail event for site "s1" (instant):

  \ Bitrix \ Main \ Mail \ Event :: sendImmediate (array (
    "EVENT_NAME" => "USER_INFO",
    "LID" => "s1",
    "C_FIELDS" => array (
        "EMAIL" => "test@mysite.ru",
        "USER_ID" => 235
    ),
));  

Instant email sending a file sent to the server as an attachment:

  $ fileId = false;

if ($ _ FILES ['file'])
    $ fileId = \ CFile :: SaveFile ($ _ FILES ['file'], "mailatt");

\ Bitrix \ Main \ Mail \ Event :: sendImmediate (array (// or send
    "EVENT_NAME" => "VM_SERVICE_REQUEST",
    "LID" => "s2",
    "C_FIELDS" => array (
        "EMAIL" => $ arCompany ["PROPERTIES"] ["EMAIL"] ["VALUE"],
        "COMPANY_NAME" => $ arCompany ["NAME"],
        "MESSAGE" => $ request-> getPost ("MESSAGE"),
        "USER_NAME" => $ USER-> GetFullName (),
        "USER_EMAIL" => $ USER-> GetEmail (),
    ),
    "FILE" => array ($ fileId), // must be saved first, from the documentation for CEvent :: send: Array of file ids that are used by the CFile class. Alternatively, you can pass an array of absolute paths to files.

));

if ($ fileId)
    CFile :: Delete ($ fileId);  

CEvent is an analogue of the \ Bitrix \ Main \ Mail \ Event functions in the old kernel. The call is very similar:

  $ arEventFields = array (
    "ID" => $ CONTRACT_ID,
    "MESSAGE" => $ mess,
    "EMAIL_TO" => implode (",", $ EMAIL_TO),
    "ADMIN_EMAIL" => implode (",", $ ADMIN_EMAIL),
    "ADD_EMAIL" => implode (",", $ ADD_EMAIL),
    "STAT_EMAIL" => implode (",", $ VIEW_EMAIL),
    "EDIT_EMAIL" => implode (",", $ EDIT_EMAIL),
    "OWNER_EMAIL" => implode (",", $ OWNER_EMAIL),
    "BCC" => implode (",", $ BCC),
    "INDICATOR" => GetMessage ("AD _". Strtoupper ($ arContract ["LAMP"]. "_ CONTRACT_STATUS")),
    "ACTIVE" => $ arContract ["ACTIVE"],
    "NAME" => $ arContract ["NAME"],
    "DESCRIPTION" => $ description,
    "MAX_SHOW_COUNT" => $ arContract ["MAX_SHOW_COUNT"],
    "SHOW_COUNT" => $ arContract ["SHOW_COUNT"],
    "MAX_CLICK_COUNT" => $ arContract ["MAX_CLICK_COUNT"],
    "CLICK_COUNT" => $ arContract ["CLICK_COUNT"],
    "BANNERS" => $ arContract ["BANNER_COUNT"],
    "DATE_SHOW_FROM" => $ arContract ["DATE_SHOW_FROM"],
    "DATE_SHOW_TO" => $ arContract ["DATE_SHOW_TO"],
    "DATE_CREATE" => $ arContract ["DATE_CREATE"],
    "CREATED_BY" => $ CREATED_BY,
    "DATE_MODIFY" => $ arContract ["DATE_MODIFY"],
    "MODIFIED_BY" => $ MODIFIED_BY
    );
$ arrSITE = CAdvContract :: GetSiteArray ($ CONTRACT_ID);
CEvent :: Send ("ADV_CONTRACT_INFO", $ arrSITE, $ arEventFields);  

It should be noted that SendImmediate immediately sends a message and does not enter information about it in the database.

A Send adds a message to the queue and adds information about the event to the database.

Sending emails in Bitrix via PHPMailer

What to do if the mail is set up, emails go away, but do not reach the recipient, or end up in spam? The situation is very common, and therefore deserves a separate solution.

Let's assume that you are already familiar with composer , you have your own smtp server (Mail for Domain services from Yandex or Mail.ru are suitable), on DKIM, spf, etc. records are configured on the domain of the mail from which the sending is made

First of all, install PHPMailer

Go to /local/php_interface/init.php and add the function

  use Bitrix \ Main \ Application;
use PHPMailer \ PHPMailer \ PHPMailer;
use PHPMailer \ PHPMailer \ SMTP;
use PHPMailer \ PHPMailer \ Exception;
// Intercept the generation of a mail event with the possibility of canceling it
AddEventHandler ("main", "OnBeforeEventAdd", array ("OnBeforeEventAddClass", "OnBeforeEventAddHandler"));
class OnBeforeEventAddClass {
    function OnBeforeEventAddHandler (& $ event, & $ lid, & $ arFields, & $ message_id, & $ files) {

        // Load Composer's autoloader
        require $ _SERVER ['DOCUMENT_ROOT']. '/ vendor / autoload.php';

// Get the database connection
        $ connection = Bitrix \ Main \ Application :: getConnection ('default');

        // Get the email template
        $ recordset = $ connection-> query ("select SUBJECT, MESSAGE, MESSAGE_PHP from b_event_message where ACTIVE = 'Y' AND EVENT_NAME = '". $ event. "'") -> fetchAll ();

// Check if there is at least one template
        if (isset ($ recordset [0] ['MESSAGE'])) {

            $ mask = $ recordset [0] ['MESSAGE'];
            $ subject = $ recordset [0] ['SUBJECT'];

$ fields = $ arFields;

// Go through the fields and replace all the patterns that we find
            foreach ($ fields as $ key => $ value) {
// Exclude arrays and symbols from replacement ~
                if (! is_array ($ value) && strpos ($ key, '~') === false) {
                    // Replace all possible patterns
                    $ mask = preg_replace ('~ \ #'. $ key. '\ # ~', $ value, $ mask);
                    $ subject = preg_replace ('~ \ #'. $ key. '\ # ~', $ value, $ subject);
                }
            }

            $ mask = preg_replace ('~ \ #SITE_NAME \ # ~', 'Site name', $ mask);
            $ mask = preg_replace ('~ \ #SERVER_NAME \ # ~', 'Site Domain', $ mask);
            $ mask = preg_replace ('~ \ #DEFAULT_EMAIL_FROM \ # ~', 'The address of the mail sent from', $ mask);

            $ subject = preg_replace ('~ \ #SITE_NAME \ # ~', 'Site name', $ subject);
            $ subject = preg_replace ('~ \ #SERVER_NAME \ # ~', 'Site Domain', $ subject);


            if ($ mask) {
// Check a possible list of mail fields
// These fields are unique for one of my projects
                if (isset ($ fields ['RS_USER_EMAIL'])) {
                    if (filter_var ($ fields ['RS_USER_EMAIL'], FILTER_VALIDATE_EMAIL)) {
                        $ email = $ fields ['RS_USER_EMAIL'];
                    }
                }

                if (isset ($ fields ['EMAIL'])) {
                    if (filter_var ($ fields ['EMAIL'], FILTER_VALIDATE_EMAIL)) {
                        $ email = $ fields ['EMAIL'];
                    }
                }

                if (isset ($ fields ['EMAIL_TO'])) {
                    if (filter_var ($ fields ['EMAIL_TO'], FILTER_VALIDATE_EMAIL)) {
                        $ email = $ fields ['EMAIL_TO'];
                    }
                }

// If there is mail and the validator gives the go-ahead
                if ($ email && filter_var ($ email, FILTER_VALIDATE_EMAIL)) {
                    $ cl = new OnBeforeEventAddClass;

// Send the letter
                    if ($ cl-> custom_mail ($ email, $ subject, $ mask, '', ''))
                        AddMessage2Log ($ event .'- The message was sent ', "messages");
                    else
                        AddMessage2Log ($ event .'- Error. No message was sent ', "messages");
                    

                } else {
                    AddMessage2Log ($ event .'- Error. Mail not found. Mail: '. $ Fields [' EMAIL_RAW '], "messages");
                }

                return false;

            } else {
                AddMessage2Log ($ event .'- Error. No message body ', "messages");
            }
        } else {
            AddMessage2Log ($ event .'- Error. Message template not found ', "messages");
        }

    }
public function custom_mail ($ to, $ subject, $ message, $ additionalHeaders = '', $ additional_parameters)
    {
        // Instantiation and passing `true` enables exceptions
        $ mail = new PHPMailer (true);

        // error logging (0 - do not output)
        $ mail-> SMTPDebug = 0;
        $ mail-> CharSet = 'UTF-8';
        $ mail-> isSMTP (); // Set mailer to use SMTP
        // $ mail-> Host = 'mail.mail.ru'; // Specify main and backup SMTP servers
        $ mail-> Host = 'ssl: //smtp.mail.ru'; // Specify main and backup SMTP servers
        $ mail-> SMTPAuth = true; // Enable SMTP authentication
        $ mail-> Username = 'zakaz @ YOUR_DOMAIN'; // SMTP username
        $ mail-> Password = 'PASSWORD'; // SMTP password
        $ mail-> SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
        // $ mail-> Port = 25; // port to connect to
        $ mail-> Port = 465; // port to connect to

        $ d_message = '';

        $ from = 'zakaz @ YOUR_DOMAIN';
        $ d_message. = '$ from -'. $ from. ' +++ '; $ d_message. = '$ to -'. $ to. ' +++ ';
        $ d_message. = '$ subject -'. $ subject. ' +++ ';
        $ d_message. = '$ message -'. $ message. ' +++ ';
        $ mail-> setFrom ($ from, 'Site name');
        $ mail-> addAddress ($ to, ''); // Add a recipient
        $ mail-> addReplyTo ($ from, '');
        //$mail->addCC('mail2@mail.ru ');
        $ mail-> AddBCC ($ from);
        // $ mail-> addAttachment ('/ var / tmp / file.tar.gz'); // Add attachments
        // $ mail-> addAttachment ('/ tmp / image.jpg', 'new.jpg'); // Optional name
        $ mail-> Subject = $ subject;
        if ($ message! = strip_tags ($ message)) {
            $ mail-> isHTML (true);
        }
        $ mail-> Body = $ message;
        // $ mail-> AltBody = 'This is the body in plain text for non-HTML mail clients';

        // log_message
        $ addLOG = 'To:'. $ to.PHP_EOL. 'Subject:'. $ Subject.PHP_EOL. 'Message:'. $ Message.PHP_EOL. 'Headers:'. $ AdditionalHeaders.PHP_EOL. 'Params:'. $ Additional_parameters.PHP_EOL;
        
file_put_contents ($ _ SERVER ['DOCUMENT_ROOT']. '/ test / emailLog.txt', $ addLOG);
        if (! $ mail-> send ()) {
            // log error
            file_put_contents ($ _ SERVER ['DOCUMENT_ROOT']. '/ test / emailLog.txt', 'Mailer Error:'. $ d_message. ''. $ mail-> ErrorInfo);
            return false;
        } else {
            return true;
        }

    }
}  

AddMessage2Log is used for debugging, on production projects can be thrown out.

Thus, we receive all generated letters before sending them, receive a letter template, replace all the constants in it, fill in and send using PHPMailer tools, and cancel the Bitrix mail event. In general, what was required.

No problems with the code or load were identified on projects with up to 10k visitors per day.

Materials used:

Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number