Connecting Tech Pros Worldwide Help | Site Map

Sending mail from perl in unix

Member
 
Join Date: Jan 2008
Posts: 36
#1: Feb 18 '08
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.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/perl/bin/perl
  2. print "Content-type: text/html\n\n";
  3.  
  4. $title='Perl Mail demo';
  5. $to='shafi.mohammed@expt.com';
  6. $from= 'helpdesk.in@expt.com';
  7. $subject='YOUR SUBJECT';
  8.  
  9. open(MAIL, "|/usr/sbin/sendmail -t") || die "unable to open";
  10. print "hhhh ",<MAIL>,"\n";
  11.  
  12. ## Mail Header
  13. print MAIL "To: $to\n";
  14. print MAIL "From: $from\n";
  15. print MAIL "Subject: $subject\n\n";
  16. ## Mail Body
  17. print MAIL "This is a test message from Cyberciti.biz! You can write your mail body text here\n";
  18.  
  19. close(MAIL);


shafi
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Feb 18 '08

re: Sending mail from perl in unix


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

Expand|Select|Wrap|Line Numbers
  1. print MAIL "Content-type:text/plain; charset=iso-8859-1\n";
Here is a sample of one I did playing around with the some time ago.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl 
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8.  
  9. my $q = CGI->new;
  10.  
  11. print $q->header; 
  12. print $q->start_html(-title =>'Mysite.com'); 
  13.  
  14. my $mail_path         = '/usr/sbin/sendmail -i -t';
  15. my $email_to        = 'xxx@xxx.com';
  16. my $email_from        = 'xxx@xxx.com';
  17. my $email_subject    = 'Testing My Email';
  18. my $email_message     = qq~ 
  19. <pre>Hello $email_to,<br><br>
  20.  
  21. Thank you for $email_subject service.<br>  
  22. <a href="http://www.somesite.com">Some Site</a></pre>
  23. ~;
  24.  
  25. &send_email($email_to, $email_from, $email_subject, $email_message);
  26.  
  27. sub send_email{
  28.  
  29.     my ($to, $from, $subject, $message) = @_;
  30.  
  31.        open (my $MAIL, "|$mail_path") || print "Could Not Open Mail Program!";
  32.  
  33.            print $MAIL "Content-type:text/html; charset=iso-8859-1\n";
  34.            print $MAIL "To: $to\n";
  35.            print $MAIL "From: $from\n";
  36.            print $MAIL "Subject: $subject\n\n";
  37.            print $MAIL "$message\n\n";
  38.  
  39.    close ($MAIL);
  40.  
  41.    print 'Done.  Please check your email box';
  42.  
  43. print $q->end_html();
  44.  
  45. 1;
Hopefully it will help you! Also, please be sure to use the [CODE][/CODE] tags when posting code here. Thank you!

--Kevin
Member
 
Join Date: Jan 2008
Posts: 36
#3: Feb 19 '08

re: Sending mail from perl in unix


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


Expand|Select|Wrap|Line Numbers
  1. #!/opt/perl/bin/perl -w
  2.  
  3. my $send_to  = "To: ".'shafi.mohammed@capgemini.com'."\n";
  4.  
  5. my $subject="hi";
  6. my $content="how r u";
  7.  
  8. my $from='sandip.swain@capgemini.com';
  9. my $sendmail = "/usr/sbin/sendmail -t";
  10. open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
  11. print SENDMAIL "Content-type:text/plain; charset=iso-8859-1\n";
  12. print SENDMAIL $subject;
  13. print SENDMAIL "From: $from\n";
  14. print SENDMAIL $send_to;
  15. print SENDMAIL "Content-type: text/plain\n\n";
  16. print SENDMAIL $content;
  17. close(SENDMAIL);
  18.  
  19. print "\nProcess completed\n";
Newbie
 
Join Date: Jan 2008
Posts: 5
#4: Feb 19 '08

re: Sending mail from perl in unix


Have you configure the ip address in the sendmail module...
Newbie
 
Join Date: Feb 2008
Posts: 14
#5: Feb 19 '08

re: Sending mail from perl in unix


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
Reply