2013年4月7日日曜日

[Sendmail]外部メールクライアントからのテスト

メールクライアントの設定
メールアドレスuser@サーバの IP address
POP サーバサーバの IP address
POP ユーザ名user
POP port110
SMTP サーバサーバの IP address
SMTP ユーザ名user
SMTP port25
  1. Sendmail が "user@サーバの IP address" というメールアドレスから受信できるように設定する
    1. /etc/mail/local-host-names に以下を追加
      サーバの IP address
    本来はサーバのドメイン名を決めてそれをここに記述するのだが、今回は IP address を直接記載にする
  2. 外部からアクセスできるように Firewall の設定で 25 (SMTP) を許可する
  3. Sendmail を再起動
    # /etc/init.d/sendmail restart
  4. 外部メールクライアントからメールの送受信ができるか確認する
  5. メールが送信されると /home/user/Mail/new にメールが格納される
  6. メールを受信すると /home/user/Mail/cur に受信済みメールが格納される (メールクライアントの設定で「サーバにメールを残さない」にしておくとここには格納されずに削除される)

[Sendmail]動作確認

  1. mail コマンドで送信
    $ mail user@localhost
    Subject: Test mail
    This is test mail.
    .         ← サイドにピリオドを押して終了
    EOT
  2. 受信確認
    $ cat /var/mail/user
    From user@localhost.localdomain Sun Nov 29 01:59:31 2009
    Return-Path: <user@localhost.localdomain>
    
    (中略)
    
    To: user@localhost.localdomain
    Subject: Test mail
    User-Agent: Heirloom mailx 12.4 7/29/08
    MIME-Version: 1.0
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    This is test mail.

2013年4月6日土曜日

[Sendmail]Sendmail 起動

Sendmail はメールの転送を行う SMTP を扱う
  1. chkconfig で sendmail が起動するようになっているか確認する
    # chkconfig --list sendmail
    mail       0:off   1:off   2:on    3:on    4:on    5:on    6:off
  2. 設定ファイル /etc/mail/sendmail.mc から /etc/mail/sendmail.cf を作成できるように sendmail-cf をインストールする
    # yum install sendmail-cf

[PHP][PHPExcel]Cellに属性を付ける

Cellに「折り返して全体を表示」「文字の配置 - 縦位置 = 上詰め」属性を付ける
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>PHP Excel</title>
  </head>
  <body>
<?php
require_once '../PHPExcel_1.7.8/Classes/PHPExcel.php';

// Create new PHPExcel object
printWithTime('Create new PHPExcel object<br/>');
$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->setActiveSheetIndex(0);

// フォント
printWithTime('Set default font<br/>');
$sheet->getDefaultStyle()->getFont()->setName('メイリオ');

// cell に「折り返して全体を表示 (Wrap text)」 属性を付ける
$sheet->getDefaultStyle()->getAlignment()->setWrapText(true);

// cell に「文字の配置 - 縦位置 = 上詰め (vertical = top)」属性を付ける
$sheet->getDefaultStyle()->getAlignment()->setVertical('top');

// Add data
printWithTime('Add data<br/>');
$str = "Hello, world.\nThis is a pen.\nGood bye.";
$sheet->setCellValue('A1', 1);
$sheet->setCellValue('B1', $str);

// Save Excel5 file
$outputFilename = 'php_excel3_output.xls';
printWithTime('Write to Excel5 format<br/>');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($outputFilename);
printWithTime('File written to Excel5 format<br/>');

print "<br/><br/>";
print "Copy $outputFilename to your local PC and open it!<br/>";

/***********************************************************************
 * 時刻と一緒にデバッグ文表示
 ***********************************************************************/
function printWithTime($str)
{
    print "[" . date('H:i:s') . "] " . $str;
}

?>
  </body>
</html>

[PHP][PHPExcel]配列からExcelにデータを書き出す

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>PHP Excel</title>
  </head>
  <body>
<?php
require_once '../PHPExcel_1.7.8/Classes/PHPExcel.php';

// Create new PHPExcel object
printWithTime('Create new PHPExcel object<br/>');
$objPHPExcel = new PHPExcel();

// フォント
printWithTime('Set default font<br/>');
$objPHPExcel->setActiveSheetIndex(0)->getDefaultStyle()->getFont()->setName('メイリオ');

// 配列でデータを準備
$data = array(
    array(1, 'Dog', 'Tokyo', '犬'),
    array(2, 'Cat', 'Chiba', '猫'),
    array(3, 'Bird', 'Saitama', '鳥')
    );

var_dump($data);
print "<br/>";

// Add data
printWithTime('Add data<br/>');
$rowNum = 1;
foreach ($data as $row) {
    $colNum = 0;
    foreach ($row as $d) {
        $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($colNum++, $rowNum, $d);
    }
    $rowNum++;
}

// Save Excel5 file
$outputFilename = 'php_excel2_output.xls';
printWithTime('Write to Excel5 format<br/>');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($outputFilename);
printWithTime('File written to Excel5 format<br/>');

print "<br/><br/>";
print "Copy $outputFilename to your local PC and open it!<br/>";

/***********************************************************************
 * 時刻と一緒にデバッグ文表示
 ***********************************************************************/
function printWithTime($str)
{
    print "[" . date('H:i:s') . "] " . $str;
}

?>
  </body>
</html>

[PHP][PHPExcel]Excelファイルに書き出す

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>PHP Excel</title>
  </head>
  <body>
<?php
require_once '../PHPExcel_1.7.8/Classes/PHPExcel.php';

// Create new PHPExcel object
printWithTime('Create new PHPExcel object<br/>');
$objPHPExcel = new PHPExcel();

// Add some data
printWithTime('Add some data<br/>');
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!');

// Save Excel5 file
$outputFilename = 'php_excel_output.xls';
printWithTime('Write to Excel5 format<br/>');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($outputFilename);
printWithTime('File written to Excel5 format<br/>');

print "<br/><br/>";
print "Copy $outputFilename to your local PC and open it!<br/>";

/***********************************************************************
 * 時刻と一緒にデバッグ文表示
 ***********************************************************************/
function printWithTime($str)
{
    print "[" . date('H:i:s') . "] " . $str;
}

?>
  </body>
</html>

[PHP][PHPExcel]インストール

  1. PHPExcel の Downloads から PHPExcel をダウンロードする
  2. ダウンロードしたファイルを解凍してサーバーに置く