3

I'm trying to send emails via php with on Elastix. When I run the php as a ssh it works but when I try to do it on Web (PHP) I doesn't send anything. This is my code:

#!/usr/bin/php -q  
<?php
    echo shell_exec('whoami');
    shell_exec("sudo sendEmail -f j.example@example.com -t example@example.com -u Subject -m Message-s example.com.mx -o tls=yes -xu j.user@example.com.mx -xp password");
?>

I think is something about permissions because when I run it on my command line the output is:

root

and the email is send. But in when I try to open the file via Web i doesn't send anything, The output is next:

Asteriks

So I tried to give Asteriks root permissions, but it doesn't work :(. In another try to do it I download the PHPMailer library but i doesn't send the email. I try to do it on Windows with Xampp and it works and is the same version of PHP (5.1.6).

This is my PHPMailer code:

<?PHP
    $mail = new PHPMailer;

    $mail->isSMTP();                                   
    $mail->Host = 'example.com';
    $mail->SMTPAuth = true;           
    $mail->Username = 'example@example.com';      
    $mail->Password = 'password';              
    $mail->SMTPSecure = 'ssl';             
    $mail->Port = 465;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );

    $mail->From = 'example@example.com';
    $mail->FromName = 'Name';
    $mail->addAddress('example@example.com'); 

    $mail->WordWrap = 50;                                
    $mail->isHTML(true); 

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    if(!$mail->send()) {

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

It works on Windows but it doesn't on Elastix Any help?

slava
  • 1,901
  • 6
  • 28
  • 32
  • 2
    the whoami is pointless, since you're using sudo - you're running `sendEmail` as root. since you've tagged this with phpmailer, where's your phpmailer code? And if you're using phpmailer, why use shell_exec in the first place? – Marc B Nov 09 '15 at 16:21
  • I added the code in a another Answer. Sorry I'm new in Stackoverflow :( Thanks for answer :) – Joan Peralta Nov 09 '15 at 16:33
  • you can edit your question. don't post additional information as an answer. In any case, what (if any) errors are you getting? If phpmailer DOESN'T spit out an error, then it's not a php problem. PHP's job is the equivalent of taking an envelope from you and walking it down the the mailbox on the street corner. once the envelope's dropped down the slot, php's job is done, and it reports success. If the postoffice then loses your mail, that's not php's problem, and isn't visible to php in the first place. – Marc B Nov 09 '15 at 16:33
  • No, I didn't get any error, just a blank page. – Joan Peralta Nov 09 '15 at 16:38
  • then something's nuking your script before it ever gets to the send() call. turn on display_errors and error_reporting. those should NEVER be off on a debug/devel system in the first place. – Marc B Nov 09 '15 at 16:40
  • Ok, I add error_reporting(-1); to the code and the display_errors are on. But it stills gives me a blank page – Joan Peralta Nov 09 '15 at 16:48
  • do it at the php.ini level. changing it in the script itself is pointless - if there's a compile-time fatal error, the ini_set() will never get executed at all. – Marc B Nov 09 '15 at 16:49
  • I already check the php.ini and display_errors is on and error_reporting is E_ALL... :( – Joan Peralta Nov 09 '15 at 17:00
  • then start commenting out code, adding debug statements, etc... figure out where/what's blowing up. – Marc B Nov 09 '15 at 17:01
  • Okay, I'm working on it, let me see. Thanks so mucho – Joan Peralta Nov 09 '15 at 17:07
  • Finally it give me something, this error: Parse error: syntax error, unexpected T_FUNCTION in /var/www/html/Nyssen/class.phpmailer.php on line 3040 – Joan Peralta Nov 09 '15 at 17:11
  • something's hosed in your copy of phpmailer, then. – Marc B Nov 09 '15 at 17:13
  • Yes, I'm downloading PHPMailer again and hope it works. Thanks man :') – Joan Peralta Nov 09 '15 at 17:18

1 Answers1

0

So, finally I was able to send emails via PHP. The PHPMailer Library didn't work properly on PHP 5.1.6 So I make a few chances on it since the error that (finally) gave me was:

Parse error: syntax error, unexpected T_FUNCTION in var/www/html/[...]/class.phpmailer.php on line 3040

So I chanced the code of class.phpmailer.php to this:

/**
     * Clear queued addresses of given kind.
     * @access protected
     * @param string $kind 'to', 'cc', or 'bcc'
     * @return void
     */
    protected function clearQueuedAddresses($kind)
    {
        //if (version_compare(PHP_VERSION, '5.3.0', '<')) {
            $RecipientsQueue = $this->RecipientsQueue;
            foreach ($RecipientsQueue as $address => $params) {
                if ($params[0] == $kind) {
                    unset($this->RecipientsQueue[$address]);
                }
            }
        //} else {
        //    $this->RecipientsQueue = array_filter(
        //        $this->RecipientsQueue,
        //        function ($params) use ($kind) {
        //            return $params[0] != $kind;
        //        });
        //}
    }
  • Done, and I added the answer to my question. Thanks – Joan Peralta Nov 11 '15 at 16:55
  • So technically the problem is that the code you commented out uses an anonymous function (http://php.net/manual/en/functions.anonymous.php) which is only supported in PHP 5.3+ Rather than commenting out potentially important code, perhaps you should consider using a PHP version that is newer than 8 years old (that is an eternity in the software development world). I don't know however why the if statement sent you down the wrong path to begin with. It should be handling the backwards compatibility aspect for you. – Mike Brant Nov 11 '15 at 17:04
  • LOL! I know, but I don't have the rights to update the version of PHP. So I have to work with that version :( – Joan Peralta Nov 11 '15 at 17:52