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

How do I rewrite mail headers using Mail::Send?

by c (Hermit)
on Jun 25, 2002 at 03:13 UTC ( #177012=perlquestion: print w/replies, xml ) Need Help??

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

The perldoc for mail::send shows that you can set headers for email being sent using the module. Although their are methods to set the To: Subject: and Cc: fields...

$msg->to('user@host'); $msg->subject('example subject'); $msg->cc('user@host');

There doesnt seem to be any a method for setting the From: field save using the $msg->set(); method, which has a description as:

$msg->set($header, @values); $msg->add($header, @values); $msg->delete($header);

I tried the following:

$msg->set("From","helpful\@mydomain.com");

I also tried "From: ", "From:" and "Its Me <helpful\@mydomaind.com>" but none gave any different results. I'm thinking that my syntax is wonky somewhere along the way. Can someone point out the correct method?

thanks -c

Replies are listed 'Best First'.
Re: How do I rewrite mail headers using Mail::Send?
by djantzen (Priest) on Jun 25, 2002 at 04:13 UTC

    I ran into this problem just the other day. Under the covers all Mail::Send does is pass the header's hash to a Mail::Mailer::* subclass and then to a platform specific program like sendmail.

    Check to see if the exact subclass of Mail::Mailer you're using is smtp. If so, it's using Mail::Util::mailaddress to set the 'From' value in the method set_headers.

    Mail::Util docs:

    Return a guess at the current users mail address. The user can force the return value by setting $ENV{MAILADDRESS}

    Update: This is the code for set_headers, which I just noticed overwrites the value returned by mailaddress with the value of 'From' (if specified) in the headers:

    $self->SUPER::set_headers({ From => "<" . mailaddress() . ">", %$hdrs, 'X-Mailer' => "Mail::Mailer[v$Mail::Mailer::VERSION] Net::SMTP +[v$Net::S\ MTP::VERSION]"

    I should note that although I encountered this problem a couple days ago, I haven't yet solved it! ;)

    UPDATE: Aha! I think I've got it. I was mistaken in thinking that sendmail was the mail program it was defaulting to. In fact, it was running mailx. Now, you can force it to use sendmail by passing the string 'sendmail' when you call Mail::Send::open. Doing so will (probably) enable you to set the 'From' value successfully.

    I believe that the reason the 'From' header gets changed has to do with /etc/mail/mailx.rc which (on my system (Solaris 8)) lists quite a number of allegedly "uninteresting" headers to ignore. I don't see 'From' specifically, but there are a couple of other likely candidates there. So, I think it wipes out the value submitted via Mail::Send/Mailer and resets it with the 'username@hostname' combination.

    Whew! Sorry for the stream of consciousness answer -- I just kept thinking I'd figured out the problem!

      Mail::Mailer allows you to set the type of mail server you are using. It checks mailx, Mail and mail in that order if you do not specify the $type. So, in other words to change this default behavior from mailx Mail and mail to smtp a snippet might look like:
      use Mail::Mailer; use Mail::Mailer qw(mail); $mailer = new Mail::Mailer; $mailer = new Mail::Mailer 'smtp', Server => $server; $mailer->open(\%headers); print $mailer $body; $mailer->close;
Re: How do I rewrite mail headers using Mail::Send?
by caedes (Pilgrim) on Jun 25, 2002 at 03:49 UTC
    That module has both "set" and "add" methods with the same parameters and very little documentation. Perhapse you have to "add" the field and then can later "set" it?

    -caedes

Re: How do I rewrite mail headers using Mail::Send?
by linebacker (Scribe) on Jun 25, 2002 at 17:28 UTC
    I don't know if I provided enough detail to get you where you are going, so here is one way to do it, from start to finish.
    #!/usr/local/bin/perl -w $body = "Some stuff here..."; use Mail::Mailer; $mailer = Mail::Mailer->new("sendmail"); $mailer->open({ From => "BurningBritches\@hotpants.com", To => "someone\@somewhere.com", Subject => "Masked Mail Headers", }) or die "Can't open: $!\n"; print $mailer $body; $mailer->close();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://177012]
Approved by samtregar
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-06-14 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (66 votes). Check out past polls.

    Notices?