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

Oh most holy Perl monks,

Is there an object method in Net::SMTP that I can use to specify an email title/subject ?

Thank you,
Travis

janitored by ybiC: Retitle from one-word "Net::SMTP" to avoid hindering site search, also minor format tweaks for legibility

Replies are listed 'Best First'.
Re: Setting subject with Net::SMTP
by Aristotle (Chancellor) on Jul 26, 2004 at 18:34 UTC
    Use MIME::Lite. See "NOTES" in the documentation about using it without a sendmail binary.

    Makeshifts last the longest.

Re: Setting subject with Net::SMTP
by wufnik (Friar) on Jul 26, 2004 at 19:50 UTC
    hola. there sure is a function, and it's in Net::SMTP.

    the OP's question can be answered without a goto MIME::Lite - unless you need to attach stuff. not that MIME::Lite is not a fine module. however, IMHO Net::SMTP is absolutely fine & dandy for subject setting.

    the following very small module wraps it and should allow you to use it relatively painlessly. of course there should be error handling etc etc. left as an exercise for the reader...

    # a simple module which uses Net::SMTP to mail the contents # of a text file to a list of users. and with a # subject too! note minor spoofing. adjust to taste. package wmail; use Net::SMTP; sub mailfile{ my $smtpserver = 'server here plz'; my $file = shift; die "no file" unless $file; my $rcptsref = shift; die "no recipients" unless ref $rcptsref; my $subj = shift or "oops"; my @recipients = @{$rcptsref}; my $oldRs = $/; undef $/; open SOURCE, "$file" or die "can't open file $!"; my $totalcontents = <SOURCE>; close SOURCE; my $smtp = Net::SMTP; $smtp = Net::SMTP->new($smtpserver); $smtp->mail("informant"); $smtp->to(@recipients); $smtp->data(); my $subjectline = "Subject: " . $subj . "\n"; $smtp->datasend($subjectline); $smtp->datasend("\n"); $smtp->datasend($totalcontents); $smtp->dataend(); # print $smtp->domain, "\n"; $smtp->quit; } 1;
    save that to say, wmail.pm, use in the following manner

    use wmail; @recipients = ('your rcpts here'); wmail::mailfile("myfile", \@recipients, "mysubject");
    ...wufnik

    -- in the world of the mules there are no rules --
Re: Setting subject with Net::SMTP
by Fletch (Bishop) on Jul 26, 2004 at 18:12 UTC

    No, because that's part of the message body you pass whole to it. Perhaps you might want something a bit higher level, such as Mail::Send or Mail::Mailer?

    Update: Aristotle is correct, the subject is one of the message headers. More correctly I should have said "part of the message contents you pass whole to it", since Net::SMTP doesn't have separate methods for sending headers and sending the body.

      Actually, it's part of the headers.

      Makeshifts last the longest.

      I forgot to mention that I am coding this in ActiveState's Win32 implementation ... (i.e. no Mail::Send and no Mail::Mailer) What do you mean by "that's part of the message body you pass whole to it" ? This is what I have so far :
      use strict; use Net::SMTP; use FileHandle; my $smtp = Net::SMTP->new('mailhost', Hello => 'exchange.cognos.com', Timeout => 30, Debug => 1, ); my $fh_email_title= new FileHandle; $fh_email_title->open("< email_title.txt") or die "Cannot open email title file: $!"; my $fh_email_content= new FileHandle; $fh_email_content->open("< email_content.txt") or die "Cannot open email content file: $!"; $smtp->mail('travis.weir@cognos.com'); $smtp->to('travis.weir@cognos.com'); $smtp->???($fh_email_title->getlines()); $smtp->data(); $smtp->datasend($fh_email_content->getlines()); $smtp->dataend(); $fh_email_title->close; $fh_email_content->close; $smtp->quit; autoflush STDOUT 1;
      Again, I'm not sure what you meant by your earlier statement .... Thanks again, Travis

        When you call $smtp->datasend, you're passing the entire message body headers and all. SMTP doesn't have any notion of a messaage subject; the mail and to methods are setting the envelope sender and receiver, but those aren't duplicated inside the body of the message unless you explicitly do so yourself (by calling datasend with "From: ...\n" and the like). If you want to set a subject, just pass datasend a line "Subject: blah\n" somewhere before the body of the message.