Which utility can I use to send email via command line?

How do I configure mailx, postfix, sendmail, or another alternative to send e-mail out through my ISP’s SMTP server? My ISP’s SMTP server requires outgoing authentication. I don’t want to setup my own machine as a POP or SMTP server, I just want to send emails via BASH.

The purpose of this is I want to create a short PERL script to send my current public IP to my external email account. (I have a dynamic public IP).

Thanks for any help!

Hi
Just install the perl module Mail::SendEasy::SMTP which supports
authentication;

http://cpan.uwinnipeg.ca/htdocs/Mail-SendEasy/Mail/SendEasy/SMTP.html


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 11.0 x86 Kernel 2.6.25.18-0.2-default
up 1 day 13:31, 4 users, load average: 0.04, 0.05, 0.08
GPU GeForce 6600 TE/6200 TE - Driver Version: 177.82

You can also use mailx. It can be configured to talk directly to a SMTP server (your ISP’s for example). So you could send mail from a shell script. See the man page for mailx.

I read mailx man. And I configured automatic mail sending from one account to another. On first account(sending) I have to be authorized. I use that command:
env MAILRC=/dev/null from=sending_account@gmail.com smtp=smtp.gmail.com smtp-auth-user=sending_account smtp-auth-password=password smtp-auth=login mailx -n -s “subject” some_other_accoun@gmail.com </root/test.txt

And It doesnt work. In /var/spool/mail/root i see that mailx try to send e-mail from local account…
>>> MAIL From:<root@localhost.localdomain> SIZE=622
<<< 553 5.1.8 <root@localhost.localdomain>… Domain of sender address root@localhost.localdomain does not exist
501 5.6.0 Data format error

How can I fix it? I think that mailx doesnt use parameters like from or smtp where is all specificaton to mail sending account… :frowning:

It’s unfortunate that you picked gmail to be your SMTP server because gmail requires SMTP over TLS. Here’s what I get:

smtp-server: 530 5.7.0 Must issue a STARTTLS command first. 27sm1253633wfa.28

You will need the option smtp-use-starttls.

Why not you use your ISP’s SMTP server which should accept plaintext connections and not require a login.

Hi
Then there are gmail modules if you wish to go the perl route.


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 11.0 x86 Kernel 2.6.25.18-0.2-default
up 2 days 22:18, 2 users, load average: 1.40, 1.30, 1.28
GPU GeForce 6600 TE/6200 TE - Driver Version: 177.82

Gmail was only an example. I can use my ISP SMTP server but I want to have universal solution which I could use everywhere. BTW on my ISP SMTP it doesnt work too. I change smtp=smtp.gmail.com to smtp-use-starttls=smtp.gmail.com. But it doesnt change anything. Probably I have problem with mailx. Mailx doesnt use parameters specified in env MAILRC=… and sending mail with some default parameters.

env MAILRC=/dev/null from=sending_account@gmail.com smtp=smtp.gmail.com smtp-auth-user=sending_account smtp-auth-password=password smtp-auth=login mailx -n -s “subject” some_other_account@gmail(or_some_else).com </root/test.txt

And it is effect:
As You see in more /var/spool/mail/root:

>>> MAIL From:<root@localhost.localdomain> SIZE=622
<<< 553 5.1.8 <root@localhost.localdomain>… Domain of sender address root@localhost.localdomain does not exist
501 5.6.0 Data format error

Mailx doesnt authorize gmail sender_user on smtp gmail server…?

It worked for me, using gmail and adding smtp-use-starttls=yes. I think you are looking at a red herring. mailx doesn’t bounce the email so what you are looking at in /var/spool/mail/root is from a different error. mailx outputs the error to the terminal, and/or leaves it in dead.letter.

You could add the -v option to mailx to see the protocol exchange. I sent myself three emails via gmail using the same command as you did, with the required changes for me, of course.

It is possible you are using an older/different mailx. I’m using the one on OpenSUSE 11.0.

Another possibility is that your ISP blocks/redirects destination port 25 to hosts other than their SMTP server, to prevent malware infected machines from sending out spam. In that case you have to switch to port 465, the designated smtps port.

Thanks for advice.Ok I add option -v to mailx. My version of mailx is mailx-8.1.1-44.2.2.
env MAILRC=/dev/null from=sending_account@gmail.com smtp=smtp.gmail.com smtp-auth-user=sending_account smtp-use-starttls=yes smtp-auth-password=password smtp-auth=login mailx -n -s “subject” some_other_account@gmail(or_some_else).com </root/test.txt

And it is effect:
some_other_account@gmail.com… Connecting to [127.0.0.1] via relay…
220 localhost.localdomain ESMTP Sendmail 8.13.8/8.13.8; Fri, 21 Nov 2008 08:10:32 +0100
>>> EHLO localhost.localdomain
250-localhost.localdomain Hello priv_serv [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-DELIVERBY
250 HELP
>>> MAIL From:<root@localhost.localdomain> SIZE=115
250 2.1.0 <root@localhost.localdomain>… Sender ok
>>> RCPT To:<some_other_account@gmail.com>
>>> DATA
250 2.1.5 <some_other_account@gmail.com>… Recipient ok
354 Enter mail, end with “.” on a line by itself
>>> .
250 2.0.0 mAL7AWtZ002423 Message accepted for delivery
some_other_account@gmail.com… Sent (mAL7AWtZ002423 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 localhost.localdomain closing connection

As You can see mailx trying sending message direct from root(local) account. It doesn’t use parameters like from, smtp etc… Mailx doesn’t connect to gmail smtp server but is trying to use local SMTP server.?
I don’t have any SMTP server localy.If I have I wouldn’t have probably problem with mailx? But I don’t want to install any smtp server!!!

I need to do something (theoretically) very easy. Just sending message from (linux)client machine using some smtp server to some other mail address… Like normally using Outlook or Thunderbird but by scripts(BASH - command line)… :frowning:

OK. I made i “little” mistake. I had running sendmail daemon by other administrator. I turned off sendmail and now I have that message:
some_other_account@gmail.com… Connecting to [127.0.0.1] via relay…
some_other_account@gmail.com… Deferred: Connection refused by [127.0.0.1]

Somehow your mailx doesn’t seem to support changing the smtp host. My version is mailx-12.2-108.1 according to rpm -q mailx. The author is Gunnar Ritter

You didn’t say what version of OpenSUSE you are running, or even if you are running OpenSUSE. Looks like you need an up to date version of mailx.

Did a little digging. From your version number, it looks like you have classic Berkeley mailx. What you need is this:

Heirloom mailx

previously called nail.