LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2006, 11:24 PM   #1
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Rep: Reputation: 15
How to send an email within a script???


Hi All,

Basically, I want to create a script which send an email if any of the condition is not met. So, it's like an alarm. So now, my problem is how to open an email to send a message from a script. Could anybody give me an idea...

I have tried the following commands but to no avail. There was no error when I run the script, but I didn't receive any email. Is there anything I need to setup beforehand? Thanks for your prompt reply...

mail -s "john" johnsanty@yahoo.com < hello.txt

or

cat hello.txt | mailx -s Subject johnsanty@yahoo.com

My Distribution is Fedora C5.



John

Last edited by johnsanty; 08-27-2006 at 11:41 PM.
 
Old 08-28-2006, 12:47 AM   #2
drj000
Member
 
Registered: Sep 2004
Location: Houston, TX
Distribution: Fedora
Posts: 261

Rep: Reputation: 33
You need to give more information. What error messages are you getting (if at all), when you try that? Both of those commands should work, if everything is set up properly.
You need sendmail working to use those. Are you sure you have sendmail installed and working? rpm -q sendmail will tell you if it's installed. It will give you the name and version number, or if it's not installed, it'll say package sendmail is not installed.
If it's not installed, install it by doing (as root), yum install sendmail, then follow the following steps once it's installed.
If it's installed, you have to check that it's actually started up. As root, type service sendmail status.
I think it should say Checking for service sendmail: [OK], if it's working properly. If it's not running, you can start it with service sendmail start, and to ensure it starts when you boot up next time, then type chkconfig sendmail on.
If it's installed, and running properly, then the problem could very well be your ISP. Some ISP's block outgoing mail if it's not send through their SMTP server (SBC, for example, does that).
 
Old 08-28-2006, 12:52 AM   #3
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
Probably the mail server consider the sender as spam, have you check the junk mail folder?
 
Old 08-28-2006, 10:34 AM   #4
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
You'd use "mailx" under Solaris, "mail" under Linux. The redirections ("cat hello.txt | command" and "command < hello.txt") are in their effect equivalent. You don't absolutely need the sendmail *package*; e.g., qmail provides a perfect replacement (and claims to be secure).
 
Old 08-28-2006, 10:48 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
It's quite possible that your MTA isn't configured correctly. If so, the 'mail' program could run without errors, but the message could still fail to send and you'd see errors in a logfile somewhere in /var/log/*

Unfortunately I don't even know what mta fedora uses by default, let alone how to tweak it.
 
Old 08-28-2006, 07:56 PM   #6
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by drj000
You need to give more information. What error messages are you getting (if at all), when you try that? Both of those commands should work, if everything is set up properly.
You need sendmail working to use those. Are you sure you have sendmail installed and working? rpm -q sendmail will tell you if it's installed. It will give you the name and version number, or if it's not installed, it'll say package sendmail is not installed.
If it's not installed, install it by doing (as root), yum install sendmail, then follow the following steps once it's installed.
If it's installed, you have to check that it's actually started up. As root, type service sendmail status.
I think it should say Checking for service sendmail: [OK], if it's working properly. If it's not running, you can start it with service sendmail start, and to ensure it starts when you boot up next time, then type chkconfig sendmail on.
If it's installed, and running properly, then the problem could very well be your ISP. Some ISP's block outgoing mail if it's not send through their SMTP server (SBC, for example, does that).
Hi drj,

This is what I got I executed the commands checking for the status of the sendmail. It seems it is installed and running. Is there any work around on how to unblock outgoing mail if it's not send through the SMTP server? Do I need to set up my email using my ISP pop server...Thanks for your prompt reply...

[root@john ~]# rpm -q sendmail
sendmail-8.13.5-3
[root@john ~]# service sendmail status
sendmail (pid 2938 2305 2301 2296) is running...

Last edited by johnsanty; 08-28-2006 at 09:53 PM.
 
Old 08-28-2006, 08:11 PM   #7
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by aluser
It's quite possible that your MTA isn't configured correctly. If so, the 'mail' program could run without errors, but the message could still fail to send and you'd see errors in a logfile somewhere in /var/log/*

Unfortunately I don't even know what mta fedora uses by default, let alone how to tweak it.

Hi aluser,

The following is what I got ferom the maillog.Can you see anything wrong with the log? How could I configure my MTA. Could you give me an idea...Sori m pretty much novice with Linux system... Thanks for your prompt reply...

Aug 28 20:42:52 john sendmail[2935]: k7T0gqLA002935: to=johnsanty@yahoo.com, ctladdr=johnsanty (500/500), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30054, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (k7T0gq6H002936 Message accepted for delivery)

Last edited by johnsanty; 08-28-2006 at 08:39 PM.
 
Old 08-28-2006, 09:04 PM   #8
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by drj000
You need to give more information. What error messages are you getting (if at all), when you try that? Both of those commands should work, if everything is set up properly.
You need sendmail working to use those. Are you sure you have sendmail installed and working? rpm -q sendmail will tell you if it's installed. It will give you the name and version number, or if it's not installed, it'll say package sendmail is not installed.
If it's not installed, install it by doing (as root), yum install sendmail, then follow the following steps once it's installed.
If it's installed, you have to check that it's actually started up. As root, type service sendmail status.
I think it should say Checking for service sendmail: [OK], if it's working properly. If it's not running, you can start it with service sendmail start, and to ensure it starts when you boot up next time, then type chkconfig sendmail on.
If it's installed, and running properly, then the problem could very well be your ISP. Some ISP's block outgoing mail if it's not send through their SMTP server (SBC, for example, does that).
Hi All,

I'm not sure if this is related. I tried to set up my Evolution email using my ISP pop and smtp servers then i got an error message when I tried to send an email to my yahoo account. The error message is the following:

"Error while performing operation.
MAIL FROM command failed: Must issue a STARTTLS command first"

Now, i tried to search for this error and one person suggested that I need to enable the TLS..how could I enable my TLS...? Thanks for your help....
 
Old 08-29-2006, 05:36 AM   #9
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
how do you write your script in sending email? I write mine in this format

$to = "user@email.com";
$subject = "test mail";
$message = "test message";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

mail($to, $subject, $message, $headers);
 
Old 08-29-2006, 09:17 AM   #10
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
John,
It seems the question shifted to whether you can send e-mail at all, whether from a script or outside? You could try mailing directly from the shell, i.e.,
Code:
echo The message body | mail -s "A subject" johnsanty@yahoo.com
And probably you get the same as from the script. (And when you add the -v option to mail it will just tell you your computer (127.0.0.1) accepted it for delivery -- while we are interested in seeing the session going out to the net.)

Requiring STARTTLS probably means someone insist on encryption. Not sure who, maybe your ISP? It's not your e-mail provider yahoo, as the session below shows. You can manually run SMTP sessions, and it can help figuring out what the problem is. So starting with the e-mail address you provided above, I did the following. What I typed is bold.

Code:
% host -t mx yahoo.com
yahoo.com mail is handled by 1 mx1.mail.yahoo.com.
yahoo.com mail is handled by 1 mx2.mail.yahoo.com.
yahoo.com mail is handled by 1 mx3.mail.yahoo.com.
yahoo.com mail is handled by 5 mx4.mail.yahoo.com.
% telnet mx1.mail.yahoo.com 25
Trying 4.79.181.14...
Connected to mx1.mail.yahoo.com.
Escape character is '^]'.
220 mta183.mail.mud.yahoo.com ESMTP YSmtp service ready
HELO lithum.mydomain.com
250 mta183.mail.mud.yahoo.com
MAIL From: <brechbuehler@gmail.com>
250 sender <brechbuehler@gmail.com> ok
RCPT To: <johnsanty@yahoo.com>
250 recipient <johnsanty@yahoo.com> ok
RSET
250 reset ok
QUIT
221 mta183.mail.mud.yahoo.com
You see it got past MAIL From without problem. I issued RSET to avoid sending you unsolicited mail.

Now I don't know what your sendmail does -- maybe it relays all outgoing mail through your ISP who insists on encryption? Or it talks directly to the destination MTA (e.g., mx1.mail.yahoo.com)? Try to pinpoint the problem. If it's your sendmail configuration, there should be a number of people on this forum who can help you.
 
Old 08-29-2006, 11:06 AM   #11
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
From the log message the OP posted, it looks like sendmail may be trying to send directly to the destination mail server (I'm guessing this because it says it's using 127.0.0.1 as a relay). If that's the case (I'm not really sure that it is), the destination could be rejecting the mail as spam because it comes from a suspicious dynamic address, or your isp could be filtering outbound connections to port 25.

If you can figure out how to get sendmail to forward everything to your isp's smtp server that might help. in the debian configs that is called a "smarthost"
 
Old 08-29-2006, 12:45 PM   #12
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
John,

Can you try to telnet to mx1.mail.yahoo.com, port 25? If you get to the 220 message, at least you know your ISP isn't filtering those connections.

Also, you could look in the mail log (where you found "relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (k7T0gq6H002936 Message accepted for delivery)") and see if you can locate further down an entry when sendmail actually attempts that delivery? It might be informative.

/Quigi
 
Old 08-29-2006, 10:14 PM   #13
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Quigi
John,
It seems the question shifted to whether you can send e-mail at all, whether from a script or outside? You could try mailing directly from the shell, i.e.,
Code:
echo The message body | mail -s "A subject" johnsanty@yahoo.com
And probably you get the same as from the script. (And when you add the -v option to mail it will just tell you your computer (127.0.0.1) accepted it for delivery -- while we are interested in seeing the session going out to the net.)

Requiring STARTTLS probably means someone insist on encryption. Not sure who, maybe your ISP? It's not your e-mail provider yahoo, as the session below shows. You can manually run SMTP sessions, and it can help figuring out what the problem is. So starting with the e-mail address you provided above, I did the following. What I typed is bold.

Code:
% host -t mx yahoo.com
yahoo.com mail is handled by 1 mx1.mail.yahoo.com.
yahoo.com mail is handled by 1 mx2.mail.yahoo.com.
yahoo.com mail is handled by 1 mx3.mail.yahoo.com.
yahoo.com mail is handled by 5 mx4.mail.yahoo.com.
% telnet mx1.mail.yahoo.com 25
Trying 4.79.181.14...
Connected to mx1.mail.yahoo.com.
Escape character is '^]'.
220 mta183.mail.mud.yahoo.com ESMTP YSmtp service ready
HELO lithum.mydomain.com
250 mta183.mail.mud.yahoo.com
MAIL From: <brechbuehler@gmail.com>
250 sender <brechbuehler@gmail.com> ok
RCPT To: <johnsanty@yahoo.com>
250 recipient <johnsanty@yahoo.com> ok
RSET
250 reset ok
QUIT
221 mta183.mail.mud.yahoo.com
You see it got past MAIL From without problem. I issued RSET to avoid sending you unsolicited mail.

Now I don't know what your sendmail does -- maybe it relays all outgoing mail through your ISP who insists on encryption? Or it talks directly to the destination MTA (e.g., mx1.mail.yahoo.com)? Try to pinpoint the problem. If it's your sendmail configuration, there should be a number of people on this forum who can help you.

Hi Quigi,

Actually, I tried to send mail directly from command line but I got the same result. Now I tried the following:
telnet mx1.mail.yahoo.com 25

And I got the following, it seems that it is not connecting at all.
[johnsanty@babe1 ~]$ telnet mx1.mail.yahoo.com 25
Trying 4.79.181.14...
telnet: connect to address 4.79.181.14: Connection timed out
Trying 4.79.181.15...
telnet: connect to address 4.79.181.15: Connection timed out
Trying 67.28.113.71...
telnet: connect to address 67.28.113.71: Connection timed out
Trying 67.28.113.73...


In the maillog, I got the following. I'm not sure if this message conveys that the message has been sent. Can you confirm?:

Aug 29 22:58:23 babe1 sendmail[3236]: k7U2wNiM003236: from=johnsanty, size=60, class=0, nrcpts=1, msgid=<200608300258.k7U2wNiM003236@babe1>, relay=johnsanty@localhost
Aug 29 22:58:23 babe1 sendmail[3237]: k7U2wNtV003237: from=<johnsanty@babe1>, size=324, class=0, nrcpts=1, msgid=<200608300258.k7U2wNiM003236@babe1>, proto=ESMTP, daemon=MTA, relay=localhost.localdomain [127.0.0.1]
Aug 29 22:58:23 babe1 sendmail[3236]: k7U2wNiM003236: to=johnsanty@yahoo.com, ctladdr=johnsanty (500/500), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30060, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (k7U2wNtV003237 Message accepted for delivery)


However,in the /var/spool/mail, I got the following several of them since I have attempted many time to send an email. Does it look like a problem with my configuration or with yahoo? Also, I tried sending an email to my hotmail account and i got the same thing. Do you have any idea.....i dont know if it is my sendmail configuration or not. Do you know by any chance on how to configure the senmail? Thanks for your prompt reply...

**********************************************
** THIS IS A WARNING MESSAGE ONLY **
** YOU DO NOT NEED TO RESEND YOUR MESSAGE **
**********************************************

The original message was received at Mon, 28 Aug 2006 21:27:23 -0400
from localhost.localdomain [127.0.0.1]

----- Transcript of session follows -----
<johnsanty@yahoo.com>... Deferred: Connection timed out with mx4.mail.yahoo.com.
Warning: message still undelivered after 4 hours
Will keep trying until message is 5 days old

--k7U2BsfC002300.1156905836/babe1
Content-Type: message/delivery-status

Reporting-MTA: dns; babe1
Arrival-Date: Mon, 28 Aug 2006 21:27:23 -0400

Final-Recipient: RFC822; johnsanty@yahoo.com
Action: delayed
Status: 4.4.1
Remote-MTA: DNS; mx4.mail.yahoo.com
Last-Attempt-Date: Tue, 29 Aug 2006 22:43:56 -0400
Will-Retry-Until: Sat, 2 Sep 2006 21:27:23 -0400

--k7U2BsfC002300.1156905836/babe1
Content-Type: message/rfc822

Return-Path: <jonhsanty@babe1>
Received: from babe1 (localhost.localdomain [127.0.0.1])
by babe1 (8.13.5/8.13.5) with ESMTP id k7T1RNvY002973
for <johnsanty@yahoo.com>; Mon, 28 Aug 2006 21:27:23 -0400
Received: (from btamayo@localhost)
by babe1 (8.13.5/8.13.5/Submit) id k7T1RNVv002972
for johnsanty@yahoo.com; Mon, 28 Aug 2006 21:27:23 -0400
Date: Mon, 28 Aug 2006 21:27:23 -0400
From: John Santy <johnsanty@babe1>
Message-Id: <200608290127.k7T1RNVv002972@babe1>
To: johnsanty@yahoo.com

Subject: john

--k7U2BsfC002300.1156905836/babe1--

From MAILER-DAEMON@babe1 Tue Aug 29 22:47:57 2006
Return-Path: <MAILER-DAEMON@babe1>
Received: from localhost (localhost)
by babe1 (8.13.5/8.13.5) id k7U2BsfD002300;
Tue, 29 Aug 2006 22:47:57 -0400
Date: Tue, 29 Aug 2006 22:47:57 -0400
From: Mail Delivery Subsystem <MAILER-DAEMON@babe1>
Message-Id: <200608300247.k7U2BsfD002300@babe1>
To: <johnsanty@babe1>
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
boundary="k7U2BsfD002300.1156906077/babe1"
Subject: Warning: could not send message for past 4 hours
Auto-Submitted: auto-generated (warning-timeout)

This is a MIME-encapsulated message

Last edited by johnsanty; 08-29-2006 at 10:53 PM.
 
Old 08-30-2006, 10:11 AM   #14
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Quote:
Originally Posted by johnsanty
I tried the following:
telnet mx1.mail.yahoo.com 25

And I got the following, it seems that it is not connecting at all.
[johnsanty@babe1 ~]$ telnet mx1.mail.yahoo.com 25
Trying 4.79.181.14...
telnet: connect to address 4.79.181.14: Connection timed out
Trying 4.79.181.15...
telnet: connect to address 4.79.181.15: Connection timed out
Trying 67.28.113.71...
telnet: connect to address 67.28.113.71: Connection timed out
Trying 67.28.113.73...
Yes. Indeed it looks like your ISP is filtering any outbound connection to port 25. They are going out of their way to make your life more difficult!

Quote:

