Sending mail from perl in unix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdshafi01
    New Member
    • Jan 2008
    • 36

    Sending mail from perl in unix

    Hi ,
    I am trying to send mail from unix perl.

    I am using following code to send mail.
    It is not triggering mail and also it is not giving any error.

    please tell me any special settings are required or this program should be executed from special user with higher permission or something.

    please tell me.


    what changes i should bring into this program so that this program should work fine.


    [CODE=perl]#!/usr/perl/bin/perl
    print "Content-type: text/html\n\n";

    $title='Perl Mail demo';
    $to='shafi.moha mmed@expt.com';
    $from= 'helpdesk.in@ex pt.com';
    $subject='YOUR SUBJECT';

    open(MAIL, "|/usr/sbin/sendmail -t") || die "unable to open";
    print "hhhh ",<MAIL>,"\ n";

    ## Mail Header
    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";
    ## Mail Body
    print MAIL "This is a test message from Cyberciti.biz! You can write your mail body text here\n";

    close(MAIL);[/CODE]



    shafi
    Last edited by eWish; Feb 18 '08, 04:02 PM. Reason: Please use [CODE][/CODE] tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    The really only thing I see missing is the header for the email itself.

    [CODE=perl]print MAIL "Content-type:text/plain; charset=iso-8859-1\n";[/CODE]

    Here is a sample of one I did playing around with the some time ago.

    [CODE=perl]#! /usr/bin/perl

    use strict;
    use warnings;

    use CGI;
    use CGI::Carp qw/fatalsToBrowser/;

    my $q = CGI->new;

    print $q->header;
    print $q->start_html(-title =>'Mysite.com') ;

    my $mail_path = '/usr/sbin/sendmail -i -t';
    my $email_to = 'xxx@xxx.com';
    my $email_from = 'xxx@xxx.com';
    my $email_subject = 'Testing My Email';
    my $email_message = qq~
    <pre>Hello $email_to,<br>< br>

    Thank you for $email_subject service.<br>
    <a href="http://www.somesite.co m">Some Site</a></pre>
    ~;

    &send_email($em ail_to, $email_from, $email_subject, $email_message) ;

    sub send_email{

    my ($to, $from, $subject, $message) = @_;

    open (my $MAIL, "|$mail_pat h") || print "Could Not Open Mail Program!";

    print $MAIL "Content-type:text/html; charset=iso-8859-1\n";
    print $MAIL "To: $to\n";
    print $MAIL "From: $from\n";
    print $MAIL "Subject: $subject\n\n";
    print $MAIL "$message\n \n";

    close ($MAIL);

    print 'Done. Please check your email box';

    }
    print $q->end_html();

    1;[/CODE]

    Hopefully it will help you! Also, please be sure to use the &#91;CODE]&#91;/CODE] tags when posting code here. Thank you!

    --Kevin

    Comment

    • mdshafi01
      New Member
      • Jan 2008
      • 36

      #3
      Hello here is the sample code with all the parameter filled still it is not working please let me know.


      [CODE=perl]#!/opt/perl/bin/perl -w

      my $send_to = "To: ".'shafi.mohamm ed@capgemini.co m'."\n";

      my $subject="hi";
      my $content="how r u";

      my $from='sandip.s wain@capgemini. com';
      my $sendmail = "/usr/sbin/sendmail -t";
      open(SENDMAIL, "|$sendmail ") or die "Cannot open $sendmail: $!";
      print SENDMAIL "Content-type:text/plain; charset=iso-8859-1\n";
      print SENDMAIL $subject;
      print SENDMAIL "From: $from\n";
      print SENDMAIL $send_to;
      print SENDMAIL "Content-type: text/plain\n\n";
      print SENDMAIL $content;
      close(SENDMAIL) ;

      print "\nProcess completed\n";[/CODE]
      Last edited by eWish; Feb 19 '08, 03:33 PM. Reason: Please use [CODE][/CODE] tags

      Comment

      • krishshan
        New Member
        • Jan 2008
        • 5

        #4
        Have you configure the ip address in the sendmail module...

        Comment

        • npidaparthy
          New Member
          • Feb 2008
          • 14

          #5
          try to run the sendmail command from unix prompt, then you will have some understanding on where exactly the problem lies, ie., unix config, or calling from perl

          also you can try using mailx utility in Unix

          -Nagendra

          Comment

          Working...