Mailx: Difference between revisions

From Leo's Notes
This page was last edited on 6 January 2020, at 23:18.
m Text replacement - "Category:Linux{{Navbox Linux}}" to "{{Navbox Linux}}Category:Linux"
No edit summary
Line 1: Line 1:
{{DISPLAYTITLE:mailx}}
{{code|mailx}} is a Linux command line mail client. It is typically used to send mail from shell scripts. By default, {{code|mailx}} will use a local MTA (typically sendmail) to actually transfer emails to a mail server.  If your machine does not have sendmail or any other MTAs, you will need to configure {{code|mailx}} to use an external mail server.


By default, {{code|mailx}} will use a local MTA (usually sendmail) to transfer emails. If the machine you're using doesn't have sendmail, {{code|mailx}} will fail to send the email.
== Usage ==
The email message is piped to {{code|mailx}}. Specify {{code|-s}} for subject. Any options are passed as variables set using {{code|1=-S key=value}}. Recipients are specified at the end.


This can be resolved by specifying a mail server directly, in the case below, the SMTP server:
For example, to send a test message using an external SMTP server:
mail -s "subject" -S smtp=smtp://smtp.home.steamr.com "recipient@home.steamr.com"
{{highlight|lang=terminal|code=
$ echo "Hello there." | mail -s "test" -S smtp=smtp://mail.steamr.com:25 "recipient@steamr.com"
}}


You can set the default SMTP server by defining it in {{code|/etc/mail.rc}} with the following line:
Rather than specifying multiple values with multiple {{code|-S}} parameters, values can also be defined in {{code|/etc/mail.rc}} using {{code|1=set key=value}}. For example, to use an external mail server with SMTP authentication:
set smtp=smtp://smtp.home.steamr.com  
{{highlight|lang=text|code=
set smtp=smtp://mail.steamr.com:587
set smtp-auth=login
set smtp-auth-user="home@steamr.com"
set smtp-auth-password="mein-passwort"
set smtp-use-starttls
set from="Home Robot <home@steamr.com>"
}}


To verify whether mail is being sent successfully, use {{code|-v}} for verbose outputs.
{{highlight|lang=terminal|code=
root@nas:~# echo "testing" {{!}} mail -v -s testing xyz@xyz.xyz
Resolving host mail.steamr.com . . . done.
Connecting to 107.189.11.145:587 . . . connected.
220 mail.steamr.com ESMTP ready
>>> EHLO nas
250-mail.steamr.com
250 STARTTLS
>>> STARTTLS
220 2.0.0 Start TLS
Comparing DNS name: "mail.steamr.com"
>>> EHLO nas
250-mail.steamr.com
250 AUTH PLAIN
>>> AUTH LOGIN
334
>>> Vh
334
>>> VU
235 2.0.0 OK
>>> MAIL FROM:<xyz@xyz.xyz>
250 2.1.0 Ok
>>> RCPT TO:<xyz@xyz.xyz>
250 2.1.5 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as 25661254B6
>>> QUIT
221 2.0.0 Bye
}}


{{Navbox Linux}}[[Category:Linux]]
{{Navbox Linux}}[[Category:Linux]]
[[Category:Todo]]

Revision as of 23:18, 6 January 2020

mailx is a Linux command line mail client. It is typically used to send mail from shell scripts. By default, mailx will use a local MTA (typically sendmail) to actually transfer emails to a mail server. If your machine does not have sendmail or any other MTAs, you will need to configure mailx to use an external mail server.

Usage

The email message is piped to mailx. Specify -s for subject. Any options are passed as variables set using -S key=value. Recipients are specified at the end.

For example, to send a test message using an external SMTP server:

$ echo "Hello there."

Rather than specifying multiple values with multiple -S parameters, values can also be defined in /etc/mail.rc using set key=value. For example, to use an external mail server with SMTP authentication:

set smtp=smtp://mail.steamr.com:587
set smtp-auth=login
set smtp-auth-user="home@steamr.com"
set smtp-auth-password="mein-passwort"
set smtp-use-starttls
set from="Home Robot <home@steamr.com>"

To verify whether mail is being sent successfully, use -v for verbose outputs.

root@nas:~# echo "testing" | mail -v -s testing xyz@xyz.xyz
Resolving host mail.steamr.com . . . done.
Connecting to 107.189.11.145:587 . . . connected.
220 mail.steamr.com ESMTP ready
>>> EHLO nas
250-mail.steamr.com
250 STARTTLS
>>> STARTTLS
220 2.0.0 Start TLS
Comparing DNS name: "mail.steamr.com"
>>> EHLO nas
250-mail.steamr.com
250 AUTH PLAIN
>>> AUTH LOGIN
334 
>>> Vh
334 
>>> VU
235 2.0.0 OK
>>> MAIL FROM:<xyz@xyz.xyz>
250 2.1.0 Ok
>>> RCPT TO:<xyz@xyz.xyz>
250 2.1.5 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as 25661254B6
>>> QUIT
221 2.0.0 Bye