間違いだらけの備忘録

このページの内容は無保証でありこのページの内容によって直接、または間接に損害を受けられたとしても私は責任を取りません。

Perlでメールを送る その2

http://www.site-cooler.com/kwl/perl/smtp.htm
http://www.site-cooler.com/kwl/perl/


機能ダウン版(日本語メール対応不可)
perlはよく知らないので、本能のおもむくままに改造

cat smtp.pl
#05/07/27 
use     Socket;
if ( $#ARGV != 3 ) {
	die("Argv:SmtpServer FromMailaddress Subject ToMailaddress") ;
}
$server = $ARGV[0] ;
$from = $ARGV[1] ;
$subject = $ARGV[2] ;
$to = $ARGV[3] ;
while ( $line = <STDIN> ){
$data .= $line ;
}
$send_data = 'X-Mailer: SMTP Sample' . "\n";
$send_data .= 'MIME-Version: 1.0' . "\n";
$send_data .= 'Content-Type: text/plain; charset=iso-2022-jp' . "\n";
$send_data .= 'From: '. $from . '<'. $from .'>' . "\n";
$send_data .= 'To: '. $to . "\n";
$send_data .= 'Subject: ' . $subject . "\n";
$send_data .= "\n";
$send_data .= $data;
$port = getservbyname('smtp','tcp');

$struct = sockaddr_in($port,inet_aton($server)); 
socket(SH, PF_INET, SOCK_STREAM, 0)
|| die("socket make error $!") ; 

connect(SH, $struct )
|| die("connect error1 $!") ; 

select(SH); $| = 1; select(STDOUT);

$respons = <SH> ;
unless($respons =~ /^220/) {
close(SH); die("connect error2 $!") ; 
}
$command = "HELO $server\n";
print SH $command ;
$respons = <SH> ;
&decode(\$respons) ;
unless($respons =~ /^250/){
close(SH); die("HELO cmd error $!") ; 
}
$command = "MAIL FROM:$from\n";
print SH $command ; 
$respons = <SH> ;
&decode(\$respons) ;
unless($respons =~ /^250/){
print SH "RSET\n"; close(SH);
die("MAIL cmd error $!") ; 
}
$command = "RCPT TO:$to\n";
print SH $command ;
$respons = <SH> ;
&decode(\$respons) ;
unless($respons =~ /^25[0|1]/){
print SH "RSET\n"; close(SH);
die("RCPT cmd error $!") ; 
}
$command = "DATA\n";
print SH $command ;
$respons = <SH> ;
&decode(\$respons) ;
unless($respons =~ /^354/){
print SH "RSET\n"; close(SH);
die("DATA cmd error $!") ; 
}
#&jcode'convert(*send_data,'jis');
$command = "$send_data\n.\n";
print SH $command ;
$respons = <SH> ;
&decode(\$respons) ;
unless($respons =~ /^250/){
print SH "RSET\n"; close(SH);
die("main or head send error $!") ; 
}
$command = "QUIT\n";
print SH $command ;
close(SH); select(STDOUT);

print "MAIL send OK! \n";
sub decode{

my $inf = $_[0];
$$inf =~ s/\x0D\x0A|\x0D|\x0A/\n/g;
}

メール差出人(From)の変更(Solaris8 mailx)
http://oshiete1.goo.ne.jp/kotaeru.php3?q=1268128

solaris9から、-rははなくなりました。
ちなみに、Linux等ではこの-rに対応するオプションは元から存在しないと思います。

ほほー
追記
http://d.hatena.ne.jp/init9/20080409/1207703509

$ echo hoge|mail -s "test" "init9@gmail.com" -- -f "hogefuga@init9.com"
$ echo "ほげ" |jmail -s "テスト" "init9@gmail.com" -- -f "hogefuga@init9.com"

hogefuga@init9.com を送信者としてメールが届く

http://www.wingnotes.net/linux_server/mail_command_from.html

シェル(バッチ)を組んで、その中でメールコマンドによりメール送信する場合、本文はパイプ"|"でmailコマンドに引き渡してあげると良い。

例)
echo "これは本文です" | /bin/mail -s MailSubject -c cc_user@hogehoge.com to_user@hogehoge.com -- -f from_user@hogehoge.com

シェルのmailコマンドにFromを指定した時の問題

メール送信するサーバのドメインに関係なく、勝手なドメインのメールアドレスをFromを指定することができるわけだが、これは送信先アドレスのメールサーバがスパムフィルタが強いと、送信もとホスト(IPアドレス)とそのFromに指定されたメールアドレスのドメインが一致しないことにより、スパムと扱われる可能性が非常に高いので、Fromの設定の仕方は十分に注意する必要がある。

めも

このページにはhatena以外のサービスからのコンテンツが埋め込まれています。 hatenaによりGoogle AdSense 広告が埋め込まれています。