Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Send e-mail using perl

by roho (Monsignor)
on Aug 07, 2012 at 12:53 UTC ( #985967=note: print w/ replies, xml ) Need Help??


in reply to Send e-mail using perl

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."


Comment on Re: Send e-mail using perl
Download Code
Re^2: Send e-mail using perl
by rbala (Acolyte) on Aug 08, 2012 at 07:18 UTC
    Hi,

    Thanks for your code.

    Sorry for not mentioning the OS . I use linux ubuntu- 12.04.

    Before trying this code, I just wanted to learn how to configure my mail server in linux , before running the code. I just tried the following code by doing the configuration as given in the CPAN documentation of Mail::Sendmail module.
    #! /usr/bin/perl -w use Mail::Sendmail; use Mail::Sendmail qw(sendmail %mailcfg); $mail{smtp} = qw(imap.googlemail.com); $mail{port} = 993; %mail = ( To => 'xxx@gmail.com', From => 'yyy@gmail.com', Message => 'This is a very short message' ); if ( sendmail(%mail) ) { print "OK.\n"; } else { die $Mail::Sendmail::error; } print "OK. Log says:\n", $Mail::Sendmail::log;
    Upon running this code, process continuously runs without any result.....What might be the problem with this code.?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985967]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-12-18 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls