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
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.
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 commandswhoami
anddomainname -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?
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.
精彩评论