Created
June 27, 2012 12:52
-
-
Save noqisofon/3003905 to your computer and use it in GitHub Desktop.
Perl でメール送信してみた感じ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use utf8; | |
| use warnings; | |
| use Encode; | |
| use MIME::Base64; | |
| use Net::SMTP; | |
| use Authen::SASL; | |
| use Data::Dumper; | |
| sub main() { | |
| my $mail_user = '[email protected]'; | |
| my $mail_password = 'password'; | |
| my $mail_host = 'smtp.example.net'; | |
| my $mail_from = '[email protected]'; | |
| my $mail_to = '[email protected]'; | |
| my $mail_subject = 'テストです'; | |
| $mail_subject = MIME::Base64::encode( encode( 'iso-2022-jp', $mail_subject ) ); | |
| # メールのヘッダーを作成します。 | |
| my $mail_header = << '__MAIL_HEADER__'; | |
| From: $mail_from | |
| To: $mail_to | |
| Subject: $mail_subject | |
| Mime-Version: 1.0 | |
| Content-Type: text/plain; charset=ISO-2022-JP | |
| Content-Transfer-Encoding: 7bit | |
| __MAIL_HEADER__ | |
| ; | |
| my $mail_body = << '__MAIL_BODY__'; | |
| こんにちはこんにちは | |
| __MAIL_BODY__ | |
| ; | |
| $mail_body = encode( 'iso-2022-jp', $mail_body ); | |
| my $smtp = Net::SMTP->new( $mail_host, | |
| Hello => $mail_host, | |
| Port => 587, | |
| Timeout => 20, | |
| Debug => 1 ); | |
| unless ( $smtp ) { | |
| die "can't connect SMTP server:$!"; | |
| } | |
| $smtp->auth( $mail_user, $mail_password ) or die "can't login smtp server"; | |
| $smtp->mail( $mail_from ); | |
| $smtp->to( $mail_to ); | |
| $smtp->data(); | |
| $smtp->datasend( "$mail_header\n" ); | |
| $smtp->datasend( "$mail_body\n" ); | |
| $smtp->dataend(); | |
| $smtp->quit; | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment