Наверх

Отправка писем через API MailGun

Хук для FormIt — emailAPI вместо стандартного email

<?php
$tpl = $modx->getOption('emailTpl',$hook->formit->config,'');

/* get from name */
$emailFrom = $modx->getOption('emailFrom',$hook->formit->config,'');
if (empty($emailFrom)) {
    $emailFrom = !empty($fields['email']) ? $fields['email'] : $modx->getOption('emailsender');
}
$emailFromName = $modx->getOption('emailFromName',$hook->formit->config,$emailFrom);

/* get subject */
$useEmailFieldForSubject = $modx->getOption('emailUseFieldForSubject',$hook->formit->config,true);
if (!empty($fields['subject']) && $useEmailFieldForSubject) {
    $subject = $fields['subject'];
} else {
    $subject = $modx->getOption('emailSubject',$hook->formit->config,'');
}

/* check email to */
$emailTo = $modx->getOption('emailTo',$hook->formit->config,'');
$emailToName = $modx->getOption('emailToName',$hook->formit->config,$emailTo);
if (empty($emailTo)) {
    $hook->addError('emailTo',$modx->lexicon('formit.email_no_recipient'));
    $modx->log(modX::LOG_LEVEL_ERROR,'[FormIt] '.$modx->lexicon('formit.email_no_recipient'));
    return false;
}

/* compile message */
$origFields = $fields = $hook->getValues();

/** @var pdoFetch $pdoFetch */
$fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
$path = $modx->getOption('pdofetch_class_path', null, MODX_CORE_PATH . 'components/pdotools/model/', true);
if ($pdoClass = $modx->loadClass($fqn, $path, false, true)) {
    $pdoFetch = new $pdoClass($modx, []);
} else {
    return false;
}
$message = $pdoFetch->getChunk($tpl, $fields);


$apiUrl = 'https://api.eu.mailgun.net/v3/domain.ru/messages';

$client = $modx->getService('rest', 'rest.modRest');

$headers = ['Authorization' => 'Basic ' . base64_encode('api:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')];
$data = [
    'from' => $emailFromName . ' <' . $emailFrom . '>',
    'to' => $emailTo,
    'subject' => $subject,
    'html' => $message
];



/* cc */
$emailCC = $modx->getOption('emailCC',$hook->formit->config,'');
if (!empty($emailCC)) {
    $emailCCName = $modx->getOption('emailCCName',$hook->formit->config,'');
    $emailCC = explode(',',$emailCC);
    $emailCCName = explode(',',$emailCCName);
    $data['cc'] = array_shift($emailCCName) . ' <' . array_shift($emailCC) . '>';
}

/* bcc */
$emailBCC = $modx->getOption('emailBCC',$hook->formit->config,'');
if (!empty($emailBCC)) {
    $emailBCCName = $modx->getOption('emailBCCName',$hook->formit->config,'');
    $emailBCC = explode(',',$emailBCC);
    $emailBCCName = explode(',',$emailBCCName);
    $data['bcc'] = array_shift($emailBCCName) . ' <' . array_shift($emailBCC) . '>';
}

$response = $client->post($apiUrl, $data, $headers)->process();

return !empty($response['id']);


0 комментариев

    Авторизация

    через сервис Loginza:


    Шаблоны MODX

    1 2 Дальше »

    Объектная
    модель
    MODX