Sending Email in Perl

Discussion in 'Perl' started by pradeep, Aug 21, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    As Perl programmer we might want to shoot off a mail for some reason or the other, especially if we've used Perl for the web. Perl being such a powerful language, it's can do virtually anything you want to do with it. Sending a mail is a piece of cake, be it using sendmail or using SMTP. CPAN is a vast reposiroty of modules, modules for every task you might be able to think.

    Sending Mails using Sendmail



    Sendmail [www.sendmail.org] is a mail transfer agent that is a well-known project of the open source, free software and Unix communities, which is distributed both as free software and proprietary software, you can find it on most Unix/Linux installations.

    Here's an example of sending mails with sendmail.
    Code:
      #!/usr/bin/perl
      
      $path_to_sendmail = '/usr/sbin/sendmail -t'; # this is the usual location of the sendmail program, 
                            # see if your's is somewhere else
      open MAIL,"|$path_to_sendmail"; # open a pipe to the program
      print MAIL q(From: deepz@go4expert.com
      To: HeyYou@ThatDomain.com
      Subject: Whats up?
      
      See, I mailed you through my program.
      );
      
      close MAIL;
      
    That's it. Wasn't that easy!! Let's make it easier by using a Perl module Email::Send::Sendmail

    Code:
      use Email::Send;
      
      my $message = q(From: deepz@go4expert.com
      To: HeyYou@ThatDomain.com
      Subject: Whats up?
      
      See, I mailed you through my program.
      );
      Email::Send->new({mailer => 'Sendmail'})->send($message);
      

    Sending Mails Using SMTP



    Most of you must be knowing what SMTP is, is a protocol for sending mails, read more here.
    For sending mails using SMTP we'll use a very popular Perl module Net::SMTP.

    Code:
      #!/usr/bin/perl
      
      use Net::SMTP;
      
      $smtp = Net::SMTP->new('email.go4expert.com');
      
      $smtp->mail('deepz@go4exper.com');
      $smtp->to('HeyYou@ThatDomain.com');
      
      $smtp->data();
      $smtp->datasend('To: HeyYou@ThatDomain.com');
      $smtp->datasend('Subject: Whats up?');
      $smtp->datasend("\n");
      $smtp->datasend("See, I mailed you through my program.\n");
      $smtp->dataend();
      
      $smtp->quit;
      
    So, I hope it was helpful, some of you might want to checkout the module Net::SMTP_auth which helps you autheticate to the SMTP server before sending any mails, most SMTP servers today aren't open relays.
     
  2. daveritwick

    daveritwick New Member

    Joined:
    Aug 20, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    A very very useful tutorial man Thanks for that. But tell me that can we also receive the mail back when some on reply on the perl mail to our actual email address
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. amu1983

    amu1983 New Member

    Joined:
    Oct 25, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://theindianmba.com/
    Great! I was just looking for something like this for my company phpBB forum. Sadly, I didn't get a permission from IT-Admin to use the company mail server for the forum (due to potential high traffic), so this one would be to rescue. Any comments on how Sendmail would perform in a high volume environment?
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Sendmail performs well in high volume environments.
     
  7. obafemi59

    obafemi59 New Member

    Joined:
    Dec 23, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    lagos
    Hello can you please explain better to me because im a newbie here
     
  8. erigits

    erigits New Member

    Joined:
    Aug 5, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    meru.kenya
    hello.i like ur interests.wat do u take in uni/colle? to do with comp sci or it
     
  9. Tobiasgar

    Tobiasgar New Member

    Joined:
    Aug 29, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.mspy.com
    College and university are not the main things.. you get better knowledge and experience outside their walls :nice:
     
  10. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.expertsguide.info
    @pradeep, thatnks for this info.

    I had written something similar but had a problem when my manager asked me a question that what if sendmail itself is not installed on a server. He asked me to write something which is very basic and works with basic perl modules.

    So my question to you. What if sendmail is not installed?
     
  11. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    If sendmail is unavailable use SMTP.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice