メール

Perl で UTF-8 のメール件名を正しく送る

概要

メールの件名や表示名は、MIME対応ライブラリに文字列として渡します。独自のエンコード処理を重ねず、ヘッダー用APIを使います。

実施例

use Email::MIME;

my $mail = Email::MIME->create(
  header_str => [
    From    => 'sender@example.com',
    To      => 'recipient@example.com',
    Subject => '日本語の件名',
  ],
  attributes => { content_type => 'text/plain', charset => 'UTF-8' },
  body_str => '本文です。',
);

注意点

送信前に実在する宛先ではなくテスト用メールボックスで、件名・本文・添付の表示を確認します。

参考: 公式ドキュメント

続きを読む

Linux で、あるドメインの MX レコードを調べる方法

あるドメインで運用されているトップのメールサーバ(ルートのメールサーバ、つまり外部と通信する大元のメールサーバのこと)を調べるには、次のようにコマンドを打てばよい。 nslookup -type=mx amazonaws.com 上の例では amazonaws.com のトップのメールサーバ、つまり Amazon Web Services(AWS)の MX レコードを調べている。preference = 10 などのフィールドは、そのサーバの(通信の)優先度を表す。値が等しい場合は優先度は同じ、値が大きくなるほど優先度は低くなる。
続きを読む

bash で、シェルスクリプトからメールを送る方法

