Atomなどで使われる日付形式RFC3339 を.Net Framework(C#)で出力するには下記のように行う。
string rfc3339 = System.Xml.XmlConvert.ToString(new DateTime(2009, 1, 1, 12, 3, 4, DateTimeKind.Local), System.Xml.XmlDateTimeSerializationMode.Utc);
変換結果は下記。
2009-01-01T03:03:04Z
LINQ to SQLでIDENTITY列のある表にInsertした場合、SubmitChanges()後に挿入したオブジェクトに自動的に発番したIDを入れてくれるようである。
NorthwindデータベースでCategoryIDがIdentity列であるCategoryテーブルに対して、下記LINQのコードを実行したところ
using (DataClasses1DataContext db = new DataClasses1DataContext())
{
Categories cat = new Categories(){CategoryName = “test”};
db.Categories.InsertOnSubmit(cat);
db.SubmitChanges();
var id = cat.CategoryID;
}
最後のcat.CategoryIDには自動発番されたIDが取得できていた。
Visual StudioでASP.Net MVC Web Applicationプロジェクトを作成する。
Modelクラス、Repositoryクラスを作成する。RepositoryクラスはModelクラスの取得、保存の操作を実装するクラス。Modelクラスにはビジネスロジックだけを実装する。
Controlクラスを作成するにはControllerフォルダで右クリックし、追加->Controllerをクリックする。
Controllerクラスが作成されるので、例えば下記のようなリストデータ出力用メソッドを作成する。 (more…)
エンコーディングは一旦ASCIIのバイト列に変換し、System.ConvertクラスのToBase64Stringメソッドを使う。
string base64Str = Convert.ToBase64String(Encoding.ASCII.GetBytes(textData)));
デコーディングはSystem.ConvertクラスのFromBase64Stringメソッドを使ってASCIIテキストのバイト列を取り出し、stringに変換する。
string textData = Encoding.ASCII.GetString(Convert.FromBase64String(”dGVzdA==”));
phpmailer をダウンロードして、phpMailer という名前で vendors 以下にコピーします。
Codeworx Technologies
http://phpmailer.codeworxtech.com/
僕が PHP をあまり理解していないからかもしれませんが、class.smtp.php 内で未定義の変数を使っているためエラーとなるので修正します。
class.smtp.php 165行目から
(省略)
public function StartTLS() {
$this->error = null; # to avoid confusion
if(!$this->connected()) {
$this->error = array(“error” => “Called StartTLS() without being connected”);
return false;
}
+ $extra = “”;
fputs($this->smtp_conn,”STARTTLS” . $extra . $this->CRLF);
(省略)
次に、app/controllers/components/email.php を作ります。*1
function startup(&$controller) {
$this->controller = &$controller;
}
function _element($template) {
$temp_layout = $this->controller->layout;
$this->controller->layout = ”;
ob_start();
$this->controller->autoRender = false;
App::import(‘Core’, array(‘View’));
$viewClass = new View($this->controller);
$content = $viewClass->element($template , $this->controller->viewVars, true);
echo $viewClass->renderLayout($content, $this->layout) ;
$this->controller->autoRender = ‘auto’;
$body = ob_get_clean();
$this->controller->layout = $temp_layout;
return $body;
}
function bodyText() {
if ($this->layout == null) {
$this->layout = ‘email/text/default’;
}
return $this->_element($this->elementDir . ‘/text/’ . $this->template);
}
function bodyHtml() {
if ($this->layout == null) {
$this->layout = ‘email/html/default’;
}
return $this->_element($this->elementDir . ‘/html/’ . $this->template);
}
function attach($filename, $asfile = ”) {
if (empty($this->attachments)) {
$this->attachments = array();
$this->attachments[0]['filename'] = $filename;
$this->attachments[0]['asfile'] = $asfile;
} else {
$count = count($this->attachments);
$this->attachments[$count+1]['filename'] = $filename;
$this->attachments[$count+1]['asfile'] = $asfile;
}
}
function base64($str, $charset=“JIS”, $transfer_encoding=“B”, $linefeed=“\n“) {
return mb_encode_mimeheader($str, $charset, $transfer_encoding, $linefeed);
}
function send($html = false) {
App::import(‘Vendor’, ‘PHPMailer’, array(‘file’ => ‘phpMailer’ . DS . ‘class.phpmailer.php’));
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
if ($this->smtpAuth) {
$mail->SMTPAuth = true;
$mail->Host = $this->smtpHostNames;
$mail->Username = $this->smtpUserName;
$mail->Password = $this->smtpPassword;
}
$mail->From = $this->from;
$mail->FromName = $this->base64(mb_convert_encoding($this->fromName, “JIS”, “UTF-8″));
$mail->AddReplyTo($this->from);
// to
if (is_array($this->to)) {
foreach ($this->to as $to) {
$mail->AddAddress($to);
}
} else {
$mail->AddAddress($this->to, $this->base64(mb_convert_encoding($this->toName, “JIS”, “UTF-8″)));
}
// cc
if(!empty($this->cc)) {
if (is_array($this->cc)) {
foreach ($this->cc as $cc) {
$mail->AddCC($cc);
}
} else {
$mail->AddCC($this->cc);
}
}
// bcc
if(!empty($this->bcc)) {
if (is_array($this->bcc)) {
foreach ($this->bcc as $bcc) {
$mail->AddBcc($bcc);
}
} else {
$mail->AddBcc($this->bcc);
}
}
$mail->Port = $this->smtpPort;
$mail->SMTPSecure = $this->smtpSecureType;
$mail->CharSet = “iso-2022-jp”;
$mail->Encoding = “7bit”;
$mail->WordWrap = 70; // set word wrap to 70 characters
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
if (empty($attachment['asfile'])) {
$mail->AddAttachment($attachment['filename']);
} else {
$mail->AddAttachment($attachment['filename'], $attachment['asfile']);
}
}
}
$mail->IsHTML($html); // set email format to HTML
$mail->Subject = mb_convert_encoding($this->subject, “JIS”, “UTF-8″);
if ($html) {
$mail->Body = mb_convert_encoding($this->bodyHtml(), “JIS”, “UTF-8″);
$mail->AltBody = mb_convert_encoding($this->bodyText(), “JIS”, “UTF-8″);
} else {
$mail->Body = mb_convert_encoding($this->bodyText(), “JIS”, “UTF-8″);
}
$result = $mail->Send();
if($result == false) {
$this->smtpError = $mail->ErrorInfo;
}
return $result;
}
}
?>
コントローラはまず、EmailComponent を読み込みます。
class UsersController extends AppController {
(省略)
+ var $components = array(‘Email’);
使い方
CakePHP の EmailComponent に似せています。
メールを送るには以下のようにします。
$this->set(‘user’, $user);
$this->Email->template = ’signup’; // views/elements/email/text/signup
$this->Email->to = $user['User']['pc_email'];
$this->Email->subject = ‘登録が完了しました’;
$this->Email->send();
if (!empty($this->Email->smtpError)) {
$this->log($this->Email->smtpError, LOG_DEBUG);
}
このとき、実際に使われるテンプレートは以下のようになります。
| layout | views/layout/email/text/default |
|---|---|
| view | views/elements/email/text/signup |
ビューは例えば以下のようにします。
<?php echo $user['User']['name'] ?>さん ユーザー登録が完了しました。
複数アドレスに送信したい場合、$this->Email->to, $this->Email->cc, $this->Email->bcc に配列を指定します。
$this->Email->to = array($user['User']['pc_email'], $user['User']['mobile_email']);
// layouts/email $this->Email->layout = ‘email’;
$result = $this->Email->send(true);
html メールの場合、実際に使われるテンプレートは以下のようになります。
| layout | views/layout/email/html/default |
|---|---|
| element | views/elements/email/html/signup |
#でオブジェクトをJSON形式でシリアライズするにはまずシリアライズするクラスにDataContract属性とそのプロパティにDataMember属性を設定しておく。 (more…)
ここで言う空メールとは携帯からメールを送ると、会員登録用の URL を書いたメールが自動返信されてくるアレのことです。
仕様としては reg@example.com へ空メールを送ると会員登録 URL を書いたメールが自動返信されてくるものとします。
Postfix の設定
既に Postfix を使ってメールを送信できているとします。 (more…)
JSFのdataTableを利用します。
名前の通り、テーブルを作成してくれます。
以下では、ユーザ情報の一覧を作成して、
テーブルに表示します。
dataTableのvalueにユーザ情報を保持するArrayListを設定します。
表示したいカラムを設定し、カラムのvalue値を設定します。 (more…)
http://rubyforge.net/projects/ruby-dbi/からDBIモジュールをダウンロードする。
あらかじめインストールするDBDドライバが使用するDBモジュールは先に入れておく。
mysqlとsqlite3のDBDドライバを使用する場合は下記を実行する。
ruby setup.rb config –bin-dir=$HOME/local/bin –rb-dir=$HOME/local/lib/ruby/site_ruby/1.8 –so-dir=$HOME/local/lib/ruby/site_ruby/1.8/i386-freebsd6 –with=dbi,dbd_mysql,dbd_sqlite3
ruby setup.rb setup
ruby setup.rb install
Powered by WordPress