Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use PHP Mail() function within PHP-FPM? On Nginx?

Tags:

php

email

nginx

I have searched everywhere for this and I really want to resolve this. In the past I just end up using an SMTP service like SendGrid for PHP and a mailing plugin like SwiftMailer. However I want to use PHP.

Basically my setup (I am new to server setup, and this is my personal setup following a tutorial)

Nginx
Rackspace Cloud
PHP 5.3 PHP-FPM
Ubuntu 11.04

My phpinfo() returns this about the Mail entries:

mail.log                     no value
mail.add_x_header            On
mail.force_extra_parameters  no value

sendmail_from   no value
sendmail_path   /usr/sbin/sendmail -t -i

SMTP        localhost
smtp_port   25

Can someone help me to as why Mail() will not work - my script is working on all other sites, it is a normal mail command. Do I need to setup logs or enable some PHP port on the server?

My Sample script

<?
    # FORMS VARS

    // $to = $customers_email;
    // $to = $customers_email;

    $to = $_GET["customerEmailFromForm"];

    $subject = "Thank you for contacting Real-Domain.com";
    $message = "
    <html>
    <head>
    </head>
    <body>
    Thanks, your message was sent and our team will be in touch shortly.
    <img src='http://cdn.com/emails/thank_you.jpg' />
    </body>
    </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: <[email protected]>' . "\r\n";

    // SEND MAIL
    mail($to,$subject,$message,$headers);

?>

Thanks

like image 945
TheBlackBenzKid Avatar asked Nov 02 '12 10:11

TheBlackBenzKid


People also ask

How does PHP-FPM work with NGINX?

Install PHP-FPM PHP-FPM, on the other hand, runs outside the NGINX environment by creating its own process. Therefore when a user requests a PHP page the nginx server will pass the request to PHP-FPM service using FastCGI. The installation of php-fpm in Ubuntu 18.04 depends on PHP and its version.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

Can we send email through PHP script?

Using the PHP mail() function. PHP's built-in mail() function is one of the simplest ways to send emails directly from the web server itself. It just takes three mandatory parameters: the email address, email subject and message body—and sends it to the recipient.


2 Answers

If found that on a new installation of Ubuntu 14.04 with nginx and PHP-FPM (no apache), neither postfix nor mailutils were installed.

I used: sudo apt-get install postfix

(as in the recommended answer)

AND

sudo apt-get install mailutils

to get everything working on my server. Both were required. An entry in PHP.ini (as mentioned in the recommended answer) may also have helped, but without both of the other packages, it wouldn't have made a difference.

like image 51
TheDavidJohnson Avatar answered Oct 15 '22 07:10

TheDavidJohnson


As there is no value for sendmail_from you need to set one in php.ini:

sendmail_from = "[email protected]" 

Or in the headers when you call to mail:

mail($to, $subject, $message, 'From: [email protected]');

The email address should follow RFC 2822 for example:

Failing that, have you actually installed a working email system?

If not, you can install postfix with the following command:

sudo apt-get install postfix

See below for more information on configuring postfix for use with PHP in Ubuntu:

https://serverfault.com/questions/119105/setup-ubuntu-server-to-send-mail

like image 35
Mitch Satchwell Avatar answered Oct 15 '22 07:10

Mitch Satchwell