Skip to content

Instantly share code, notes, and snippets.

@ahomu
Created March 23, 2011 03:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahomu/882563 to your computer and use it in GitHub Desktop.
Save ahomu/882563 to your computer and use it in GitHub Desktop.
[a-blog cms] 登録ユーザーにメールを一斉配信できたりするかもしれない一式
<?php
require_once ACMS_LIB_DIR.'GET.php';
class ACMS_GET_User_Messanger extends ACMS_GET
{
function get()
{
if ( !sessionWithAdministration() ) die;
$Tpl = new Template($this->tpl, new ACMS_Corrector());
$Tpl->add(null, $this->buildField($this->Post, $Tpl));
return $Tpl->get();
}
}
<?php
require_once ACMS_LIB_DIR.'POST/User.php';
require_once ACMS_LIB_DIR.'GET.php';
class ACMS_POST_User_Messanger extends ACMS_POST_User
{
function post()
{
if ( !sessionWithAdministration() ) die;
@set_time_limit(0);
$Field =& $this->extract();
$Field->validate(new ACMS_Validator());
if ( $this->Post->isValidAll() ) {
// get params
$dumpDir = SCRIPT_DIR.ARCHIVES_DIR.'maildump/';
$dumpFile= $dumpDir.'maildump'.time().'.txt';
$preview = $Field->get('preview') === 'on' ? true : false;
$targets = $Field->getArray('target_auth');
$from = $Field->get('from');
$replyTo = $Field->get('replyTo');
$subject = $Field->get('subject');
$bodyTpl = $Field->get('body');
// get users
$DB = DB::singleton(dsn());
$SQL = SQL::newSelect('user');
$SQL->addWhereIn('user_auth', $targets);
$SQL->addWhereOpr('user_blog_id', BID);
$q = $SQL->get(dsn());
$DB->query($q, 'fetch');
// dump open
if ( !file_exists($dumpDir) ) {
mkdir($dumpDir);
chmod($dumpDir, 0777);
}
$fh = fopen($dumpFile, 'w+');
while ( $user = $DB->fetch($q) ) {
// merge fields
$Ufd = loadUserField($user['user_id']);
foreach ( $user as $k => $v ) {
$Ufd->setField($k, $v);
}
// exceptions
if ( !($to = $Ufd->get('user_mail')) || preg_match('/example.com$/', $to) ) {
$dump = $Ufd->get('user_id').'@'.$Ufd->get('user_code')." is skipped.".
"\r\n\r\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n\r\n";
fwrite($fh, $dump);
continue;
}
// get instance
$Mail = new Mail();
$Tpl = new Template($bodyTpl, new ACMS_Corrector());
$Tpl->add(null, ACMS_GET::buildField($Ufd, $Tpl));
$body = $Tpl->get();
// set mail header
$Mail->setHeader('From', $from);
$Mail->setHeaderDate();
if ( !empty($replyTo) ) {
$Mail->setHeader('Reply-To', $replyTo);
}
// dump write
$dump = $Mail->getHeader().
"\r\nTo: $to".
"\r\nSubject: $subject".
"\r\n- - - - -\r\n".
$body.
"\r\n\r\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n\r\n";
fwrite($fh, $dump);
// execute
if ( !$preview ) {
$Mail->php_mail($to, $subject, $body);
} else if( @$previeewSendDone !== true ) {
$Mail = new Mail();
$Mail->setHeader('From', $from);
$Mail->php_mail($from, $subject, $body);
$previeewSendDone = true;
}
unset($Ufd);
unset($Mail);
unset($Tpl);
}
// dump close
fclose($fh);
$this->Post->set('send', $preview ? 'false' : 'true');
$this->Post->set('result', 'success');
$this->Post->set('dumpfile', $dumpFile);
} else {
$this->Post->set('result', 'fail');
}
return $this->Post;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="ja" lang="ja" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=%{CHARSET}" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<link href="%{THEMES_DIR}system/css/login.css" rel="stylesheet" type="text/css" media="all" />
<title>%{BLOG_NAME}</title>
<!-- BEGIN_MODULE Js -->
<script type="text/javascript" src="/index.js{arguments}" charset="UTF-8" id="acms-js"></script><!-- END_MODULE Js -->
<style type="text/css">
body {
padding: 20px;
}
input, textarea {
font-size: 120%;
padding: 3px;
}
#success {
color: blue;
}
#fail {
color: red;
}
#dump {
color: gray;
font-size: 80%;
}
dt span {
padding-left: 10px;
color: #cd5c5c;
}
dd {
margin-bottom: 5px;
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<h1>登録ユーザーへのメール配信</h1>
<!-- BEGIN_MODULE User_Messanger -->
<!-- BEGIN result:touch#success --><h2 id="success">成功しました.</h2><!-- END result:touch#success -->
<!-- BEGIN result:touch#fail --><h2 id="fail">入力値に不備があります.項目をもう一度確認してください.</h2><!-- END result:touch#fail -->
<!-- BEGIN send:touch#true --><p>&gt; ユーザーへのメール送信を実行しました.内容のダンプも処理されました.</p><!-- END send:touch#true -->
<!-- BEGIN send:touch#false --><p>&gt; テスト実行として,内容のダンプのみ処理されました.</p><!-- END send:touch#false -->
<!-- BEGIN dumpfile:veil--><h2 id="dump">ダンプ: {dumpfile}</h2><!-- END dumpfile:veil -->
<form action="" method="post">
<dl>
<dt>配信対象ユーザー (必須)<!-- BEGIN target_auth:validator --><span>少なくとも1つ以上選択してください</span><!-- END target_auth:validator --></dt>
<dd>
<label><input type="checkbox" name="target_auth[]" value="administrator"{target_auth:checked#administrator} />&nbsp;管理者</label>
<label><input type="checkbox" name="target_auth[]" value="editor"{target_auth:checked#editor} />&nbsp;編集者</label>
<label><input type="checkbox" name="target_auth[]" value="contributor"{target_auth:checked#contributor} />&nbsp;投稿者</label>
<label><input type="checkbox" name="target_auth[]" value="subscriber"{target_auth:checked#subscriber} />&nbsp;購読者</label>
<small>※現在のブログ(bid %{BID})のユーザーのみ対象となります.</small>
<input type="hidden" name="field[]" value="target_auth" />
<input type="hidden" name="target_auth:v#required" />
</dd>
<dt>From (必須)<!-- BEGIN from:validator --><span>未入力または無効な書式です</span><!-- END from:validator --></dt>
<dd>
<input type="text" name="from" value="{from}" size="66" class="js-placeholder" title="mail@example.com" />
<input type="hidden" name="field[]" value="from" />
<input type="hidden" name="from:v#email" />
<input type="hidden" name="from:v#required" />
</dd>
<dt>Reply-To<!-- BEGIN replyTo:validator --><span>未入力または無効な書式です</span><!-- END replyTo:validator --></dt>
<dd>
<input type="text" name="replyTo" value="{replyTo}" size="66" class="js-placeholder" title="mail@example.com" />
<input type="hidden" name="replyTo:v#email" />
<input type="hidden" name="field[]" value="replyTo" />
</dd>
<dt>Subject (必須)<!-- BEGIN subject:validator --><span>未入力です</span><!-- END subject:validator --></dt>
<dd>
<input type="text" name="subject" value="{subject}" size="66" class="js-placeholder" title="メールタイトル" />
<input type="hidden" name="field[]" value="subject" />
<input type="hidden" name="subject:v#required" />
</dd>
<dt>Body (必須・テンプレート)<!-- BEGIN body:validator --><span>未入力です</span><!-- END body:validator --></dt>
<dd>
<textarea rows="20" cols="100" name="body" class="js-placeholder" title="userのフィールドと,カスタムフィールドを変数として利用可能">{body}</textarea>
<input type="hidden" name="field[]" value="body" />
<input type="hidden" name="body:v#required" />
</dd>
<dt>テスト実行</dt>
<dd>
<label><input type="checkbox" name="preview" value="on" checked="checked" />&nbsp;ON <small>※実際には送信せず,Fromに指定されたアドレスにサンプルを送信します.</small></label>
<input type="hidden" name="field[]" value="preview" />
</dd>
<dt>送信</dt>
<dd>
<input type="submit" name="ACMS_POST_User_Messanger" value="送信" style="width:200px;" />
</dd>
</dl>
</form>
<!-- END_MODULE User_Messanger -->
</body>
</html>
@ahomu
Copy link
Author

ahomu commented Mar 23, 2011

v1.4.0b3以降で,使い方とか改造の方法が分かる人だけノークレームでどうぞ…(´-`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment