[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] SMTP and setting mail subject



Hi to all,

I attach a script below for some comments from the more experienced. I
intend it to take a hash %mail and use it to send an email via SMTP.pm, the
connection being established within an eval statement to capture any
failure in the connection -- passing control to a general error-reporting
routine if anything goes wrong. It seems to work but I have two
difficulties:

(a) how can I use SMTP.pm to set the email header? All the text I pass
    to SMTP.pm seems to be going into the body of the email;
(b) do more experienced eyes  detect any major bug in the way I use SMTP?
    I have to admit that the script is blindly cribbed and put together
    from fragments I have seen on this list and the archive (mostly Paul
    Schinder's contributions). It works, but I don't know why :-)

Thanks in advance,

Gauden Galea
http://www.synapse.net.mt


##  ###################################################  #
##  Connect to SMTP server and send mail                 #
##  ###################################################  #

sub sendMail {
        use SMTP;

        my ($err) = "Something went wrong with the mail server;
                        it could be busy and not responding.
                        Please try again at some other time.";

        eval <<'TEST';
        $smtp = Net::SMTP->new($mail{'mailhost'});
        $smtp->mail($mail{'from'})                   ;
        $smtp->to($mail{'to'})                       ;
        $smtp->data()                                ;
        $smtp->datasend("$mail{'subject'}\n")        ;
        $smtp->datasend("\n")                        ;
        $smtp->datasend("$mail{'text'}\n")           ;
        $smtp->dataend()                             ;
        $smtp->quit                                  ;
TEST
        if ($@) { &doError ($err) }

}