In the maillog, I got the following. I'm not sure if this message conveys that the message has been sent. Can you confirm?:

Aug 29 22:58:23 babe1 sendmail[3236]: k7U2wNiM003236: from=johnsanty, size=60, class=0, nrcpts=1, msgid=<200608300258.k7U2wNiM003236@babe1>, relay=johnsanty@localhost
Aug 29 22:58:23 babe1 sendmail[3237]: k7U2wNtV003237: from=<johnsanty@babe1>, size=324, class=0, nrcpts=1, msgid=<200608300258.k7U2wNiM003236@babe1>, proto=ESMTP, daemon=MTA, relay=localhost.localdomain [127.0.0.1]
Aug 29 22:58:23 babe1 sendmail[3236]: k7U2wNiM003236: to=johnsanty@yahoo.com, ctladdr=johnsanty (500/500), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30060, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (k7U2wNtV003237 Message accepted for delivery)
No. If I read it right, it only confirms that your own machine (localhost AKA babe1 AKA 127.0.0.1) got the message from you, and will later try to deliver it.

Quote:

However,in the /var/spool/mail, I got the following several of them since I have attempted many time to send an email.

Code:
   ----- Transcript of session follows -----
<johnsanty@yahoo.com>... Deferred: Connection timed out with mx4.mail.yahoo.com.
Warning: message still undelivered after 4 hours
Will keep trying until message is 5 days old
Yes, this one documents the real problem. In five days you'll get a final failure notice. You see that sendmail is trying to connect to yahoo's mx machine (just as you did manually above), and the connection times out just as it did for you.
Quote:
Does it look like a problem with my configuration or with yahoo? Also, I tried sending an email to my hotmail account and i got the same thing.
It's a problem with your "friendly" ISP, who doesn't let you talk to yahoo, or hotmail, or any SMTP server (except theirs, presumably). No, it's not a problem with yahoo -- I can talk to them just fine.
Quote:
Do you have any idea.....i dont know if it is my sendmail configuration or not. Do you know by any chance on how to configure the senmail?
Your configuration would be ok with a "normal ISP, but you may need to change it to work around the filtering.

I'm not using sendmail, so I'm not the right person to ask about its configuration. Many years ago I heard people say getting it right is close to black magic. But hopefully things got better. And there is documentation out there. A quick Google search turned up http://sendmail.org and you may particularly want to study the section about configuration, http://www.sendmail.org/m4/readme.html .

If after reading you still have questions, you may want to come back. Maybe even post a new question.
 
Old 08-30-2006, 11:21 AM   #15
drj000
Member
 
Registered: Sep 2004
Location: Houston, TX
Distribution: Fedora
Posts: 261

Rep: Reputation: 33
Try this

Ok, so to fix sendmail to forward through your ISP, here's what you need to do (all as root, of course):

First, you need to open up sendmail's config file (actually it's the master control file, but no reason to nitpick). If I remember correctly, it's /etc/mail/sendmail.mc

Look for a line that says something like "dnl define(`SMART_HOST', `smtp:smtp.yourisp.net')dnl."
If it doesn't say smtp, that's ok. smtp is the default if it's not specified. Change whatever address is in there to your isp's smtp address, and then delete the dnl at the beginning of the line (but not at the end). Be sure not to change any of the quotes, etc. It uses specific types of quotes, and screws up if the wrong kind are used.

Once you've done that, assuming your current directory is /etc/mail, type make. That should generate a new config file for you. If it didn't (you'll know if it says "Nothing to be done for all"), then you have to do it yourself. "m4 sendmail.mc > sendmail.cf" should do it. Then type service sendmail restart, and you should be good to go.

Now this assumes that you're ISP doesn't require you to login to the smtp server, or use a secure connection. I never was able to figure out how to get it working in that case. I just found another smtp server provided by my ISP that didn't require login.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to send email... bfloeagle Linux - General 5 10-13-2009 10:20 AM
write a shell script to send email yenonn Programming 4 02-11-2005 10:06 PM
Write a script to send an email from bash kpelczar Linux - Software 5 02-09-2005 04:19 PM
Allowing an IP to send email using my email server... culprit Linux - Networking 7 09-09-2003 12:24 PM
script to send email message meluser Programming 2 03-26-2003 12:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration