Sources:

After some trial and error and digging through the Sendgrid documentation I managed to successfuly find a way to send mail on linux using mailx. Sendgrid's documentation covers the use of the sendmail and ssmtp modules which require additional downloads. Granted both options are valid, I wanted to do it with mailx.

There is alot of documentation available on setting up mailx with gmail, and this is where I was able to start, crossed that over with sendgrids docs and about a dozen tries later I had mail sending through without errors (yes some configurations seemed to return me errors. I'll show you those aswell)


Configuring For Sendgrid

Basicaly the setup is very simple. On your linux system open up a terminal and type:

sudo nano /etc/mail.rc  

This will open up the config file for mailx. Scroll to the bottom of the config file and enter the following:

set smtp=smtp://smtp.sendgrid.net:587  
set [email protected]  
set smtp-auth=login  
set smtp-auth-user=mysendgridusername  
set smtp-auth-password=mysendgridpassword  

Replace mysendgridusername and mysendgridpassword with your sendgrid login credentials, and also set the from value to some useful email of some kind. The email you put in the set from value is what will default be used as to who the email is sent from. You can always override this when using the mail command and sendgrid accepts anyone as the recipient. You should note from Sendgrids's documentation that there is a maximum of 100 emails per connection. Also wouldn't be surprised if there is a max rate per hour also at that level.

After that save changes and your good to go. Interestingly enough mailx doesn't need anything restarted to work. Test out your configuration with some call like this:

mail -s "This is A Test Subject" [email protected]  

Hit enter and nothing will happen. This is where you can type the body of your email. When you are done hit enter. Type . and then hit enter again and the email should send.

Alternate Setup Attempts

Here I'm going to list some alternative ways to setup mailx that I was having it work in actualy sending mail, but mailx kept spitting out obscure errors after sending.

set smtp=smtp://smtp.sendgrid.net:587  
set [email protected]  
set ssl-verify=ignore  
set smtp-use-starttls  
set smtp-auth=login  
set smtp-auth-user=mysendgridusername  
set smtp-auth-password=mysendgridpassword  
set nss-config-dir=~/.mozilla/firefox/pblrihc5.default/  

This setup was the generally recommended setup for working with gmail. It uses a certificate from Firefox and explicitly enforces tls and along with that ignoring of the ssl certificates (what that means about the setup that worked I don't know. It wouldn't surprise me if it is actually sending in plain text). It sends everything successfully, the odd issue is after sending it returns an error that the peer's certificate is invalid.