---
title: "在命令行下发送邮件（mailx+msmtp）"
author: "qdzheng"
date: "2009-11-15T23:43:00Z"
tags: []
slug: "mailx-msmtp"
draft: false
lastmod: "2009-11-15T23:43:36Z"
id: 517808
url: "https://eidolon.iteye.com/blog/517808"
---

有时候Linux管理员需要在命令行发送邮件到指定的邮箱。但是，又不愿在机器上装一个全功能的mail服务器（如PostFix、SendMail之类），在这种情况下，msmtp是个不错选择。

一、安装  
   pacman -S mailx msmtp  
二、msmtp的配置/etc/msmtprc，以gmail的smtp服务器为例：

```
host smtp.gmail.com
domain gmail.com
tls on
tls_starttls on
tls_certcheck off
protocol smtp


auth on
user yourname@gmail.com
password yourpassword
from yourname@gmail.com

```

需要将yourname、yourpassword改成实际的用户名及密码。  
三、mailx的配置  
默认情况下，mailx使用/usr/sbin/sendmail来发送邮件，这个程序包括在sendmail、postfix中。可以编辑~.mailrc配置文件来更改这个配置（做个/usr/bin/msmtp到/usr/sbin/sendmail的符号链接也可）：

```
set sendmail="/usr/bin/msmtp"
```

四、测试  
echo 'test from msmtp' | mail -s 'hello' demo@163.com