1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
use strict; use warnings; use POSIX qw ( strftime ); &process_emails; # E-Mails versenden use Email::Stuffer; use Email::Sender::Transport::SMTP; # Anmeldung an einem SMTP-Server use utf8; # korrekte Darstellung von Sonderzeichen sub process_emails { my $email_to = undef; my $email_subject = 'subject'; my $email_att = 'test.xlsx'; my @email_to = ( 'Stefan_S <stefan_s@domain.de>' ); foreach $email_to ( @email_to ) { Email::Stuffer -> subject ( $email_subject.&now ) -> to ( $email_to ) -> from ( 'Sender <sender@domain.de>' ) -> text_body ( "body\n" ) -> attach_file ( $email_att ) -> transport ( 'SMTP', { host => 'my_smtp_server.de', port => 25, sasl_username => 'sender@domain.de', sasl_password => 'passwort', }) -> send; }; } # aktuelles Datum und aktuelle Uhrzeit ermitteln sub now { my $now = strftime ( "%d.%m.%y", localtime ) . " " . strftime ( "%H:%M:%S", localtime ) . " : "; return $now; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
use IO::All; use Email::MIME; my @parts = ( Email::MIME->create( attributes => { filename => "test.xlsx", content_type => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", encoding => "Base64", name => "test.xlsx", }, body => io( "test.xlsx" )->binmode()->all, ), Email::MIME->create( attributes => { # filename => "test1.ods", content_type => "application/vnd.oasis.opendocument.spreadsheet", encoding => "Base64", name => "test1.ods", }, body => io( "test1.ods" )->binmode()->all, ), # gerates DEFECT attachment Email::MIME->create( attributes => { # filename => "DEF-test1.ods", content_type => "application/vnd.oasis.opendocument.spreadsheet", encoding => "Base64", name => "DEF-test1.ods", }, body => io( "test1.ods" )->all, ), Email::MIME->create( attributes => { content_type => "text/plain", disposition => "attachment", encoding => "quoted-printable", charset => "US-ASCII", }, body_str => "Hello there!", ), ); my $email = Email::MIME->create( header_str => [ From => 'casey@geeknest.com' ], parts => [ @parts ], ); # standard modifications $email->header_str_set( To => 'test@local' ); open (my $mh, '>', 'test.eml'); binmode $mh; print $mh $email->as_string; close $mh;
1 2 3 4 5 6 7 8 9
sub _slurp { my $file = shift; local $/ = undef; local *SLURP; open( SLURP, "<$file" ) or return undef; my $source = <SLURP>; close( SLURP ) or return undef; \$source; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
sub attach_file { my $self = shift; my $body_arg = shift; my $name = undef; my $body = undef; # Support IO::All::File arguments if ( Params::Util::_INSTANCE($body_arg, 'IO::All::File') ) { $name = $body_arg->name; $body = $body_arg->all; # Support file names } elsif ( defined $body_arg and -f $body_arg ) { $name = $body_arg; $body = _slurp( $body_arg ) or return undef; # That's it } else { return undef; } # Clean the file name $name = File::Basename::basename($name) or return undef; # Now attach as normal $self->attach( $body, name => $name, filename => $name, @_ ); }