Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Log4Perl to send mail via SMTP

by weismat (Friar)
on Nov 20, 2008 at 13:08 UTC ( [id://724864]=perlquestion: print w/replies, xml ) Need Help??

weismat has asked for the wisdom of the Perl Monks concerning the following question:

Maybe I am missing something obvious, but how do I send SMTP mail via Log4Perl?
I use this configuration

log4perl.category = FATAL, Mailer
log4perl.appender.Mailer = Log::Dispatch::Email::MailSend
log4perl.appender.Mailer.to = x@z.com
log4perl.appender.Mailer.from = y@z.com
log4perl.appender.Mailer.subject = Something's broken!
log4perl.appender.Mailer.layout = SimpleLayout
log4perl.appender.Mailer.Server = x.x.x.x
log4perl.appender.Mailer.smtp = 1

I have tested that Mail::Mailer works, but I do not get it to work with the standard appender. The constructor for Mailer needs to be in the following format: $mailer = Mail::Mailer->new('smtp', Server => $server);

Replies are listed 'Best First'.
Re: Log4Perl to send mail via SMTP
by mje (Curate) on Nov 20, 2008 at 14:36 UTC
    Am am successfully using Log4perl to send email using the following appender:
    log4perl.logger=ERROR, Mailer log4perl.appender.Mailer = Log::Dispatch::Email::MailSend # only mail ERRORs and above: log4perl.appender.Mailer.Threshold = ERROR log4perl.appender.Mailer.to = x@y.z log4perl.appender.Mailer.subject = db error log4perl.appender.Mailer.buffered = 0 log4perl.appender.Mailer.layout = PatternLayout log4perl.appender.Mailer.layout.ConversionPattern=%d %p> %F{1}:%L %M - + %m%n
      I think that the main problem is that Log4Perl can only call the constructor with key-value pairs, unfortunately Mail:Mailer uses smtp as a scalar parameter.
      The documentation mentions the following:
      Since Mail::Send is a subclass of Mail::Mailer, you can change how mail is sent from this module by simply useing Mail::Mailer in your code before mail is sent. For example, to send mail via smtp, you could do:
      use Mail::Mailer 'smtp', Server => 'foo.example.com';
      I do not understand this part of the docu. Could one explaint it to me?
        #!/usr/bin/perl ... this is your code, to it you add the following use Mail::Mailer 'smtp', Server => 'foo.example.com';
Re: Log4Perl to send mail via SMTP
by GeneralElektrix (Acolyte) on Nov 21, 2008 at 17:11 UTC

    Hi,

    Got it to work this way:

    log4perl.rootLogger = ERROR, Email log4perl.appender.Email = Log::Dispatch::Email::MailSender log4perl.appender.Email.name = email log4perl.appender.Email.min_level = error log4perl.appender.Email.to = x@z.com log4perl.appender.Email.from = y@z.com log4perl.appender.Email.subject = Something's broken! log4perl.appender.Email.smtp = smtp.z.com log4perl.appender.Email.layout = SimpleLayout
      This worked for me too after lots of hair pulling.

      Note the difference between:

      Log::Dispatch::Email::MailSender

      in the working example, and:

      Log::Dispatch::Email::MailSend

      in the original, which I also had from somewhere on the web, but was unable to get to work with SMTP, although it does not error, it just writes to mailer.testfile instead.

      Also that:

      log4perl.appender.Email.smtp = smtp.z.com

      is the correct way to specify the server, at least this works for me.

      I see - you are using the smtp server adress as a parameter - I will try this, but it did not work for me before when I added some prints into Mail::Mailer.
      I have written my own appender now - it is the easier solution for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://724864]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found