bash では、様々なコマンドを`(バッククオーテーション)で囲むと、そのコマンドを変数に入れたりすることができる。つまり、何か処理を始めたり終わったことをメールで通知する場合は次のようにする。 #!/bin/bash echo 'Subject: Started: your_program' > temp.txt echo 'To: yourname@yourdomain.com' >> temp.txt echo 'Started.' >> temp.txt cat temp.txt | `sendmail -t -i` rm temp.txt your_program (←ここに処理したいプログラム名(時間がかかるもの)を書く) echo 'Subject: Completed: your_program' > temp.txt echo 'To: yourname@yourdomain.com' >> temp.txt echo 'Completed.' >> temp.txt cat temp.txt | `sendmail -t -i` rm temp.txt 大事なのは cat temp.txt | `sendmail -t -i`
続きを読む

Perl で、SpamAssassin を使って迷惑メールを処理する。

オープンソースで迷惑メールの処理するソフトといえば SpamAssassin (直訳するとスパム暗殺者)が有名だ。何しろ spamassassin.apache.org というサイト名からも分かるとおり、Apache Foundation 謹製なのだ。

その SpamAssassin、Perl で書かれている。なので当然 Perl のライブラリが用意されており、Perl 使いの人は簡単に自身のプログラム中から呼び出すことができる。SpamAssassin 自体はメールサーバのフロントエンドで、Procmail なんかと一緒に使うことが多いだろうから、あまり自身のプログラム中から呼び出すようなことはないとは思うのだが、何かのときに役に立つかもしれないから、とりあえず以下にサンプルを載せておこう(というかこのサイトのすべての内容自体、自分のためのメモなのでご了承を)。

続きを読む

Requirements for Mobile Phones (English)

I would like to propose the unique requirements for mobile phones. The following items are significant considerations under mobile site development, especially in i-mode phones.

  1. UserAgent type that is, a Browser Name of CGI environments which is sent by a mobile phone to a web server.
  2. Range of IP addresses in each mobile operator, which is required to judge whether a user is using a mobile phone or a PC in order to support both of them in the web site. In Japanese mobile phone operators, they are using their specific proxy servers, which are accompanying with the specific range of IP addresses.
  3. Screen Size of each mobile phone, especially the size of width, which would be found in a CGI environment UserAgent in the latest phones
  4. Image Format such as JPEG, PNG and GIF supported by each mobile phone
  5. Maximum Size of Receiving Bytes per a page
  6. Difference of Handling Colors in each mobile phone
  7. Difference of Smilies (Picture Icons) both in each mobile phone and operator
  8. Name Convention of an E-mail Address in each operator (Japanese i-mode e-mail system violates RFC).
  9. Available Character Set in each operator (Shift-JIS, UTF-8 in Japan)
  10. Available Character Types in each operator (such as ZENKAKU, HANKAKU in Japan)
  11. Available Input Method in each operator (Access-key concept in CHTML)
  12. Support of Native Functions of a mobile phone (i.e. switching a browser screen to an e-mail application with 'mailto:' in CHTML at a single screen of a mobile phone, launching Java application, and directly calling to someone with 'tel:' in CHTML, etc)
  13. Difference in between Official Sites and Unofficial Sites (The acquiring information is different)
  14. Session Management because we cannot use cookie in i-mode phones
続きを読む

Linux で、postfix を使ってイントラネットからメールを送信する方法

Fedora Core 6 の場合、デフォルトの MTA (Mail Transfer Agent) は sendmail となっている。で、実験用 Linux サーバをイントラネットに置いてメールを出そうとしたら、Connection Refused というエラーメールが返ってきてしまった。どうもイントラネットから外へのポート 25 がふさがっているから外部のホストにつながらないらしい。

そこで sendmail の設定をいろいろと調べてはみたものの、やっぱり sendmail.mc とか sendmail.cf は呪いたくなります。結局挫折。今までの人生の中で何度か sendmail を設定する機会があったが、いずれもよくわからなかった。

続きを読む

Perl で、Base64 のファイルをデコードして元のバイナリファイルに戻す方法

メールに添付されてきた .pdf ファイルが巨大すぎてメールサーバによって分割されて自分のところに配信された結果、Base64 のデータがバラバラに届いたケースを考える。

このような場合は Perl が活躍する。まず、バラバラになった Base64 のファイルは仕方がないのでエディタを使って手作業でくっつけて 1つのファイルにする。そのあと、以下のコマンドを実行すればよい。

使用するモジュール use MIME::Base64; perl -MMIME::Base64 -ne 'print decode_base64($_)' < くっつけたBase64ファイル名.txt > 出力ファイル名.pdf (上は、すべて1行に書く)
続きを読む

Perl で、添付ファイル付きのメールを送る方法

Perl で、

open(SEND, '/usr/sbin/sendmail') || die;

などとせずにモジュールを使ってメールを送信するには、以下のようにする。

使用するモジュール


use Net::SMTP;
use MIME::Entity;
#!/usr/bin/perl use Net::SMTP; use MIME::Entity; # Settings our $SENDER = '送信元のメールアドレス'; our $RECIPIENT = '送信先のメールアドレス'; our $SUBJECT = 'タイトル'; our $ATTACHED_FILE_1 = '/home/youraccount/添付ファイル名その1.jpg'; our $ATTACHED_FILE_2 = '/home/youraccount/添付ファイル名その2.jpg'; # Create object my $smtp=Net::SMTP->new('localhost',
                    HELLO=>'yourmailsever.yourdomain.com');
# Built headers $smtp->mail($SENDER); # Sender $smtp->to ($RECIPIENT); # Receiver # Built Data (Create data by MIME::Entity) $smtp->data(); my $mime = MIME::Entity->build(
        From    =>    $SENDER   , # Sender   (data)
        To      =>    $RECIPIENT, # Receiver (data)
        Subject =>    $SUBJECT  , # Subject
        Data    => ['']);         # body
# Attached file $mime->attach(
    Path     => $ATTACHED_FILE_1,
    Type     => 'image/jpeg',
    Encoding => 'Base64'
); # Attached file $mime->attach(
    Path     => $ATTACHED_FILE_2,
    Type     => 'image/jpeg',
    Encoding => 'Base64'
); # Attached file (Text) # $mime->attach( # Path => $ATTACHED_FILE, # Type => 'text/plain', # Encoding => '-SUGGEST' #); $smtp->datasend($mime->stringify); # transfer strings # Data termination and send mail $smtp->dataend(); #Quit SMTP connection $smtp->quit; # for debug print "Sender : $SENDER\n"; print "Recipient : $RECIPIENT\n"; print "Attached : $ATTACHED_FILE\n"; 1;
続きを読む

Perl での、文字列の扱い:<br />シングルクォートとダブルクォーテーションの違い

まずはこのエントリのタイトルにある「シングルクォート ('…') とダブルクォーテーション ("…")」、なぜ 「クォート」 と 「クォーテーション」 なのか? という細かい^^;質問は置いておいて、Perl で文字列を表現する方法は実はいくつかあるのだということに注意したい。基本的にはシングルクォートとダブルクォーテーションの違いがある。シングルクォートの場合は、その中に書いた文字列はそのまま文字列として Perl に解釈される。特にダブルクォーテーションの場合は、文字列の中に変数を埋め込める。これは便利な反面、やっかいなことになることがある。例えばメールアドレスをダブルクォーテーションで囲んだ場合には、メールアドレスには必ずアットマーク 「@」 があり、この文字に Perl が出くわしたときに 「配列 」と解釈してしまうため、多くの場合は自分の予期しない結果か、エラーとなる。

my $str = 'Perl では'; print $str . '文字列をシングルクォートで囲むのが基本'; print "$str このように、ダブルクォーテーションで囲むことができる";

なお、文字列の連結にはピリオド「.」を使う。

print '文字列を' . '連結する';

また、'…' と "…" はそれぞれ以下のように書くことができる。

print q|これはシングルクォートで囲ったのと同じ意味|; print qq|これはダブルクォーテーションで囲ったのと同じ意味|; print q/…qの後にくる記号はスラッシュとかも使える/; print qq/…同じく qqの後にくる記号はスラッシュとかも使える/; my $mail = 'support@twinkle.cc'; print $mail; →これはきちんと support@twinkle.cc と表示される。 $mail = "support@twinkle.cc"; print $mail; → こちらは support.cc と表示される。なぜなら、@twinkle が変数とみなされるからである。 → use strict を使ったらエラーとなる。
続きを読む
メール を購読