Question

PHP mail() does not work on digitalocean ubuntu 16.04 server

my tools: server= digitalocean domain= godaddy installed apache/2.4.18 php 7.0.30 ubuntu 16.04

i have tried install sendmail and configure it as below:

sudo apt-get install sendmail

and configure the /etc/hosts

sudo nano /etc/hosts

i am sure it contains 127.0.0.1 localhost localhost.localdomain myhostname and i made

sendmailconfig

yes for all questions in sendmail config. also restarted apache2 with

sudo service apache2 restart

it is not sending mail. can u help me?

i tried send mail with send.php it contains:

<?php
$to = 'sendto@outlook.com';
$title = 'title of mail';
$content = 'hello from world';
$titles = 'From: sendfrom@outlook.com' . "\r\n" .
    'Reply-To: sendfrom@outlook.com' . "\r\n" .
    'X-Mailer: PHP/' .phpversion();
mail($to, $title, $content, $titles);
?>

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
June 12, 2023
Pinned Answer

Heya all,

In case anyone else stumbles upon this question I’ll try to provide points on what could be the problem here.

PORT 25: DigitalOcean block port 25 on new and sometimes older accounts. This is something DigitalOcean has been doing to reduce spam on it’s network. It is more that we simply cannot accurately determine who is going to send it.

Stopping spam is a constant fight, so DigitalOcean has implemented some restrictions on newer accounts. Having said that, you can always contact them and ask for the port block to be lifted on:

https://www.digitalocean.com/support/

More information here:

https://docs.digitalocean.com/support/why-is-smtp-blocked/

Use SSL/TLS ports 465/587: Using SMTP would be the way forward. For you to do that, you’ll need to use either port 465 or port 587

How to use SMTP with port 465/587 and PHP mail():

The built-in PHP mail() function does not provide an option to specify the SMTP server or the port to be used for sending emails. It’s just a simple interface for sending mail using the built-in mail server which is typically configured at the system level, and not meant for advanced email sending features.

If you need more control over the mail sending process like specifying the SMTP server and port, using SMTP authentication, or sending HTML emails, it’s recommended to use a library like PHPMailer, SwiftMailer or Zend_Mail.

Here is an example of how to send mail using PHPMailer with SMTP on ports 465 or 587:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

// Enable verbose debug output
$mail->SMTPDebug = 3;                               

// Set mailer to use SMTP
$mail->isSMTP();                                     
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

You can adjust the SMTP server, username, password, and port according to your mail server settings. Use SMTPSecure = 'ssl' and Port = 465 if your server requires SSL connection on port 465.

Other Struff to try :

  1. Check the Mail Queue and Logs: Sendmail logs its messages to /var/log/mail.log or /var/log/syslog depending on your system configuration. Check these files to see if there are any error messages that could help you debug the issue. You can also use mailq command to see if your mails are stuck in the queue.

  2. PHP Mail Configuration: Check your PHP configuration file (php.ini) to ensure that the sendmail_path directive is set correctly. It should be something like sendmail_path = /usr/sbin/sendmail -t -i.

  3. Sendmail Configuration: Make sure your Sendmail is configured correctly. The /etc/mail/sendmail.mc file is the configuration file for Sendmail. After any change in this file, you need to generate the sendmail.cf file by running m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf and then restart the Sendmail service.

  4. Hosting Provider Restrictions: Some hosting providers restrict outbound SMTP to prevent spam. Check with DigitalOcean to ensure they aren’t blocking outbound connections on port 25.

  5. SPF Records: Check if your domain’s SPF records are set correctly. If not, your emails might be being marked as spam. An SPF record is a type of Domain Name Service (DNS) record that identifies which mail servers are permitted to send email on behalf of your domain.

jarland
DigitalOcean Employee
DigitalOcean Employee badge
July 11, 2018

Hello friend!

This email should fail to be delivered at outlook.com because your server will not be an authorized sender for outlook.com. Microsoft will drop this and consider it a spoofed/spam sender. You can read more about SPF, the underlying function for this, here:

http://www.openspf.org/FAQ/How_does_it_work

You will want the From email to always be from a domain that you can speak for, so that you can delegate the authority properly.

Kind Regards, Jarland

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel