Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending email automatically with Perl 2

Status
Not open for further replies.
Jul 14, 2003
116
CA
Hi,

I have an html form that when submitted it calls a perl script. I want this script to then automatically email me with these results. I believe I have to use the mailx command but it doesn't seem to be working. Also this script is running on a linux server. Thanks for any help that anybody can give me.
 
You should use perl's sendmail module. It works really well and is easy to use. Here is a brief example:
Code:
  use Mail::Sendmail;

  $eadd = "sean4e\@yahoo.com";  #I think you have to backslashe the @
  $message = "Hello this is a test email";
  $subject = "Test";
  $from = &quot;Your Name <you@yoursp.com>&quot;;
  
  %mail = ( To => &quot;$eadd&quot;,
            From => &quot;$from&quot;,
            Subject => &quot;$subject&quot;,
            Message => &quot;$message&quot;
  );     
                                                                                                                    
  sendmail(%mail) or die $Mail::Sendmail::error;

I hope this helps, get back to me if it doesn't. Oh yeah and if it says that it can't find the module download it from CPAN. If you are using linux you can just type this in the terminal to download it:
perl -MCPAN -e &quot;install CGI::SESSION&quot;
This will download and install the module.

Best of luck.
Sean. :)
 
Woops sorry I was supposed to type this to for the module download:
perl -MCPAN -e &quot;install Mail::Sendmail&quot;
Remember you must type this from the linux terminal. If however you aren't hosting the site yourself and you are using a webhost, don't worry. They will almost certainly have sendmail installed.

If they don't, don't hesitate to phone them up and tell them to install it. It doesn't take longer than 30 seconds.

Get back to me if you run into any walls.

Sean.
 
#!/usr/bin/perl

open (MAIL, '| /usr/sbin/sendmail -t');

print MAIL &quot;To: scan\@btinternet.com\n&quot;;
print MAIL &quot;From: me\@my_isp.co.uk\n&quot;;
print MAIL &quot;Subject: subject here\n&quot;;
print MAIL &quot;blah blah blah...&quot;;

close MAIL;



Kind Regards
Duncan
 
Sean4e - you can avoid having to escape the @ symbol by using single quotes:
Code:
$eadd = 'sean4e@yahoo.com';

Duncan, opening pipes to sendmail is not considered to be a good thing, unless you're very careful about the data that's going into it, in which case you're better off using a module to do that for you anyway.

 
Is there anyway to include an attachment to the mail?
 
Code:
$sender = you@yourisp.co.za

(ref ($sender->MailFile(
     {to =>'johnny@somewhere.com', 
      subject => 'this is a test',
      msg => &quot;Hi Johnie.\nI'm sending you the pictures.&quot;,
      file => 'image1.jpg,image2.jpg'
     }))
     and print &quot;Mail sent OK.&quot;
)
or die &quot;$Mail::Sender::Error\n&quot;;
I think that would work.

Sean.
psa.gif
 
Woops, ignore that last reply. Here is the proper version. It makes use of your smtp server and the perl module Mail::Sender.
Code:
use Mail::Sender;

$sender = new Mail::Sender 
{smtp => 'mail.yourdomain.com', from => 'your@address.com'};
$sender->MailFile({to => 'some@address.com',
                   subject => 'Here is the file',
                   msg => &quot;I'm sending you the list you wanted.&quot;,
                   file => 'filename.txt'});
[/color]
If you dont want to use that module, and want to stick to the Mail::Sendmail module, here is an example of its use:
Code:
use MIME::QuotedPrint;
use MIME::Base64;
use Mail::Sendmail 0.75; # doesn't work with v. 0.74!

%mail = (
         from => 'you@domain.tld',
         to => 'whoever@someplace.tld',
         subject => 'Test attachment',
        );


$boundary = &quot;====&quot; . time() . &quot;====&quot;;
$mail{'content-type'} = &quot;multipart/mixed; boundary=\&quot;$boundary\&quot;&quot;;

$message = encode_qp( &quot;Voilà le fichier demandé&quot; );

$file = $^X; # This is the perl executable

open (F, $file) or die &quot;Cannot read $file: $!&quot;;
binmode F; undef $/;
$mail{body} = encode_base64(<F>);
close F;

$boundary = '--'.$boundary;
$mail{body} = <<END_OF_BODY;
$boundary
Content-Type: text/plain; charset=&quot;iso-8859-1&quot;
Content-Transfer-Encoding: quoted-printable

$message
$boundary
Content-Type: application/octet-stream; name=&quot;$^X&quot;
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=&quot;$^X&quot;

$mail{body}
$boundary--
END_OF_BODY

sendmail(%mail) || print &quot;Error: $Mail::Sendmail::error\n&quot;;

I hope this helps.

Sean.
psa.gif
 
I tested the Mail::Sender version and was unsuccessful. Which was dissappointing, since it looked like the easiest of the two, but the Mail::Sendmail version works great.

I decided to send a picture (/root/temp/lars/lars0035.gif).

Here is the exact code I used:
Code:
#!/usr/bin/perl
                                                                                                                                                             
use MIME::QuotedPrint;
use MIME::Base64;
use Mail::Sendmail;
                                                                                                                                                             
%mail = (
         from => 'dm@test.co.za',
         to => 'sm@testrec.co.za',
         subject => 'Test attachment',
        );
                                                                                                                                                             
                                                                                                                                                             
$boundary = &quot;====&quot; . time() . &quot;====&quot;;
$mail{'content-type'} = &quot;multipart/mixed; boundary=\&quot;$boundary\&quot;&quot;;
                                                                                                                                                             
$message = encode_qp( &quot;Voila finished demands.&quot; );
                                                                                                                                                             
$file = &quot;/root/temp/lars/lars0035.gif&quot;; ##Your file here
                                                                                                                                                             
open (F, $file) or die &quot;Cannot read $file: $!&quot;;
binmode F; undef $/;
$mail{body} = encode_base64(<F>);
close F;
                                                                                                                                                             
$boundary = '--'.$boundary;
$mail{body} = <<END_OF_BODY;
$boundary
Content-Type: text/plain; charset=&quot;iso-8859-1&quot;
Content-Transfer-Encoding: quoted-printable
                                                                                                                                                             
$message
$boundary
Content-Type: application/octet-stream; name=&quot;lars0035.gif&quot;
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=&quot;lars0035.gif&quot;
                                                                                                                                                             
$mail{body}
$boundary--
END_OF_BODY
                                                                                                                                                             
sendmail(%mail) || print &quot;Error: $Mail::Sendmail::error\n&quot;;
[/color]

Sean.
psa.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top