vi /home/bitrix/.msmtprc
# Defaults for all accounts
defaults
keepbcc on
auth on
tls on
tls_starttls off
tls_certcheck off
port 465
host smtp.mail.ru
logfile /home/bitrix/msmtp_default.log
account example #Основной ящик из Панели управления
from example@mail.ru
user example@mail.ru
password ************
account example1 #ящик пользователя 1 из CRM
logfile /home/bitrix/msmtp_example1.log
from example1@mail.ru
user example1@mail.ru
password ************
account example2 #ящик пользователя 2 из CRM
logfile /home/bitrix/msmtp_example2.log
from example2@mail.ru
user example2@mail.ru
password ************
# Set a default account
account default : example #указывает ящик по умолчанию который в Панели управления
vi /home/bitrix/www/bitrix/php_interface/init.php
<?
// Отправка почты
function custom_mail ( $to , $subject , $body , $headers ) {
$arHeaders = cdek_email_parse_headers( $headers );
$fromHeader = $arHeaders [ 'sender' ][ 0 ] ?? $arHeaders [ 'from' ][ 0 ];
$fromAddress = explode( ',' , $fromHeader )[ 0 ];
if (preg_match( '/<([^>]+)>/' , $fromAddress , $matches )) {
$fromAddress = $matches [ 1 ];
}
$accounts = getMsmtpAccounts();
if ( $accounts && $param = array_search( $fromAddress , $accounts )) {
$params = '-a ' . $param ;
}
return mail( $to , $subject , $body , $headers , $params );
}
// Получение хедеров письма
function cdek_email_parse_headers ( $headersStr ) {
$headers = [];
$currentHeader = null ;
foreach (explode( "\n" , $headersStr ) as $line ) {
$line .= "\n" ;
if (preg_match( '/^([\w-]+):(.*)$/' , $line , $matches )) {
if ( $currentHeader ) {
$currentHeader [ 'value' ] = rtrim( $currentHeader [ 'value' ], "\r\n" );
$headers [ $currentHeader [ 'name' ]][] = $currentHeader [ 'value' ];
}
$currentHeader = [ 'name' => strtolower( $matches [ 1 ]), 'value' => ltrim( $matches [ 2 ])];
} else {
$currentHeader [ 'value' ] .= $line ;
}
}
if ( $currentHeader ) {
$currentHeader [ 'value' ] = rtrim( $currentHeader [ 'value' ], "\r\n" );
$headers [ $currentHeader [ 'name' ]][] = $currentHeader [ 'value' ];
}
return $headers ;
}
// Получение аккаунтов msmtp
function getMsmtpAccounts () {
$msmtpConfig = file_get_contents( "/home/bitrix/.msmtprc" );
$accounts = null ;
$accountName = "" ;
foreach (explode( "\n" , $msmtpConfig ) as $line ) {
$line .= "\n" ;
if (preg_match( '/^([\w-]+)\s*\t*(.*)$/' , $line , $matches )) {
if ( $matches [ 1 ] == "account" ) {
if (trim(explode( ":" , $matches [ 2 ])[ 0 ]) == "default" ) {
//$defaultAccountName = trim(explode(":",$matches[2])[1], " \t\r\n");
continue ;
}
$accountName = $matches [ 2 ];
} else if ( $matches [ 1 ] == "from" ) {
$accounts [ $accountName ] = rtrim( $matches [ 2 ], "\r\n" );
}
}
}
return $accounts ;
}
?>