javanerd
javanerd

Reputation: 2852

mail command of unix

I am using the mail command of unix in a perl script. I specify the 'to', 'cc', 'subject' and 'body' of the mail. I do not specify the from address. Where is the from address picked from? Pls help

Upvotes: 0

Views: 3822

Answers (3)

Mike Pennington
Mike Pennington

Reputation: 43107

There are portable libraries for handling email as daxim and David W mention, but if you want a quick fix, this works under linux if your mail command uses bsd-mailx (as it does on my machine)...

#!/usr/bin/env perl

$BODY = "Hello self";
$RECIPIENT = "destination\@email.local";
$FROM = "mike\@localhost";
$SUBJECT = "some subject here";
$CMD = qq(echo "$BODY" | mail -a "From: $FROM" -s $SUBJECT $RECIPIENT);
exec($CMD);

If you have more questions about the unix mail command, try man mail from your shell prompt.

Upvotes: 3

David W.
David W.

Reputation: 107060

Don't use the mail command linecommand! Use Net::SMTP.

The mail command may not even be configured on a particular system, and it won't work on Windows. Meanwhile, Net::SMTP is a standard Perl module that should be available on all systems.

Never used it before? Read the documentation and try it out. That's how you learn.

Upvotes: 1

daxim
daxim

Reputation: 39158

The mail command on most system nowadays is Heirloom mailx. It claims compatibility with POSIX, so the information I give here should be good for any well-behaving mail command.

The From address is set by:

  • either the user@domain as returned by the appropriate POSIX system calls (see shell commands whoami and domainname -f for a different way to access them)
  • or set by the from environment variable
  • or set by the -r command line option (going to be deprecated?)

Obligatory Clippy: Hi! I see you are trying to send mail from Perl. Did you mean to use Email::Sender/Email::Simple instead?

Upvotes: 1

Related Questions