Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You don't say which OS you're using, but here is a copy of code I use regularly to send emails with attachments on MS Windows. Note that attachments too large to send are skipped and attachments that end in ".pl" are temporarily renamed to end in ".txt" to avoid rejection by email filters. Replace "smtp.yourisp.net" with your smtp server. Hope this helps.

#!/usr/bin/perl use strict; use warnings; use MIME::Lite; use File::Copy qw( move ); use File::Basename; fileparse_set_fstype($^O); # Specify OS type. my $suffix_pattern = qr/\.[^.]*/; my $mime_types = { '.txt' => 'text/plain', '.jpg' => 'image/jpeg', '.gif' => 'image/gif', '.zip' => 'application/zip', '.doc' => 'application/msword', '.docx' => 'application/msword', '.xls' => 'application/vnd.ms-excel', '.xlsx' => 'application/vnd.ms-excel', }; my $mime = MIME::Lite->new( 'To' => 'me@example.com', 'Cc' => 'you@example.com,them@example.com', 'From' => '"Us" <us@example.com>', 'Subject' => 'Email using Perl', 'Type' => 'text/plain', 'Data' => 'Text of email message goes here', ); my $dir = "/tmp"; opendir(DIR, $dir) or die "Error opening directory: $dir\n"; print "\nFormatting email ... \n"; print "\nAttaching files: "; my ($full_name_old, $full_name_new); while (my $file_name = readdir(DIR)) { my $full_name = "$dir/$file_name"; next if $file_name =~ /^\.\.?/; # Skip 'dot' files. next if $file_name =~ /.bat$/; # Skip batch files. next if $file_name eq "Huge.zip"; # File exceeds email max. next if -d $full_name; # Skip directories. $full_name_old = $full_name_new = ''; if ($file_name =~ /\.pl$/) { # Add txt extension for email filter +s. $file_name .= '.txt'; $full_name_old = $full_name; $full_name_new = $full_name . '.txt'; $full_name = $full_name_new; move($full_name_old,$full_name_new); } print "$file_name, "; my ($file, $path, $ext) = fileparse($full_name, $suffix_pattern); my $mime_type = $mime_types->{$ext} || 'text/plain'; $mime->attach( Type => $mime_type, Path => $full_name, Filename => $file_name, Disposition => 'attachment', ); } closedir(DIR); print "\n\nSending email ... "; MIME::Lite->send( 'smtp', 'smtp.yourisp.net', 'Timeout' => 90 ); $mime->send; opendir(DIR, $dir) or die "Error opening directory: $dir\n"; while (my $file_name = readdir(DIR)) { my $full_name = "$dir/$file_name"; if ($full_name =~ /\.pl\.txt$/) { # Remove txt extensions added ab +ove. $full_name_old = substr($full_name,0,length($full_name)-4); move($full_name,$full_name_old); } } closedir(DIR); print "\n\nFiles in '$dir' sent to recipients as attachments\n";

"Its not how hard you work, its how much you get done."


In reply to Re: Send e-mail using perl by roho
in thread Send e-mail using perl by rbala

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-26 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found