Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to mail to gmail from the Linux command line?

1,035 views
Skip to first unread message

Sunshine

unread,
Feb 23, 2015, 10:32:03 AM2/23/15
to
Linux has a "mail" and "mailx" command which, if set up right,
should allow quick emails from the Linux command line of the
format:

mailx -s "this is the subject" f...@gmail.com < "^M^M this is the body"

Is setting up mailx or mail to work with Google Gmail difficult?

Bit Twister

unread,
Feb 23, 2015, 1:24:27 PM2/23/15
to
On Mon, 23 Feb 2015 09:32:01 -0600, Sunshine wrote:
> Linux has a "mail" and "mailx" command which, if set up right,
> should allow quick emails from the Linux command line of the
> format:
>
> mailx -s "this is the subject" f...@gmail.com < "^M^M this is the body"

Pretty sure that will not work. Even without the ^M^M characters"


> Is setting up mailx or mail to work with Google Gmail difficult?

There are a few brief general terms which you need to understand and
who is talking to who.

MTA Mail Transport Agent (qmail, postfix, sendmail, exim,...) receive
from MUA, send to ISP or store on disk

MDA Mail Delivery Agent (dovecot, courier. cyrus,...) IMAP/POP3 server
providing email to MUA for reading

MUA Mail User Agent (thunderbird, kontact, knode, mail. mailx...) send
to MTA, read from MDA or ISP

You might want to read http://www.catb.org/~esr/faqs/smart-questions.html

My Linux distribution provides postfix as my MTA. To send email
upstream my ISP required me to provide an ID/PW to send mail.

My previous ISP also required ID/PW to send and use a SSL encryption
wrapper so I had to install/configure stunnel.

How easy will depend on setting your MTA to talk with the desired
upstream MTA and how you want to setup your MDA.

I used dovecot for my MDA. An easy 4 line definition placed in
/etc/dovecot/conf.d/ allows easy reading by Thunderbird.

I have hourly cron jobs using fetchmail to pull email down to all my Linux
users except for hotmail. There I use getlive.

William Unruh

unread,
Feb 23, 2015, 4:17:56 PM2/23/15
to
You sure have complicated things.
Anyway, postfix can send directly to gmail. Ie, you could just tall
postfix to sent mail to gmail directly and not through your ISP.
It will not need an ID/PWD.

Sunshine

unread,
Feb 23, 2015, 7:14:46 PM2/23/15
to
On Mon, 23 Feb 2015 18:23:45 +0000, Bit Twister wrote:

> My previous ISP also required ID/PW to send and use a SSL encryption
> wrapper so I had to install/configure stunnel.

I don't know what the ISP has to do with it.
The transfer is between my computer and the Gmail server.
Currently I'm set for IMAP to get mail from the Gmail server.
And, it's set for SMTP, I think, to send mail to the Gmail server.
My current MUA is Thunderbird.

The mailx (and mail) commands work. I just need to know the
syntax.

My telnet test just hung though ...
$ telnet mail.google.com imap
Trying 74.125.239.149...

$ telnet mail.google.com smtp
Trying 74.125.239.149...

Bit Twister

unread,
Feb 23, 2015, 9:39:41 PM2/23/15
to
On Mon, 23 Feb 2015 18:14:44 -0600, Sunshine wrote:
> On Mon, 23 Feb 2015 18:23:45 +0000, Bit Twister wrote:
>
>> My previous ISP also required ID/PW to send and use a SSL encryption
>> wrapper so I had to install/configure stunnel.
>
> I don't know what the ISP has to do with it.

In my case my ISP would be the next MTA that my Linux MTA would
contact for sending emails.

> The transfer is between my computer and the Gmail server.
> Currently I'm set for IMAP to get mail from the Gmail server.
> And, it's set for SMTP, I think, to send mail to the Gmail server.
> My current MUA is Thunderbird.

In that case your MUA is communicating directly with Gail's MTA.

>
> The mailx (and mail) commands work. I just need to know the
> syntax.

And there is where we have a mis-communication.

When you run the mailx command, the email is sent to your
installation's MTA.

Your MTA will see that the email is not for anyone on the system and
connect to whatever MTA you have configured as the relay host in order
to send/forward/pass the email onto the desired MTA.

> My telnet test just hung though ...
> $ telnet mail.google.com imap

I do not do google.com, I would have had guessed mail.google.com is to
retrieve your email, not send it.

Get into Thunderbird and look at the Outgoing Server. If it is
mail.google.com then whatever MTA you have chosen will need to be
configured to use that server as the relay host.

Once you have that set and restarted your MTA, all command line emails
will be sent through that server.

Sunshine

unread,
Feb 23, 2015, 10:40:51 PM2/23/15
to
On Tue, 24 Feb 2015 02:38:59 +0000, Bit Twister wrote:

> When you run the mailx command, the email is sent to your
> installation's MTA.

I guess that's why you guys had suggested postfix, whatever that
is (I had heard of sendmail, but not postfix).

Googling for postfix gmail setup for Kubuntu, this may work:

Configure Postfix to Use Gmail SMTP on Ubuntu
https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

Configure Postfix to Send Mail Using an External SMTP Server
https://www.linode.com/docs/email/postfix/postfix-smtp-debian7

William Unruh

unread,
Feb 23, 2015, 11:22:37 PM2/23/15
to
On 2015-02-24, Bit Twister <BitTw...@mouse-potato.com> wrote:
> On Mon, 23 Feb 2015 18:14:44 -0600, Sunshine wrote:
>> On Mon, 23 Feb 2015 18:23:45 +0000, Bit Twister wrote:
>>
>>> My previous ISP also required ID/PW to send and use a SSL encryption
>>> wrapper so I had to install/configure stunnel.
>>
>> I don't know what the ISP has to do with it.
>
> In my case my ISP would be the next MTA that my Linux MTA would
> contact for sending emails.

Why go through you ISP? Just send it directly on to gmail

>
>> The transfer is between my computer and the Gmail server.
>> Currently I'm set for IMAP to get mail from the Gmail server.
>> And, it's set for SMTP, I think, to send mail to the Gmail server.
>> My current MUA is Thunderbird.
>
> In that case your MUA is communicating directly with Gail's MTA.
>
>>
>> The mailx (and mail) commands work. I just need to know the
>> syntax.
>
> And there is where we have a mis-communication.
>
> When you run the mailx command, the email is sent to your
> installation's MTA.
>
> Your MTA will see that the email is not for anyone on the system and
> connect to whatever MTA you have configured as the relay host in order
> to send/forward/pass the email onto the desired MTA.
>
>> My telnet test just hung though ...
>> $ telnet mail.google.com imap
>
> I do not do google.com, I would have had guessed mail.google.com is to
> retrieve your email, not send it.
>
> Get into Thunderbird and look at the Outgoing Server. If it is
> mail.google.com then whatever MTA you have chosen will need to be
> configured to use that server as the relay host.
>
> Once you have that set and restarted your MTA, all command line emails
> will be sent through that server.

And why would he want or need to do that? It is possible that the dest.
mail server will not accept mail from his own computer, but will from
the ISP, but other than that his system can send it directly.


>

Bit Twister

unread,
Feb 23, 2015, 11:39:05 PM2/23/15
to
On Mon, 23 Feb 2015 21:40:50 -0600, Sunshine wrote:

> I guess that's why you guys had suggested postfix, whatever that
> is (I had heard of sendmail, but not postfix).

Postfix is a very powerful MTA with lots of flexibility.
You may want to bookmark http://www.postfix.org

> Googling for postfix gmail setup for Kubuntu, this may work:

Yeah, finally. Nice to know what distribution you are using.

> Configure Postfix to Use Gmail SMTP on Ubuntu
> https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/
>
> Configure Postfix to Send Mail Using an External SMTP Server
> https://www.linode.com/docs/email/postfix/postfix-smtp-debian7

I think those urls could use at least one more setting if not two.

The first is to have any root email's sent to your account.
Here you can see I have set bittwister to have root's email sent to me

# tail -11 /etc/postfix/aliases | head -5
# Person who should get root's mail. This alias
# must exist.
# CHANGE THIS LINE to an account of a HUMAN
root: bittwister

Once you have changed /etc/postfix/aliases
cd /etc/postfix/
postalias aliases

The other thing I can suggest is using generic to munge the email
header from suns...@my.antispam.invalid to rea...@gmail.com.
After changing generic, run
postmap generic

That way when you do something like
mail -s "Test generic" you...@gmail.com < /dev/null
in a sunshine terminal you should see
From: containing you...@gmail.com instead of
suns...@my.antispam.invalid which will not route for anyone wanting
to do a reply.

Here are some changesnippets from my main.cf which I think you might
find helpful.
# dif /var/local/vorig/etc/postfix/main.cf_vinstall /etc/postfix/main.cf
35a36,102
>
> #*****************************************************************
> # My changes appended to main.cf for my LAN nodes
> #
> # Modified aliases, generic, and created a smtp_auth file.
> # dovecot is my IMAP server
> #
> # Do a tail -12 /etc/postfix/aliases
> # you need to change postfix to your login id
> # and create the .db file with the command
> # postalias aliases
> # postfix reload
> #
> #*****************************************************************

>
> # #*************************************************
> # #*
> # #* If file changes be sure to change
> # #* /local/bin/postfix_changes
> # #*
> # #*************************************************
>
> mydestination = $myhostname localhost.$mydomain localhost $mydomain
> mynetworks = 127.0.0.0/8 192.168.0.0/24
>
>
> relayhost = smtp-server.MY.ISP.com
> relay_domains =
> inet_interfaces = all
> inet_protocols = ipv4
> unknown_local_recipient_reject_code = 550
> smtp_host_lookup = dns, native
>
> masquerade_exceptions = root
>
> default_destination_concurrency_limit = 1
> default_destination_recipient_limit = 3
>
> #*****************************************************************
> # Sending: swap out email addresses that appear inside messages (From:)
> # Recipients doing a reply, sends reply to second column.
> # Added a line like
> # bittw...@wm81.darkstar.test my_addy@sys_isp.net
> # to the generic file and create the .db file with the command
> # postmap generic
> # postfix reload
> #
> # file:///usr/share/doc/postfix/html/generic.5.html
> #
> #*****************************************************************
> smtp_generic_maps = hash:/etc/postfix/generic


Do look up the directives at http://www.postfix.org so you know what
they do.

Although your FQDN is correct for what you are doing, I would suggest
changing it to .test instead of .invalid.

Example:
$ hostname --fqdn
wb.home.test

Jasen Betts

unread,
Feb 24, 2015, 1:30:59 AM2/24/15
to
heriloom mailx can do it, I use it, but I used it unconfigured, for
testing servers

I think the BSD mailx is more limited, and IIRC mail only talks to a
local MTA.



--
umop apisdn

David W. Hodgins

unread,
Feb 24, 2015, 7:15:31 PM2/24/15
to
On Mon, 23 Feb 2015 23:21:54 -0500, William Unruh <un...@invalid.ca> wrote:

> And why would he want or need to do that? It is possible that the dest.
> mail server will not accept mail from his own computer, but will from
> the ISP, but other than that his system can send it directly.

That is the case with gmail. I can use the nc command, in a script, to
send to most servers, but not to gmail. Trying to send to
gmail-smtp-in.l.google.com fails with 451 4.5.0 SMTP protocol violation.
In order to send to gmail, I have to relay through my isp.

My ip address is dynamic, which is likely why it's being rejected.

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)

William Unruh

unread,
Feb 24, 2015, 8:29:41 PM2/24/15
to
On 2015-02-24, David W. Hodgins <dwho...@nomail.afraid.org> wrote:
> On Mon, 23 Feb 2015 23:21:54 -0500, William Unruh <un...@invalid.ca> wrote:
>
>> And why would he want or need to do that? It is possible that the dest.
>> mail server will not accept mail from his own computer, but will from
>> the ISP, but other than that his system can send it directly.
>
> That is the case with gmail. I can use the nc command, in a script, to
> send to most servers, but not to gmail. Trying to send to
> gmail-smtp-in.l.google.com fails with 451 4.5.0 SMTP protocol violation.
> In order to send to gmail, I have to relay through my isp.
>
> My ip address is dynamic, which is likely why it's being rejected.

Or does it check whether the return address has the same domain as the
address which is connecting to them?
So yes, if gmail puts some such filtering onto the mail then you
probably will need to go through some relay.

>
> Regards, Dave Hodgins
>

David W. Hodgins

unread,
Feb 24, 2015, 10:40:09 PM2/24/15
to
In this case, I was using the from address for my isp email account,
so the domain name of the from address did match the domain name
that shows up in a reverse dns lookup of my ip address.

I'd previously tried with my local username@$HOSTNAME, with the
same result.

Checking my isp's dns records, I see they have a SPF1 record limiting
sending to one /24, which my ip address is not in, so having it not
working using the isp mail address, does make sense.

Jasen Betts

unread,
Feb 25, 2015, 6:31:03 AM2/25/15
to
On 2015-02-24, David W. Hodgins <dwho...@nomail.afraid.org> wrote:
> On Mon, 23 Feb 2015 23:21:54 -0500, William Unruh <un...@invalid.ca> wrote:
>
>> And why would he want or need to do that? It is possible that the dest.
>> mail server will not accept mail from his own computer, but will from
>> the ISP, but other than that his system can send it directly.
>
> That is the case with gmail. I can use the nc command, in a script, to
> send to most servers, but not to gmail. Trying to send to
> gmail-smtp-in.l.google.com fails with 451 4.5.0 SMTP protocol violation.
> In order to send to gmail, I have to relay through my isp.

I have used telnet successfully.

> My ip address is dynamic, which is likely why it's being rejected.

More likely you're violating the SMTP protocol.


telnet gmail-smtp-in.l.google.com 25
Trying 2a00:1450:4010:c07::1b...
Connected to gmail-smtp-in.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP da3si14479541lac.0 - gsmtp
EHLO you
250-mx.google.com at your service, [2001:xxx:xxxx:xxxx::xxxx]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
MAIL FROM: <ja...@xnet.co.nz>
250 2.1.0 OK da3si14479541lac.0 - gsmtp
RCPT TO: <address...@gmail.com>
250 2.1.5 OK da3si14479541lac.0 - gsmtp
DATA
354 Go ahead da3si14479541lac.0 - gsmtp
Subjesc: Test
From: Jasen
To: address...@gmail.com

1
2
3
.
250 2.0.0 OK 1424857894 da3si14479541lac.0 - gsmtp


--
umop apisdn

David W. Hodgins

unread,
Feb 25, 2015, 3:46:56 PM2/25/15
to
Strange. It's working in telnet here too, but not with nc, with
identical input. I was using HELO instead of EHLO, but changing
that doesn't make any difference.

The nc program works ok with every other mail server I've tried.
Comparing nc and telnet, using wireshark, nc sends all of the lines
in one packet, where with telnet, there is one packet per line of
input.

Why that works with other mail servers, but not gmail, I don't know.

I'm not aware of a way to get telnet to take input from a file,
so it wouldn't be suitable for a script, unless something like
expect was used.

Jasen Betts

unread,
Feb 26, 2015, 1:01:09 AM2/26/15
to
All lines in one packet is non-standard, you must pause after saying
DATA, and should also pause at the start and after EHLO.
https://tools.ietf.org/html/rfc2920

> Why that works with other mail servers, but not gmail, I don't know.

gmail is checking. they may catch quite a few spammers that way.
checking also probably reduces load on their servers as after a
rejected DATA they don't need to attempt to parse body data as smtp
commands.

> I'm not aware of a way to get telnet to take input from a file,
> so it wouldn't be suitable for a script, unless something like
> expect was used.

I think redirection will work.

I have used chat (which AIUI is something like expect) with modemu, but it's
slow.

Heirloom mailx is another option, you can put the host, to, from, and
subject on the command line and the body on stdin. I use it for testing
SMTP servers. Must be "Heirloom", not "BSD".

"swaks" is another command-line smtp tool, I've only encountered it
recently and am not aware of all its capabilities.

--
umop apisdn

0 new messages