Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Send e-mail using perl

by roho (Abbot)
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 "" 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/', '.xlsx' => 'application/', }; my $mime = MIME::Lite->new( 'To' => '', 'Cc' => ',', 'From' => '"Us" <>', '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 ""; # 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', '', '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."

Replies are listed 'Best First'.
Re^2: Send e-mail using perl
by rbala (Acolyte) on Aug 08, 2012 at 07:18 UTC

    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(; $mail{port} = 993; %mail = ( To => '', From => '', 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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2016-09-30 06:12 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (562 votes). Check out past polls.