8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PerlでGmailから添付つきメール

Last updated at Posted at 2012-03-16
send_mail_with_attachment.pl
# !/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Path::Class qw/file/;
use Encode;
use Email::Sender::Simple qw/try_to_sendmail/;
use Email::MIME;
use Email::MIME::Creator;
use Email::Sender::Transport::SMTP::TLS;

my $username = 'gmail-user-name';
my $password = 'gmail-password';

my $from     = "$username\@gmail.com";
my $to       = 'example@example.com';

my $subject  = 'subject';
my $body     = 'body';

my $file     = file('/path/to/attachment');

my $email = Email::MIME->create(
    header => [
        From    => $from,
        To      => $to,
        Subject => Encode::encode('MIME-Header-ISO_2022_JP', $subject),
    ],
    parts => [
        # メール本文
        Email::MIME->create(
            attributes => {
                content_type => 'text/plain',
                charset      => 'iso-2022-jp',
                encoding     => '7bit',
            },
            body_str => $body,
        ),
        # 添付ファイル
        Email::MIME->create(
            attributes => {
                filename     => $file->basename,
                encoding     => 'base64',
                disposition  => "attachment",
            },
            body => scalar $file->slurp,
        ),
    ],
);

try_to_sendmail(
    $email,
    {   
        transport => Email::Sender::Transport::SMTP::TLS->new(
            host     => 'smtp.gmail.com',
            port     => 587,
            username => $username,
            password => $password,
        )
    }
) or do { ... };
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?