Report this

What is the reason for this report?

How to set up server to send emails using mail() - a php function?

Posted on March 23, 2015

Hi, we’ve installed all programs we need in our droplet and we are using it to develop a project. So far we’ve set up in our ubuntu droplet: nginx, php 5 and ruby. We’ve installed php 5, we followed some tutorials here…it seems everything is set up, but when we use the command mail() in a php program inside our server, it seems to work, but the email is never received. Something is wrong…we think there may be something we forgot to set up, since we are used to use hosting services from companies that offer everything already set up within the infrastructure. could you please help us? Thank you!



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!

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.

Thanks for your help! great! All we’ve gotta do is run those commands? or there’s some extra configuration we’ll need to do?

You will need an MTA to process outgoing mail on your server. You can use sendmail or postfix (I recommend postfix). To install postfix:

apt-get update;
apt-get install postfix;

If someone really wants to use mail() while I wouldn’t recommend you can follow these steps:

  1. Check PHP Configuration:

    • Make sure that PHP’s mail() function is properly configured in your php.ini file. You can find the php.ini file location by running php --ini.

    • Ensure that the sendmail_path directive in your php.ini file is correctly configured to point to your system’s sendmail binary. It should look something like this:

sendmail_path = /usr/sbin/sendmail -t -i`
  1. Check for Errors:

    • Check the error logs in your web server and PHP for any error messages related to sending emails. Look in the Nginx error log and PHP error log for any clues.
  2. Test the mail() Function:

    • Create a simple PHP script that uses the mail() function to send an email to an external email address (e.g., a Gmail or Yahoo address). This will help determine if the issue is specific to certain recipient domains.

    • Check the return value of the mail() function to see if it returns true (indicating success) or false (indicating failure). You can use this return value for debugging.

$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email.';
$headers = 'From: your_email@example.com';

$result = mail($to, $subject, $message, $headers);

if ($result) {
    echo 'Email sent successfully.';
} else {
    echo 'Email sending failed.';
}

Here are a few alternatives:

  1. SMTP (Simple Mail Transfer Protocol): Configuring your PHP application to send emails via SMTP is a popular and reliable option. SMTP allows you to send emails through a designated email server, which can be a dedicated email server or a third-party email service. You can use PHP libraries like PHPMailer or Swift Mailer to facilitate SMTP email sending.

    • PHPMailer: PHPMailer is a widely-used PHP library for sending emails via SMTP or other methods. It provides a more robust and flexible way to send emails compared to the mail() function.

    • Swift Mailer: Swift Mailer is another powerful and feature-rich library for sending emails in PHP. It offers a clean and expressive API for sending emails via SMTP or other transport methods.

    Example using PHPMailer:

require 'vendor/autoload.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_username';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject';
$mail->Body = 'Email content';

if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed: ' . $mail->ErrorInfo;
}
  1. Third-Party Email Services: Consider using third-party email services like SendGrid, Mailgun, Amazon SES, or SMTP relay services provided by email hosting providers. These services offer high deliverability rates, tracking, and scalability. They often have PHP SDKs or libraries that make integration straightforward.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.