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

I'm trying to create a cgi script that will send mail to a certain address, appearing to be from another address (one a user types into a form).
I can do this using sendmail thusly:
sendmail ('god@heaven.com', 'die.puny@earthling.net', 'Test', 'This is + a test'); sub sendmail { my ($from, $to, $subject, $body) = @_; open(MAIL, "|/usr/lib/sendmail -oem -t -oi -f $from"); print MAIL "To: $to\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$body\n"; close MAIL; }
Which works fine. However, attempting to do this with Mail::Mailer, results in the mail appearing to come from www-data.
sendmail ('god@heaven.com', 'die.puny@earthling.net', 'Test', 'This is + a test'); sub sendmail { my ($from, $to, $subject, $body) = @_; my $mailer; eval { $mailer = Mail::Mailer->new(); }; if ($@) { warn "Couldn't send mail: $@"; } else { $mailer->open({ From => $from, To => $to, Subject => $subject }); print $mailer $body; unless (close ($mailer)) { warn "Couldn't close mailer: $!"; } } }
I've read all the documentation I've been able to lay my hands on (Mail::Mailer's is rather minimal), and done a Super Search, but all to no avail.
So, fellow monks, is there any way of forcing the 'From:' field using Mail::Mailer?

[Btw, I'm using exim as my MTA, and www-data is listed in trusted_users]

Replies are listed 'Best First'.
(crazyinsomniac) Re: Changing the 'From:' header with Mail::Mailer
by crazyinsomniac (Prior) on Sep 14, 2000 at 22:15 UTC
    UPDATED:

    Hi,

    As kilinrax already knows from our chat, the following will work (he confirmed it)

    Upon another suggestion of mine kilinrax decided to use Mail::Sendmail  - v. 0.77 - Simple platform independent mailer. Hey it works.

    I haven't tested this but it should work:

    Mail::Send - Simple electronic mail interface --------------------------------------------------------- SYNOPSIS require Mail::Send; $msg = new Mail::Send; $msg = new Mail::Send Subject=>'example subject', To=>'timbo'; $msg->to('user@host'); $msg->subject('example subject'); $msg->cc('user@host'); $msg->bcc('someone@else'); $msg->set($header, @values); $msg->add($header, @values); $msg->delete($header); # Launch mailer and set headers. The filehandle returned # by open() is an instance of the Mail::Mailer class. $fh = $msg->open; print $fh "Body of message"; $fh->close; # complete the message and send it $fh->cancel; # not yet implemented

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac

      Yeah, using Mail::Sendmail works fine, thanks crazyinsomniac ;-)
      my %mail = ( To => 'abowley@lave.london.excite.com', From => 'god@heaven.com', Message => "Test" ); sendmail(%mail) or die $Mail::Sendmail::error;
      Still, if anyone can tell me a solution using Mail::Mailer, i'd really appreciate it, as using Mail::Sendmail will mean having to install it on N servers....
Re: Changing the 'From:' header with Mail::Mailer
by merlyn (Sage) on Sep 14, 2000 at 23:13 UTC
    Mail::Mailer defaults to mail, which cannot specify the From header at all (and is also insecure!).

    So, as others have suggested, use a different module (there are many {grin}) or be sure to specify an underlying mailer that can control the From, such as with:

    my $mailer = Mail::Mailer->new("sendmail");
    or
    my $mailer = Mail::Mailer->new("smtp");

    -- Randal L. Schwartz, Perl hacker