If your Unix-based operating system fails, you'll never know if you don't forward system emails. When your system is acting up, it will generate emails. However, a Unix system doesn't send emails by default because spammers hate everyone.

## Prerequisites

Before we begin, ensure you have:
- A Unix/Linux system with root access
- A Gmail account with 2FA enabled
- An App Password generated for your Gmail account

## Step-by-Step Configuration

1. Install mailx (use a package manager)

```sh
# For Debian/Ubuntu
sudo apt-get install mailutils

# For RHEL/CentOS
sudo yum install mailx
```

2. Edit your **mail.rc**

```sh
sudo vi /etc/mail.rc
```

3. Add the following configuration at the bottom of your **mail.rc**:

```sh
set smtp=smtps://smtp.gmail.com:465
set smtp-auth=login
set smtp-auth-user=USERNAME@gmail.com
set smtp-auth-password=YOURPASSWORD
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/ # You might need to adjust this depending on your Unix OS
```

4. Test the configuration

```sh
echo "My Test Message" | mail -v -s "My Test Subject" email@some.domain.tld
```

5. If successful, you'll see output similar to this:

```sh
Resolving host smtp.gmail.com . . . done.
Connecting to 74.125.199.108:465 . . . connected.
220 smtp.gmail.com ESMTP u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> EHLO homelab.is-leet.com
250-smtp.gmail.com at your service, [50.126.101.7]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
>>> AUTH LOGIN
334 VXNlcm5hbWU6
>>> amVmZmFiYWlsZXlAZ21haWwuY29t
334 UGFzc3dvcmQ6
>>> eHZhdHNldXhzb3FvcGt3ZA==
235 2.7.0 Accepted
>>> MAIL FROM:<jeff@homelab.is-leet.com>
250 2.1.0 OK u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> RCPT TO:<jeffabailey@gmail.com>
250 2.1.5 OK u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> DATA
354  Go ahead u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> .
250 2.0.0 OK  1650153154 u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
>>> QUIT
221 2.0.0 closing connection u12-20020a17090a890c00b001b8efcf8e48sm12327759pjn.14 - gsmtp
```

## Troubleshooting Common Issues

### SSL/TLS Errors
If you encounter SSL/TLS errors, ensure your system has the necessary certificates installed:

```sh
# For Debian/Ubuntu
sudo apt-get install ca-certificates

# For RHEL/CentOS
sudo yum install ca-certificates
```

### Authentication Failures
If authentication fails:
1. Verify your Gmail username is correct
2. Ensure you're using an App Password, not your regular Gmail password
3. Check that 2FA is enabled on your Gmail account

### Permission Issues
If you can't edit mail.rc:
```sh
sudo chmod 644 /etc/mail.rc
```

## Security Best Practices

1. Use App Passwords instead of your main Gmail password
2. Keep your mail.rc file permissions restricted
3. Regularly rotate your App Passwords
4. Monitor your Gmail account for suspicious activity

## Alternative Email Clients

While mailx is the traditional choice, consider these alternatives:
- Postfix
- Sendmail
- Exim

## Conclusion

Setting up system email notifications is crucial for system monitoring. With this guide, you can now receive important system alerts directly in your Gmail inbox.

Have a great day! 😎