Skip to content

Instantly share code, notes, and snippets.

@atsu666
Created June 7, 2021 09:50
Show Gist options
  • Select an option

  • Save atsu666/5a5c5caeec2adc71f22ec1fc596452dc to your computer and use it in GitHub Desktop.

Select an option

Save atsu666/5a5c5caeec2adc71f22ec1fc596452dc to your computer and use it in GitHub Desktop.
php/ACMS/POST/Entry/Mail.php
<?php
class ACMS_POST_Entry_Mail extends ACMS_POST_Entry
{
var $isCacheDelete = false;
function post()
{
@set_time_limit(0);
$subject = '';
$plain = '';
$html = '';
if ( 1
and ACMS_SID
and sessionWithAdministration()
and $bid = $this->Post->get('bid')
and isBlogAncestor($bid, SBID, true)
and $eid = $this->Post->get('eid')
and $subjectTpl = findTemplate(config('mail_entry_tpl_subject'))
and $bodyTplPlain = findTemplate(config('mail_entry_tpl_body_plain'))
and $bodyTplHtml = findTemplate(config('mail_entry_tpl_body_html'))
) {
} else {
return $this->Post;
}
$user_bid = ($this->Post->get('user_blog_id') > 0 )?$this->Post->get('user_blog_id'):$bid;
//---------
// subject
$header = array(
'User-Agent: acms',
'Accept-Language: ' . HTTP_ACCEPT_LANGUAGE,
);
$url = array(
'bid' => $bid,
'eid' => $eid,
'tpl' => config('mail_entry_tpl_subject'),
);
if ( SESSION_USE_COOKIE ) {
$header[] = 'Cookie: ' . SESSION_NAME . '=' . ACMS_SID;
} else {
$url['sid'] = ACMS_SID;
}
try {
$req = Http::init(acmsLink($url), 'GET');
$req->setRequestHeaders($header);
$response = $req->send();
$responseHeaders = $response->getResponseHeader();
$body = $response->getResponseBody();
if ( 1
and isset($responseHeaders['Content-Type'])
and preg_match('@^text/[^;]+; charset=(.*)$@', $responseHeaders['Content-Type'], $match)
) {
$subject = mb_convert_encoding($body, 'UTF-8', $match[1]);
}
} catch (\Exception $e) {
$this->addError($e->getMessage());
}
if (empty($subject)) {
return $this->Post;
} else if ( !LICENSE_PLUGIN_MAILMAGAZINE ) {
$subject = '[test]'.$subject;
}
//-------
// plain
$header = array(
'User-Agent: acms',
'Accept-Language: ' . HTTP_ACCEPT_LANGUAGE,
);
$url = array(
'bid' => $bid,
'eid' => $eid,
'tpl' => config('mail_entry_tpl_body_plain'),
);
if ( SESSION_USE_COOKIE ) {
$header[] = 'Cookie: ' . SESSION_NAME . '=' . ACMS_SID;
} else {
$url['sid'] = ACMS_SID;
}
try {
$req = Http::init(acmsLink($url), 'GET');
$req->setRequestHeaders($header);
$response = $req->send();
$responseHeaders = $response->getResponseHeader();
$body = $response->getResponseBody();
if ( 1
and isset($responseHeaders['Content-Type'])
and preg_match('@^text/plain; charset=(.*)$@', $responseHeaders['Content-Type'], $match)
) {
$plain = mb_convert_encoding($body, 'UTF-8', $match[1]);
}
} catch (\Exception $e) {
$this->addError($e->getMessage());
}
if ( empty($plain) ) {
return $this->Post;
}
//------
// html
$header = array(
'User-Agent: acms',
'Accept-Language: ' . HTTP_ACCEPT_LANGUAGE,
);
$url = array(
'bid' => $bid,
'eid' => $eid,
'tpl' => config('mail_entry_tpl_body_html'),
);
if ( SESSION_USE_COOKIE ) {
$header[] = 'Cookie: ' . SESSION_NAME . '=' . ACMS_SID;
} else {
$url['sid'] = ACMS_SID;
}
try {
$req = Http::init(acmsLink($url), 'GET');
$req->setRequestHeaders($header);
$response = $req->send();
$responseHeaders = $response->getResponseHeader();
$body = $response->getResponseBody();
if ( 1
and isset($responseHeaders['Content-Type'])
and preg_match('@^text/html; charset=(.*)$@', $responseHeaders['Content-Type'], $match)
) {
$htmlCharset = $match[1];
$html = mb_convert_encoding($body, 'UTF-8', $htmlCharset);
}
} catch (\Exception $e) {
$this->addError($e->getMessage());
}
if ( empty($html) ) {
return $this->Post;
}
//------
// mail
foreach ( array(
array(
'mail' => 'user_mail',
'magazine' => 'user_mail_magazine',
'html' => true,
),
array(
'mail' => 'user_mail_mobile',
'magazine' => 'user_mail_mobile_magazine',
'html' => false,
),
) as $config ) {
$aryaryBcc = array();
if ( $this->Post->get('issue') and LICENSE_PLUGIN_MAILMAGAZINE ) {
$DB = DB::singleton(dsn());
$SQL = SQL::newSelect('user');
$SQL->setSelect($config['mail']);
$SQL->addWhereOpr($config['magazine'], 'on');
// 購読者以外または購読者で本登録済み(user_pass != '')であること
$shouldRegistered = SQL::newWhere();
$shouldRegistered->addWhereOpr('user_auth', 'subscriber', '!=', 'OR');
$shouldRegistered->addWhereOpr('user_pass', '', '!=', 'OR');
$SQL->addWhere($shouldRegistered);
$SQL->addWhereOpr('user_blog_id', $user_bid);
$q = $SQL->get(dsn());
$n = 0;
$m = 0;
foreach ( $DB->query($q, 'all') as $row ) {
if ( empty($aryaryBcc[$n]) ) {
$aryaryBcc[$n] = array();
}
if ($row[$config['mail']]) {
$aryaryBcc[$n][$m] = $row[$config['mail']];
$m++;
}
if ( $m >= (config('mail_entry_bcc_limit') - 1) ) {
$n++;
$m = 0;
}
}
} else {
$aryaryBcc[] = array();
}
foreach ( $aryaryBcc as $aryBcc ) {
$to = implode(', ', configArray('mail_entry_to'));
$from = config('mail_entry_from');
if ( empty($to) ) {
$to = ACMS_RAM::userMail(SUID);
}
try {
$mailer = Mailer::init();
$mailer = $mailer->setFrom($from)
->setTo($to)
->setSubject($subject)
->setBody($plain);
if ( $config['html'] ) {
$mailer = $mailer->setHtml($html);
}
if ( !empty($aryBcc) ) {
$mailer->setCc(implode(',', $aryBcc));
}
$mailer->send();
} catch ( Exception $e ) {
throw $e;
}
}
}
return $this->Post;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment