SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2012-05-24 14:23:20

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

HowTo: Quickly send messages via SMTP from the command line

This is fairly straightforward, and isn't really a howto, more like a tip, but I find it useful. Debian (well, most Linux distros) include BSD's mailx, a command line email client  dating back to the early years of computing. That client, invoked with the simple "mail" command, is configured for internal message queing, via the exim4 Mail Transfer Agent. Full knowledge about Mail User Agents, Mail Delivery Agents, Mail Transfer Agents and so on isn't necessary, this is a simple tip.

What you need to know is that you need to configure an application to do the emailing for you. You could use exim, but exim is really a server application. So, we'll use msmtp, a simple SMTP agent. Also, we'll install heirloom-mailx, which is the full suite of BSD mail tools.

sudo apt-get install msmtp heirloom-mailx

Now, I suppose most of you use gmail like me, so I'll base this on gmail. Other providers take some adjustments to the file. Put this in .msmtprc

# msmtp config file

account gmail 
host smtp.gmail.com
port 587
protocol smtp
auth on
from Your Name
user USERNAME@gmail.com
password xxxx
tls on
tls_nocertcheck
account default : gmail

Make the proper ownership

chmod 600 .msmtprc

and this in .mailrc

set sendmail=/usr/bin/msmtp

There are multiple ways to use mail. Try to send an email to yourself like this:

mail you@yourdomain.com

Input Subject, hit Enter. Type in text, when you're done, hit Enter for a new line, and press . + Enter (dot and Enter/Return, or just hit Control+D). Hit Control + D on Cc: and you're done.

Or like this

echo "Look at me" | mail you@yourdomain.com

Or send an attachment

echo "Sending porn" | mail -a ~/videos/porncollection.tgz you@yourdomain.com

You can put aliases in .mailrc, like so:

alias mom mrs.mom@aol.com
alias wife dear.wife@hotmail.com
alias mistress sweetiepie81@gmail.com

Then you can use the alias instead of the address.

If you want to be able to send mail via mailx as both user and root, you need to put the files in /etc/msmtprc and /etc/mail.rc

the man page for mail is extremely exhaustive, in proper BSD style. Just in case you wanna learn more. You an use mailx for reading all your mail if you want, you'll just need a program which will deliver the mail to you, but this is outside the scope of this tutorial.

Last edited by el_koraco (2012-11-25 22:02:32)

Offline

Help fund CrunchBang, donate to the project!

#2 2012-05-24 22:53:03

Kandid
Member
From: New York City
Registered: 2012-05-21
Posts: 15

Re: HowTo: Quickly send messages via SMTP from the command line

Thanks for the tutorial. big_smile
I am going to have to try this.

Offline

#3 2012-05-24 23:00:11

dubois
The Old Codger
From: Missoula, Montana
Registered: 2010-09-03
Posts: 2,238

Re: HowTo: Quickly send messages via SMTP from the command line

Well, sir, el_k.  I always thought you were good, but after reading your last few tips and howtos I've got to admit you're getting better and I've got another bookmark.  big_smile

Offline

#4 2012-05-24 23:31:54

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

Re: HowTo: Quickly send messages via SMTP from the command line

dubois wrote:

Well, sir, el_k.  I always thought you were good, but after reading your last few tips and howtos I've got to admit you're getting better and I've got another bookmark.  big_smile

That's the sweet spot with Linux, you never run out of things to fiddle with. I was into LVM and filesystems for a while, and once that got boring, I switched to mailing stuff. After that, who knows big_smile

This is actually useful, aside from being cool. You don't have to wait for a mail client to fire up and send, mail exits right after the EOT, and msmtp does its stuff in the background. So you only have to define your most frequently used aliases, and voila.

Offline

#5 2012-05-25 04:53:20

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 3,030
Website

Re: HowTo: Quickly send messages via SMTP from the command line

Thank you!
This could easily be used to send mails from a script too of course. cool


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#6 2012-05-25 05:09:31

gutterslob
#! Resident Bum
Registered: 2009-11-03
Posts: 3,130

Re: HowTo: Quickly send messages via SMTP from the command line

NIce thread, El K.

I've played and tweaked of some of these for my own uses in the past. You guys might find some love in em.
https://github.com/amery/msmtp-wrappers
https://github.com/redondos/mutt/tree/m … msmtpqueue


Point & Squirt

Offline

#7 2012-05-25 07:53:36

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: HowTo: Quickly send messages via SMTP from the command line

cool stuff mr koraco. thanks for writing that up.

Offline

#8 2012-05-25 08:10:38

machinebacon
#! unstable
From: China
Registered: 2009-07-02
Posts: 6,636
Website

Re: HowTo: Quickly send messages via SMTP from the command line

^ Will be put into the wiki, thanks el_koraco smile

Offline

#9 2012-11-24 23:51:34

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

This may be a stupid question but I'll ask it anyway  Will doing this and changing the sendmail_path in the php.ini file to /user/bin/msmtp be sufficient to use php's mail() command?


Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#10 2012-11-25 02:49:33

damionhh
Member
Registered: 2012-10-11
Posts: 37

Re: HowTo: Quickly send messages via SMTP from the command line

You can ignore this message.  Exim wants to see a : set sendmail="/usr/bin/msmtp" in the .mailrc file.  then the mail command worked, then i no longer got the weird message.  im not sure why im not getting any FROM line in the emails but at least it sending them


hey el_koraco
nice guide, thanks for the trick.  unfortunately (and you knew this was coming), i solved a bunch of little problems in my configuration and finally got it talking to my SMTP server.  now im getting a "Missing FROM and TO and other stuff" error and the mail won't send.  heres the debug output (with names changed to protect the innocent).  any tips appreciated.  the problem is i can SEE the FROM and TO in the output.  why does it kick it out?:

(also, if i run msmtp it works fine, if i use mail it says Unknown command: "sendmail=" and i DID edit the .mailrc line as you stated above, does Exim not recognize the sendmail= command?)

$ msmtp -v darren@another.xxx 
ignoring system configuration file /etc/msmtprc: No such file or directory
loaded user configuration file /home/darren/.msmtprc
falling back to default account
using account default from /home/darren/.msmtprc
host                  = smtp.another.xxx
port                  = 25
timeout               = off
protocol              = smtp
domain                = another.xxx
auth                  = LOGIN
user                  = darren@another.xxx
password              = *
passwordeval          = (not set)
ntlmdomain            = (not set)
tls                   = on
tls_starttls          = on
tls_trust_file        = (not set)
tls_crl_file          = (not set)
tls_fingerprint       = (not set)
tls_key_file          = (not set)
tls_cert_file         = (not set)
tls_certcheck         = off
tls_force_sslv3       = off
tls_min_dh_prime_bits = (not set)
tls_priorities        = (not set)
auto_from             = off
maildomain            = (not set)
from                  = darren@another.xxx
dsn_notify            = (not set)
dsn_return            = (not set)
keepbcc               = off
logfile               = /home/darren/.msmtp.log
syslog                = (not set)
aliases               = (not set)
reading recipients from the command line
<-- 220-smtp.another.xxx ESMTP whitelabel 1.2.1-a; Sat, 24 Nov 2012 18:22:24 -0800
<-- 220 Our servers are monitored and all connections are logged.
--> EHLO another.xxx
<-- 250-smtp.another.xxx Hello another.xxx [66.66.66.66], pleased to meet you.
<-- 250-ENHANCEDSTATUSCODES
<-- 250-SIZE 21343540
<-- 250-EXPN
<-- 250-ETRN
<-- 250-ATRN
<-- 250-DSN
<-- 250-CHECKPOINT
<-- 250-8BITMIME
<-- 250-AUTH PLAIN LOGIN DIGEST-MD5 CRAM-MD5 GSSAPI
<-- 250-STARTTLS
<-- 250 HELP
--> STARTTLS
<-- 220 2.0.0 Ready to start TLS
TLS certificate information:
    Owner:
        Common Name: *.another.xxx
        Organization: *.another.xxx
        Organizational unit: GT15894921
        Country: US
    Issuer:
        Common Name: RapidSSL CA
        Organization: GeoTrust, Inc.
        Country: US
    Validity:
        Activation time: Tue 21 Dec 2011 12:47:25 AM CST
        Expiration time: Mon 23 Jan 2013 12:22:58 PM CST
    Fingerprints:
        SHA1: 3E:60:F5:D5:39:4B:17:BD:CB:54:12:23:45:22:51:FF:13:43:EB:7E
        MD5:  28:B4:BB:16:66:A7:00:00:00:21:07:8B:57:0B:D6:66
--> EHLO another.xxx
<-- 250-smtp.another.xxx Hello another.xxx [66.66.66.66], pleased to meet you.
<-- 250-ENHANCEDSTATUSCODES
<-- 250-SIZE 2131354
<-- 250-EXPN
<-- 250-ETRN
<-- 250-ATRN
<-- 250-DSN
<-- 250-CHECKPOINT
<-- 250-8BITMIME
<-- 250-AUTH PLAIN LOGIN DIGEST-MD5 CRAM-MD5 GSSAPI
<-- 250 HELP
--> AUTH LOGIN
<-- 334 BXHYEcm5sdq6
--> hfy798JJKIo99==
<-- 334 TRYzcsdvfg566
--> MzYzssdfKJkjLK0=
<-- 235 2.0.0 Authentication successful
--> MAIL FROM:<darren@another.xxx>
<-- 250 2.1.0 <darren@another.xxx>... Sender ok
--> RCPT TO:<darren@another.xxx>
<-- 250 2.1.5 <darren@another.xxx>... Recipient ok; will forward
--> DATA
<-- 354 Enter mail, end with "." on a line by itself
sdfsdfsdfsdf
--> sdfsdfsdfsdf
sdfsdfsdf
--> sdfsdfsdf
sdfsdfsdf
--> sdfsdfsdf
.
--> ..
--> .
<-- 554 5.7.3 Missing FROM and TO and other stuff.
msmtp: the server did not accept the mail
msmtp: server message: 554 5.7.3 Missing FROM and TO and other stuff.
msmtp: could not send mail (account default from /home/darren/.msmtprc)

Last edited by damionhh (2012-11-25 03:33:49)

Offline

#11 2012-11-25 04:03:38

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

Re: HowTo: Quickly send messages via SMTP from the command line

I don't know man, I'm drunk, and this is a wall of text....

Offline

#12 2012-11-25 17:35:12

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

Another question after you re-hydrate... I tried putting the .mailrc file in the home directory and also tried copying it over to /etc and both places I get an error Unknown command "sendmail=".  I tried googling it but couldn't really find anything that would suggest any help.  Any ideas?


Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#13 2012-11-25 18:30:42

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

Re: HowTo: Quickly send messages via SMTP from the command line

show me the contents of .mailrc

Offline

#14 2012-11-25 21:53:28

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

Hello, here's the file:

freddythunder@crunchbang:~$ more .mailrc
sendmail=/usr/bin/msmtp

Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#15 2012-11-25 22:02:08

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

Re: HowTo: Quickly send messages via SMTP from the command line

I'm sorry, my mistake. It should be

set sendmail=/usr/bin/msmtp

Offline

#16 2012-11-25 22:20:17

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

Thanks for your help on that but I don't ting the config is setting.  When I test it, it comes back with /user/bin/sendmail: No such file or directory. shouldn't it be looking for msmtp instead? Sorry I don't know enough to keep this a "quick tip" but if I can get outgoing php mail to work then I can surely take over the world smile


Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#17 2012-11-25 22:35:24

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

Re: HowTo: Quickly send messages via SMTP from the command line

sudo cp .msmtprc /etc/msmtprc
sudo cp .mail.rc /etc/mail.rc

Offline

#18 2012-11-25 22:46:56

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

I had actually already tried that to see if the config was not working from the home directory when looking at the messages above and no dice.  Any other ideas?


Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#19 2012-11-25 23:34:04

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

Re: HowTo: Quickly send messages via SMTP from the command line

None. I'll get back to you if I think of anything.

Offline

#20 2012-11-26 03:30:53

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

I tried just running msmtp like damonhh did and it sent mail so I'm halfway there. If you think about something that can help please let me know.  Otherwise I'm going to search and/or post a new question about php mail() which is currently returning false but logging that mail is sent.  Thanks it is a good tutorial for mailing from the command line!


Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#21 2012-11-26 18:04:44

damionhh
Member
Registered: 2012-10-11
Posts: 37

Re: HowTo: Quickly send messages via SMTP from the command line

hey freddy_thunder have you seen this article?
http://www.absolutelytech.com/2010/07/1 … -on-linux/

Offline

#22 2012-11-26 18:58:39

Freddythunder
Member
Registered: 2012-11-21
Posts: 35

Re: HowTo: Quickly send messages via SMTP from the command line

@damionhh,
Yes and no - I found almost that exact same information on another website.  Except it didn't have the -t on the end of the sendmail_path.  I actually just got it working a little bit ago.  I had some permissions issues just looking into the error.log, but once I got in there, I saw that it wasn't getting recipients so I added the -t.  Then the error.log stated that there was no configuration file.  I went and chmoded the config files in /etc/ so that www-data can read it and all then it worked right away.  Thank you for your help!! Very exciting!


Sometimes you run across a problem where regex is the best answer.  Then you have two problems.

Offline

#23 2013-01-19 15:44:09

M.I.H.H
#! Member
From: /etc/init.d/
Registered: 2012-11-01
Posts: 84

Re: HowTo: Quickly send messages via SMTP from the command line

thank man, ill try it  smile


i will continue to seek knowledge.

WiFi Ralink RT3290 | AMD Driver | Intel IPS | Samba on Thunar

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo