Skip to content

Instantly share code, notes, and snippets.

@nanusdad
Created March 12, 2024 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanusdad/c9f015d97c03705cc7d938fa1d7a0ab6 to your computer and use it in GitHub Desktop.
Save nanusdad/c9f015d97c03705cc7d938fa1d7a0ab6 to your computer and use it in GitHub Desktop.
Setting up sendmail relay on Ubuntu 22.04 (with PHP)

Setting up sendmail relay on Ubuntu 22.04 (with PHP)

Test relay, needs host and port

One can use telnet to test; output will be similar to below. (use your email instead of joe@doe.com)

telnet 172.16.10.100 8081
Trying 172.16.10.100...
Connected to 172.16.10.100.
Escape character is '^]'.
220 mailagent.test.com ESMTP Postfix (Ubuntu)
helo test.com
250 mailagent.test.com
mail from: noreply@test.edu
250 2.1.0 Ok
rcpt to: joe@doe.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
test from direct connection
.
250 2.0.0 Ok: queued as 10533C01CB

Check if email has been received. This will confirm that the relay us working.

Update sendmail.mc and build sendmail.cf

Add the mail relay host and port to sendmail.mc and build sendmail.cf file; restart sendmail.

sudo su -                                               # root user 
sudo vi sendmail.mc
# Add / edit the lines below
# define(`SMART_HOST',`[172.16.10.100]:8081')dnl
# define(`RELAY_MAILER_ARGS', `TCP $h 8081')dnl
# define(`ESMTP_MAILER_ARGS', `TCP $h 8081')dnl
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf        # Rebuild cf file
systemctl restart sendmail                              # restart sendmail

Test using sendmail

echo "Subject: sendmail test" | sendmail -v joe@doe.com

OR

create a text file containing

To: joe@doe.com
Subject: sendmail test
From: noreply@cmc.edu.in
Testing from command line "sendmail"

Use sendmail

sendmail -vt < /tmp/testmail.txt

Test using PHP

php -r 'mail("joe@doe.com","Testing php -v ".phpversion(),"php on ".gethostname());'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment