Advice on sending email from command line via Gmail...

I'd like to send one-line emails from the command line[1], via my Gmail account. I have searched here and on line but most of the search results are from before Google changed the interface to Gmail around 2022. Does anyone have any advice? Thanks.


[1] or at least from the text mode application I use at work, written in dBase language but compiled with Harbour Compiler.
uname -a =
FreeBSD pr5.blank.blank 13.2-RELEASE-p10 FreeBSD 13.2-RELEASE-p10 GENERIC amd64
 
afaik mutt() can handle imap and smtp, maybe that helps. (it even provides some batch processing functionality, so maybe that can be used to send mails directly fromm the command line)
 
Is your e-mail sending and receiving set up? With an MTA (like sendmail or DMA) and an IMAP/POP receive program (like fetchmail)?

If yes, all it takes to send an e-mail is the mail command, which can be run from the CLI, for example such:
echo "Hallo world this is Exeter" | mail -s "Hi there" foobar@example.com

The mail program is an MUA or "mail user agent"; the program that you type messages to be sent into, or use to look at messages you have received. There are also many other CLI-based e-mail programs or MUAs, including mutt, alpine, and elm; the advantage of mail is that it is part of the FreeBSD base, and always there; it also exists on most other Unixes (such as Linux). All these MUAs can talk to the MTA or "mail transfer agent" to send mail, and they use copies of the messages stored on your computer by a receive program. The bigger and more complex MUAs can usually also talk directly to an IMAP server, which is probably easier and safer than configuring fetchmail. But for sending e-mail, the simplicity of mail is hard to beat.
 
You may need to do a bit of setup I think Gmail recently did stuff to make it harder to use "third party" applications. I ran across this with Claws-mail
 
ssmtp and msmtp both frequently show up in searches, however ssmtp is unmaintained. Anybody tried them?
 
I use msmtp. Gmail did make changes making it more difficult to use with 3rd party apps, supposedly for our safety, more likely part of google's attempt to close out anyone else. I give instructions on my mutt page https://srobb.net/mutt.html#getmail

It's about halfway down, you can search the page for May 30th, it's just under that. That part talks about setting the app password for getting mail, but you can use the same password for sending with msmtp. (I go into setting up msmtp under the sending mail section).
Ah I see I made a separate page for it to make it easier to find.

 
In my case, I didn't need oauth. I don't know if it's changed, but when I did it, I just had to set a special app password. It may have changed, Google gets more and more..proprietary seems the best word.
 
I use msmtp. Gmail did make changes making it more difficult to use with 3rd party apps, supposedly for our safety, more likely part of google's attempt to close out anyone else. I give instructions on my mutt page https://srobb.net/mutt.html#getmail

It's about halfway down, you can search the page for May 30th, it's just under that. That part talks about setting the app password for getting mail, but you can use the same password for sending with msmtp. (I go into setting up msmtp under the sending mail section).
Ah I see I made a separate page for it to make it easier to find.

Thanks very much for your detailed instructions and the effort! My problem is try as I might, gmail/account/security with 2FA refuses to show any option for App password. Private account, advanced protection options not even shown, so I am hoping off, passkey disabled, etc. This is one of my many gmail accounts, so am clueless at this point. If you would know of a pointer, I'd appreciate it.
 
I don’t know about 2fa support, I use sendEmail for this kind of tasks. It’s available as a binary package and most likely in the ports as well.
 

VladiBG,​

in your link is not described how sendmail can connect as client to gmail with xoauth2.
I also have such a posting from the time that google did not impose xoauth2:


It does not help here.

I would recommend the OP to use other service than gmail and use sendmail as in this posting, then he can
use a lightweight program like mail.
 
Ah I see I made a separate page for it to make it easier to find.
You enable 2FA, get a pass for your "app" on the computer, and you can log in the computer with that pass
without confirming it every time on the phone?

If that is the case, one could also configure sendmail as you configured getmail and mutt.

In spite of it, I would avoid it, perhaps google makes from time to time troubles to log in, and if you do not have
the phone, you cannot read and write email.
 
in my case, no. I only had to do the 2FA the first time I set it up. I think that OATH is more of a pain, but I've never had to do it. As I mention on my page, I think I've had to use the 2FA once or twice. I always use the same machine to send and receive gmail, which I'm sure is a factor in not having to always use 2FA. And hruodr is right, I think, if I'm only using one machine I could do the same with sendmail.
 
in my case, no. I only had to do the 2FA the first time I set it up.

2FA affects only "apps"? Or also googles webmail?

After you enabled 2FA, are you able to log in googles webmail wirthout 2FA in any machine?

I think that OATH is more of a pain, but I've never had to do it.
Yes, because you need an adequate "app" (so are mail clients now called).
In your 2FA you only get a special pass for a normal "app" and have to eventually confirm the login with a different device.
In spite of it, my fear is to be hindered to login in a critical situation.
This happens to me from time to time even if I did not enable 2FA.
Security means to make life difficult!
 
@hrodr, actually, I only turned it on to get getmail and msmtp working and only on one machine. Now, I just tried logging into webmail on a second machine, and it did ask for 2FA. However, I almost always just have getmail download my email to one machine, which has already been authorized, so for my use case, it hasn't been an issue.
 
Back
Top