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 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?
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.
Related
I am vikas. i am looking a command that can send mail from zimbra mail-store server to external world. but as i checked in zimbra not getting Any program that have role of sending mails. that is why i am relay email through Zimbra MTA server. now i am going to use below command but still getting variable error.
awk 'BEGIN{print "Subject:test mail!\nFrom:Mailadmin <admin#knowledgelinux.com>"}{printf("%s\015\n", $0)}' $message file-name=/test.txt | sendmail -t "mail#knowledgelinux.com"
awk: command not found
The instructions here worked for me:
vim /etc/logwatch/conf/logwatch.conf
Relevant lines to be replaced:
Output = mail
MailTo = root_all#your.domain
MailFrom = Logwatch#your.domain
mailer = "/opt/zimbra/common/sbin/sendmail -t"
Zimbra uses a modified version of sendmail.
The Zimbra MTA uses a modified postfix daemon. You should be able to just use the command mail:
cat *messagefilename* |mail -r *fromaddress* -s *Subject* *recipient-list*
As long as the Zimbra MTA is running on the machine in question this should work.
I've been looking everywhere for this answer and have had no luck! I went to http://www.jeffkastner.com/2010/01/blat-stunnel-and-gmail/ and followed it step by step. Stunnel installed, installed service and started service successfully but blat still does not work after setting it up.
I have a folder on my desktop C:\Users\Nicholas\Desktop\Games which is where the contents of blat is placed, and C:\Users\Nicholas\Desktop\Games\stunnel is where stunnel files are installed. And I also tried this when stunnel was installed to program files and it still did not work. I don't want blat installed to system32 because I have a batch file I'm using it with in C:\Users\Nicholas\Desktop\Games.
Online I try to find syntax for the blat -install but none of them work or at least I dont understand how it works. This is what I believe:
You need to install your profile
You send an email using the profile (ex: blat -p gmailsmtp ...)
And thats it? It doesn't work for me.
I am willing to find an alternative to blat, but if you give an alternative please give a tutorial, not a suggestion.
Here are the install commands for blat I tried: (Which one should be working?)
blat -install smtp.gmail.com
blat -install smtp.gmail.com myemail#gmail.com -u username(does this include #gmail.com?) -pw password – - gmailsmtp
Here are email commands I tried:
blat -p gmailsmtp -to myemail#gmail.com -subject "subject text" -body "Body text" -server 127.0.0.1:1099
and a lot more commands that I can't remember.
Anyway, anyone find where I tripped here?
Yes! I found a vbscript script that finally worked! On this thread, "Email sending not working", Miguel posted the following script
Sub SendGMail()
' Object creation
Set objMsg = CreateObject("CDO.Message")
Set msgConf = CreateObject("CDO.Configuration")
' Server Configuration
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user#gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
msgConf.Fields.Update
' Email
objMsg.To = "user#gmail.com"
objMsg.From = "fromuser#domain.com"
objMsg.Subject = "Test send with Gmail account"
objMsg.HTMLBody = "HTML/Plain text message."
objMsg.Sender = "Mr. Name"
Set objMsg.Configuration = msgConf
' Send
objMsg.Send
' Clear
Set objMsg = nothing
Set msgConf = nothing
End Sub
This script is working, but a lot of people still had problems, looking closer into the script I realized I didn't even recall the SendGMail()! To make this work I simply typed SendGMail() on the next line and BAM! I got an email! Thank you #user4317867 for helping me find my answer.
I want to know if we can trigger a perl script in unix through a sending a mail.
basically the script should check the incoming mail then trigger the perl script.
Also can some1 out on setting the mail access like reading and saving mail on my unix home.
-Thanks
There are two basic approaches you can take.
Configure your SMTP server to run incoming email through your script. Procmail is the usual tool for choice for this.
Poll (by using cron, or writing your script as a daemon) your IMAP/POP server/Maildir/Mbox/etc.
The former is usually the better option.
You can hack it like this:
inotifywait -m /var/mail/$USER | grep --line-buffered MODIFY | while read _unused_; do
#your perl script here
done
Explanation: It monitors /var/mail/$USER for changes & prints events on stdout. On every MODIFY event, it will trigger the script.
Note: This will work only for unix mails on localhost. Not on external server.
Known bugs:
A mail read activity will also trigger the script.
You can keep polling (unix) mail for any new mails.
while true; do
echo "\nq" | mail >/tmp/new_mail 2>&1 && /path/to/your/perl_script.pl arg1 arg2 ...
sleep 60
done
If you want, you can use contents of /tmp/new_mail in your perl program. If you don't need it, you can redirect mail output to /dev/null instead.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to use Cygwin to send an e-mail from the command line. This is what I am putting in:
email -f myaddress#blah.com -s "This is a test" -b toaddress#blah.com
I get this error message:
/bin/sh: /usr/lib/sendmail: No such file or directory
I created a folder in /usr/lib called "sendmail", and now I get this:
/bin/sh: /usr/lib/sendmail: is a directory
Can someone please give me a step-by-step on how to send an email from Cygwin? Like how to set it up and everything? I have looked everywhere and I am about ready to tear my hair out.
EDIT: Thanks for your responses guys. This is how I finally got it to work.
bash.exe -c "echo -e 'To: thepeopleimsendingitto#blah.com\nSubject: mySQL Upload\nSQL files from machines uploaded to log table successfully.' | sendmail -f me#blah.com otherpeople#blah.com"
Even though Cygwin was in my Windows path, it couldn't recognize it, so I had to run bash.exe directly and say "do this command as a Linux command". The echo is what is constructing the email itself. The \n characters separate it into "To", "Subject" and Body.
Apparently, email is a program that lets you submit an email message (a Mail Submission Agent) that relies on another program to actually send the message (a Mail Transfer Agent).
I enabled this on my Cygwin installation last week.
I am not using email but mutt, one of the alternatives Cygwin offers (see its package list).
I use mutt not only to submit the mail to be sent, but also to read it; it's a Mail User Agent (see some screenshots).
Like email, mutt relies on a Mail Transfer Agent to send mail, so I had to install one.
On Linux, popular choices are sendmail and postfix; but they do far more than you need and Cygwin doesn't offer them as packages. It does offer exim and ssmtp.
I installed the ssmtp package and ran the ssmtp-config utility. You have to make some decisions here. You must know which SMTP server you can use and whether you need any special configuration to communicate with it.
By the way, ssmtp does install an executable called sendmail, which is not the original sendmail, but behaves like it for the purposes you need it for.
Okay So i wasn't clear enough on my first attempt... I guess that is my fault, I will try to make this more clear.
In the mail-config it will ask for the sendmail binary. It should sound something like this :
Please enter the sendmail command line [/usr/sbin/sendmail -t -i]:
This is basically asking where is the sendmail binary installed and what parameters should i pass it to send a mail. Your sendmail binary is either not installed, or not installed in the location you are specifying.
To see if you have sendmail installed try
%sendmail;
If that works, to find the path of that binary try
%which sendmail;
If that does not work, either sendmail is not installed, or the location of sendmail needs to be appended to the env path variable. There no correct answer on how you want to set things up, but the minimum requirments to make this work is to have sendmail on the current machine, have it configured and pass the correct absolute path to the mail program in the mail-config.
I am using sendmail in perl and noticed (after much banging of head against wall) that when the script is run at the command line it needs you to leave out the \n(s) after your e-mail and the recipient's email address in order to format the mail correctly, but when running via CGI if those \n(s) aren't there it returns an error stating that the recipient's e-mail is malformed.
Has anyone else encountered this? What are the two doing differently?
I am betting that you are getting data from prompts in on the commandline and not chomping them like this:
my $send_to = <>;
This means $send_to will already have a "\n". To make them both work the same way chomp the variables:
my $send_to = <>;
chomp($send_to);
or just
chomp(my $send_to = <>);
In a couple of your comments you mention that you're running the script from the command line with the -l option (perl -l foo.cgi).
The -l option enables automatic line-ending processing, and as your problem is with line endings, I suggest you try it without the -l.
Where is the data coming from? Hard coded in the script, or from a web form?
Just as an aside, if you get the recipient's email address from a web form, your form will be used by spammers. It's a 100% guarantee.
The term "CGI" is broad, if you mean your perl script run as a CGI versus yur perlscript run at the command line, I would look toward the pathing that the script has and its general inherited environment. Especially if your running it as different userids. If the webserver is in a chroot, etc.
use Data::Dumper;
warn(Dumper(\%ENV));
So I'm guessing that you have something like this for running it via the command line:
my $your_email = "you#foo.bar";
my $recipient_email = "them#foo.bar";
and this when "running via CGI":
my $your_email = "you#foo.bar\n";
my $recipient_email = "them#foo.bar\n";
So the question I would ask you then is how you're calling sendmail with the above variables, and also what you mean when you say "running via CGI" versus running via the command line? Are you just adding CGI code and still running via the command line or by visiting its URL in a web browser?