LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Scripted email (https://www.linuxquestions.org/questions/linux-software-2/scripted-email-324044/)

doctorwebbox 05-16-2005 12:36 PM

Scripted email
 
Every day at work we have to email a report to our HQ. I have written a script that puts the report together automatically as a cron job but then a user has to open our email client (thunderbird), create a new message, attach the report, fill out the subject and body of the email and send it.

Does anyone know of any tools I can use to send an email, with an attachment to a certain email address as part of a bash script?

trickykid 05-16-2005 12:52 PM

Use mail -s or possibly sendmail.. in a cron job or however you'd like

mail -s "Your Subject" recipient@domain.com < attachment.file

doctorwebbox 05-16-2005 03:38 PM

Getting Closer
 
Thanks for replying. I had a go at that, no errors were reported but no email was received at the other end. Does mail need some setting up first. I tried reading the manual but didn't find anything useful. Do I need to tell mail which smtp server and reply to address to use or does it look for this information in a config file somewhere or shouldn't any of this matter?

homey 05-16-2005 05:02 PM

Quote:

Do I need to tell mail which smtp server and reply to address to use or does it look for this information in a config file somewhere or shouldn't any of this matter?
On my box, the mail command works without any other configuring but if you are using it in a script, you may need to use the full path like this....
/bin/mail
or
/usr/bin/mutt

With mutt, you need to include a message txt or it goes into interactive mode which you surely don't want for a cron job.
-s for the subject
-a for the attachment
message.txt is a file which you typed up before hand

mutt -s "howdy" -a mypic.jpg fred@mydomain.com < message.txt

There is another thingy called uuencode. That gets installed with the sharutils package. Here is an example of how I sent some stuff but as a tar file....

tar cf - mypic.jpg | uuencode mypic.tar | mail -s "howdy" fred@mydomain.com < message.txt

doctorwebbox 05-17-2005 03:04 PM

Still Struggling
 
Thanks for the help. I tried using mutt, again no errors but no email received. I've tried it at home and at work, both on mchines running debian. This is what I did.

I put an image file on my desktop then tried to email it to myself:

adam@gabriel:~$ su
Password:
gabriel:/home/adam# apt-get install mutt
Reading Package Lists... Done
Building Dependency Tree... Done
Suggested packages:
urlview aspell ispell mixmaster openssl ca-certificates gnutls3
Recommended packages:
mime-support
The following NEW packages will be installed:
mutt
0 upgraded, 1 newly installed, 0 to remove and 150 not upgraded.
Need to get 0B/1418kB of archives.
After unpacking 3850kB of additional disk space will be used.
Selecting previously deselected package mutt.
(Reading database ... 112529 files and directories currently installed.)
Unpacking mutt (from .../archives/mutt_1.5.9-1_i386.deb) ...
Setting up mutt (1.5.9-1) ...

gabriel:/home/adam# exit
exit
adam@gabriel:~$ cd ~/Desktop/
adam@gabriel:~/Desktop$ echo "Hello there" > message.txt
adam@gabriel:~/Desktop$ mutt -s "Hello" -a Colipso.jpg me@myemailaddress.com < message.txt
adam@gabriel:~/Desktop$



But I don't receive an email. Am I doing something wrong? I have also tried sending from home to work and vice versa but still nothing.

kencaz 05-17-2005 03:33 PM

Re: Getting Closer
 
Quote:

Originally posted by doctorwebbox
Thanks for replying. I had a go at that, no errors were reported but no email was received at the other end. Does mail need some setting up first. I tried reading the manual but didn't find anything useful. Do I need to tell mail which smtp server and reply to address to use or does it look for this information in a config file somewhere or shouldn't any of this matter?
I use "mail" as well to send a lot of my e-mail, however, I did have to specify an SMTP server. I don't know how it would work without that information. I had to configure my "ssmtp.conf" file located in /etc/ssmtp/ Here is a sample of my configuration setup for comcast's SMTP server. Most of the file is just commented information on how to configure it. Valid options are Uncommented '#'

#
# /etc/ssmtp.conf -- a config file for sSMTP sendmail.
#

# The person who gets all mail for userids < 500
root=postmaster

# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and your mailhub is so named.
mailhub=smtp.comcast.net

# Example for SMTP port number 2525
# mailhub=mail.your.domain:2525
# Example for SMTP port number 25 (Standard/RFC)
# mailhub=mail.your.domain
# Example for SSL encrypted connection
# mailhub=mail.your.domain:465

# Where will the mail seem to come from?
#rewriteDomain=Comcast

# The full hostname
hostname=comcast.net

# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.
FromLineOverride=YES

# Use SSL/TLS to send secure messages to server.

#UseTLS=YES

# Use SSL/TLS certificate to authenticate against smtp host.
#UseTLSCert=YES

# Use this RSA certificate.
#TLSCert=/etc/ssl/certs/ssmtp.pem

KC

homey 05-17-2005 03:42 PM

Quote:

adam@gabriel:~$
Where I'm using a domain name....
[fred@sony ~]$ or sony.mydomain.com
So the from shows up as fred@sony.mydomain.com

Even so, I had to train thunderbird to not flag it as junk.

I wonder if your ISP or some spam blocker is kicking in.

benjithegreat98 05-17-2005 03:44 PM

Is sendmail running (or some other mta)? I'm not 100% sure but I think you at least need sendmail running before the mail command will send something out......

On your "gabriel" computer you can check out /var/log/maillog to see if an email is successfully sent out.

homey 05-17-2005 03:54 PM

Quote:

Is sendmail running (or some other mta)?
I think you're absolutely correct!
I just stopped postfix and the mail didn't come through.

doctorwebbox 05-18-2005 10:31 AM

Mail Server
 
So to use these tools, do I need to be running my own mailserver?

benjithegreat98 05-18-2005 10:49 AM

I've never used Debian (if that is what you are using) but on a lot of distro the config for sendmail is setup to where it will send emails out without further configuration. They are usually set up to not listen on any interface except localhost so you won't get attacked without realizing it.

So you will have to run sendmail (or an alternative such as Postfix, exim, etc) to send the emails out but it does not need to be able to receive email so it won't be a potential vulnerability. If you need help getting one of those setup to only send mail, we'd be happy to help. If you've never set one up, I've heard postfix is the easiest to configure. I use sendmail and it definately has a learning curve.

collen 05-19-2005 05:31 AM

Well have the same issue here.

i find it strainge that you can't send and email from the bash command line, without having a smtp server running.
software like thunderbird (and others) can, there you can supply an external smtp server.

so why can't that be done with mail or mutt ??
isn't there some other bash scripting way, or commandline program where you can specify an external smtp server ?

guess another script language might do the trick.. (php...)


Greetz

Collen..

benjithegreat98 05-19-2005 09:40 AM

telnet maybe

telnet somebody.com
helo benji.com
mail from:<sender@benji.com>
rcpt to:<dude@somebody.com>
data
Here is the body.
.
quit



I don't know if you can put that in a script, though. You have to wait for the server to respond before you put the next line in sometimes....... Plus that doesn't do things like check for an MX when you use a domain like somebody.com....

I think even if you use something like php, you still have to have an mta running for php webpage to send an email off.


All times are GMT -5. The time now is 10:15 AM.