Cron mail sending using outside SMTP mail server
I have a very simple (for someone who understands mail) mailing problem:
I want to write a cron job to periodically automatically send a short email to one address.
For everyday email, I'm using Evolution on my Fedora Core 3 box and I have it set up to use my ISP's SMTP mail server for outgoing mail. It works fine.
How can I do the same thing from a script or cron job?
Do I need a mail server running on my machine? It appears that sendmail is running but I think it's there just so root and other local users can get mail from the system. I've tried sending mail using "mail", but it never gets anywhere and mailq reports "No route to host".
If Evolution can send mail, it seems like a command line app like mail should be able to do it too if I can just get it configured correctly including telling it which smtp mail server to use.
Thanks for any help.
Perpetual Newbie, 8^)
I'm pretty new myself, so you may want to double check this, but try putting this in your /home/<user>/.mailrc or /root/.mailrc file.
set smtp=<smtp server, eg: mail.adelphia.net>
set from=<your email addy>
I use slackware, but this should let "mail" from the command line send through your ISP's mail server.
Thanks for the reply zzyzx. I tried that but it didn't do the trick.
Here's my .mailrc file (modified names for security):
When I look at the output of mailq (sendmail -bp), I see this:
-----Q-ID----- -- Size-- -----Q-Time----- ------------ Sender/Recipient-----------
j7MIg3T3028243 7 Mon Aug 22 11:42 <firstname.lastname@example.org>
(Deferred: mail.somewhere.com.: No route to host)
Note, sendmail thinks the "Sender" is "email@example.com", NOT what I set in the .mailrc file.
So, is mail even reading that file?
When Evolution sends mail, does it use sendmail at all or does it use some other method? It seems to me (in my relative network ignorance) that to simply send a text email, one would not even need a mail server running. Wouldn't you just send the packets out on the internet using SMTP to your SMTP server and that would be it. (Any and all education is welcome) 8^)
Are you typing "sendmail... " from the commandline?
When I use the commandline I type: mail -s "subject" <recipient email>. The mail command is actually the nail program which uses the mailrc config file.
You are correct in that you do not need sendmail running in order to send out mail.
No, I'm not typing the sendmail command. I only refered to it since it seems to be running and trying to do something with the mails I send using "mail".
I'm simply typing:
$ mail firstname.lastname@example.org (I use an actual address.)
"mail" then prompts for subject, then lets me enter lines of text ending in a dot by itself on a line. Then a prompt for Cc: and it finished.
I'm trying to simplify it all down to basics to try to get a handle on what's going on:
I stop sendmail.
I send mail to myself using:
$ mail mylogname
I type: mail
No mail for mylogname
I restart sendmail
I type: mail
Now I see the mail I just sent to myself.
It appears that "mail" needs "sendmail" to work. But where did the mail I sent get stored while sendmail was stopped? It was not in /var/spool/mail/mylogname. But it was obviously put somewhere since sendmail found it as soon as I restarted it.
So, can "mail" really be used to send mail (even to myself) without a mail server running?
"Somewhat confused by mail",
I think you've reached the end of my "expertise". A few other places to look
- when you type "man mail" is the man page for nail? mail can be an alias for several different mail programs. I tried commenting out those same lines from my .mailrc file and my mail command stopped working, so that's obviously a piece of the puzzle for nail. If you aren't using nail maybe your mail program uses a different user config file. Setting those smtp and from variables in the proper file may do the job.
- does your /var/log/maillog list any errors? You are using FC3 so that might be way off since I'm running Slackware.
Hopefully, another config file is the easy answer. Let me know how it goes.
I double checked and no, I'm definately not using nail. (I might look into it though) And the man pages for mail clearly show that ~/.mailrc is the user's config file for the app. Everything I read makes me think that the mail program, by itself, is not capable of sending mail over the internet using SMTP. (I'm probably wrong.) It seems to need a mail server running like sendmail so I'm guessing I just don't have sendmail setup correctly. But I don't even see anything in the sendmail man pages about defining an external SMTP server.
Anyway, I know that in *IX, there are almost always many ways to skin any particular cat. So I did some googling and found:
1) SMTP is REALLY SIMPLE! You can send email from a terminal with just a few simple commands. That's why it's called "SIMPLE Mail Transfer Protocol" 8^)
2) To do it from a program is harder since the protocol requires waiting for the replies from the SMTP server for sending the next command, etc. A "here" document in a shell script doesn't work because it doesn't wait for replies.
3) There is a really slick little Perl script that simply does just what I want. It's called "sendEmail" (http://caspian.dotconf.net/menu/Software/SendEmail/) and it works great. Don't even need to compile, etc since it's just a stand-alone script (as long as you have the Perl interpreter on your machine). It just does the proper connecting, talking, waiting, talking, etc in the SMPT language to fire off your mail without the overhead of a mail server, etc, etc. Cool!
I'd still love to know how to do this with "mail", "sendmail", etc. but for now, I'm back in business.
Thanks for your pointers, etc.
|All times are GMT -5. The time now is 09:36 AM.|