
How to setup a local SMTP email server
If you need to setup a local SMTP email server for developer or testing purposes, this tutorial might be helpful for you. Bear in mind that this is made for an Ubuntu OS environment, with an Gmail SMTP server (you can use whichever you want).
First of all, you need to install ssmtp, to do so, let’s do the following:
1 |
sudo apt-get install ssmtp |
Once installed, you will need to configure /etc/ssmtp/ssmtp.conf file, commenting out the mailhub line, and adding the followings, at the bottom of the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
mailhub=smtp.gmail.com:587 UseSTARTTLS=YES AuthUser=<your_account>@gmail.com AuthPass=<your_password> # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=<your_localhostname> # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address #FromLineOverride=YES |
Afterwards, remember to restart your apache, and you will be able to send emails from your local.
Note: The following code-snipper might help you to test email sending:
1234567891011121314151617181920212223242526 <?php$to = '<your_income_email_address>';$subject = 'Marriage Proposal';$from = 'peterparker@email.com';// To send HTML mail, the Content-type header must be set$headers = 'MIME-Version: 1.0' . "\r\n";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";// Create email headers$headers .= 'From: '.$from."\r\n".'Reply-To: '.$from."\r\n" .'X-Mailer: PHP/' . phpversion();// Compose a simple HTML email message$message = '<html><body>';$message .= '<h1 style="color:#f40;">Hi Honey!</h1>';$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';$message .= '</body></html>';// Sending emailif(mail($to, $subject, $message, $headers)){echo 'Your mail has been sent successfully.';} else{echo 'Unable to send email. Please try again.';}
Or, for a quick try, you can perform the following on your CLI:
1 php -r "mail('<your_income_email_address>', 'test subject', 'test body message');"
Happy coding!