-2

I need to send email from my website in PHP. By requirement, I can not use the built in mail() PHP function (so no sendmail). I downloaded 'mail' on my unix server and through a function in PHP should be able to connect in order to send emails. The PHP code is as follows:

<?php
    function sendMail() {
        shell_exec("echo mymessage | mail -s mysubject myreal@email.com");
    }
?>

Unfortunately it doesn't work, but if I run that command in the unix terminal (the machine is a Centos 7) it works. I also tried the system() and exec() PHP function, but without success. How can I do this work? Thanks

EDIT: This question is not the same. I must not use the mail() PHP function but an alternative

Davide
  • 1,931
  • 2
  • 19
  • 39

1 Answers1

0

Most frameworks have come to the same conclusion that mail() is not a good solution to send mail. What most frameworks do nowadays is they open a socket and pipe the mail commands directly over said socket. That's why using something like PHPMailer or Zend Framework 2 (which doesn't require you to use the entire framework) is preferred. It's a cleaner overall solution.

Your web services user may not have permissions to use the built-in Linux client. That's most likely why the shell command doesn't work. I would highly suggest avoiding shell commands and disabling them, as they are security risks.

Machavity
  • 30,841
  • 27
  • 92
  • 100