Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Send e-mail using perl

by rbala (Acolyte)
on Aug 07, 2012 at 07:48 UTC ( #985904=perlquestion: print w/ replies, xml ) Need Help??
rbala has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I just want to know how to send e-mail from perl. I just tried the following code :
#! /usr/bin/perl -w sub sendemail { my ($to, $from, $subject, $message) = @_; my $sendmail = '/usr/lib/sendmail'; open(MAIL, "|$sendmail -oi -t"); print MAIL "From: $from\n"; print MAIL "To: $to\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; close(MAIL); } &sendemail("myemail@gmail.com", "myemail@yahoo.com", "Test perl mail", + "This is the test mail from perl");
it returned me the following errors:

Name "main::gmail" used only once: possible typo at ./mail.pl line 15.

Name "main::yahoo" used only once: possible typo at ./mail.pl line 15.

Pls help me... Thanks in advance !!!

Comment on Send e-mail using perl
Download Code
Re: Send e-mail using perl
by zentara (Archbishop) on Aug 07, 2012 at 07:55 UTC
    Try backslashing the @ in your mail addresses, or single quote the strings instead of double quoting.
    &sendemail("myemail\@gmail.com", "myemail\@yahoo.com", "Test perl mail +",

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks a lot zentara for ur suggestion. Now the script does not throw any errors, but executing the script does not show anything in terminal window.The terminal was struck for a while without any response after sometime , prompt returned.

      Checked the mail inbox but mail not arrived.

      I have got one more doubt also, how authentication can done ie. entering mail password for sending/receiving mails. Pls advise....
Re: Send e-mail using perl
by Anonymous Monk on Aug 07, 2012 at 07:59 UTC

    double quotes ( "myemail@example.com" ) interpolate, while single quotes ( 'myemail@example.com' ) do not

    Use single quotes, perlintro and Modern Perl explain interpolation.

      use strict; would all most certainly catch this error!
Re: Send e-mail using perl
by Jenda (Abbot) on Aug 07, 2012 at 08:19 UTC

    Considering the number of mail related modules on CPAN it surprises me that someone still attempts to shell out and use an external executable. Without testing whether the executable exists, whether it was started successfully, whether it reported any errors ...

    Unrelated note: drop the &. It's not necessary in this case and means something else than people tend to think in other cases.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Jenda,

        Unrelated note: drop the &. It's not necessary in this case and means something else than people tend to think in other cases.

      Could you explain this or give a reference. I see this comment a lot, and have never experienced any side-effect of using the '&'. I use the '&' to visually distinguish between in-script 'sub's and 'sub's that I have no control over via use/require/Begin.

      When you haven't looked at code in 2-5 years and now need to modify it, small visual aids do help!

      Thank you

      "Well done is better than well said." - Benjamin Franklin

        It's the old Perl4-style sub call syntax, and means "ignore prototype" and (if no arguments are passed), "make current @_ visible to subroutine" (perlsub)

        Its use is discouraged but it doesn't seem to be harmful. Personally, I just view it as ugly and a hint that "this user learned Perl from tutorials written in 1995"

Re: Send e-mail using perl
by aitap (Deacon) on Aug 07, 2012 at 08:28 UTC
    Is your MTA set up to deliver mail to other hosts, not locally? Check its logs.
    Sorry if my advice was wrong.
      HI aitap..I am not much aware of this area of setting up mail client in linux. Can you please guide me on this ?

        Sounds like it's time to talk to your systems administrator.

        What OS do you use? Try google'ing "<operating system name> MTA send mail". Maybe it will be easier to use Net::SMTP and send mail using existing mail servers (e.g. smtp.google.com).
        EDIT: some terms
        Sorry if my advice was wrong.
Re: Send e-mail using perl
by roho (Monsignor) on Aug 07, 2012 at 12:53 UTC
    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."

      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: perlquestion [id://985904]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-16 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls