Page 1 of 1

Problem with Safe mode

Posted: Sun Apr 05, 2009 2:34 pm
by HHahn
I have tried SwiftMailer on two different websites, where it works fine.

On the third site, however, I am running into the following problem. The $Mailer->batchSend($Msg); call causes the following PHP error:

Code: Select all

Warning: mail() [function.mail]: SAFE MODE Restriction in effect. The fifth parameter is disabled in SAFE MODE. in /usr/(... removed ...)/SwiftMailer/lib/classes/Swift/Transport/SimpleMailInvoker.php on line 48
I have looked into SwiftMailer's source code, where I saw that indeed the mail() function is called with five parameters. I don't know what the fifth one (called $additional_parameters in the PHP manual) is meant for. You give it a default value null, which seems to imply that the PHP interpreter considers it to be defined, apparently causing the problem.

I know the ISP of this website is very "cautious". (They are the same one I mentioned earlier, who e.g. does not supply a cron, nor a command line access.)

Is there any possibility to run SwiftMailer while PHP is in safe mode? (Well, there seem to be many "levels" of safe mode, but I mean at least this situation with a the mail() function's fifth parameter disabled).
Maybe you could have us to specufy whether or not this parameter is "available", or so? And if there are other similar restrictions I am not (yet) aware of, you could possibly handle them in a similar way?

Re: Problem with Safe mode

Posted: Sun Apr 05, 2009 6:46 pm
by Chris Corbyn
I vaguely remember fixing this in version 3. I must have missed it in version 4.

In lib/classes/Swift/Transport/SimpleMailInvoker.php, edit:

Code: Select all

if (ini_get('safe_mode')) {
  mail(/* everything except the 5th parameter */);
} else {
  mail(/* everything as it is already written */);
}
I'll patch this in the code so it goes out in the next release.

Re: Problem with Safe mode

Posted: Mon Apr 06, 2009 4:22 am
by HHahn
Chris Corbyn wrote:I vaguely remember fixing this in version 3. I must have missed it in version 4.

In lib/classes/Swift/Transport/SimpleMailInvoker.php, edit:

Code: Select all

if (ini_get('safe_mode')) {
  mail(/* everything except the 5th parameter */);
} else {
  mail(/* everything as it is already written */);
}
I'll patch this in the code so it goes out in the next release.
Thanks, but with this I am getting the following error:

Code: Select all

<br />
<b>Fatal error</b>:  Uncaught exception 'ReflectionException' with message 'Class Swift_Transport_SimpleMailInvoker does not exist' in /usr/home/ws/hahn-informatica/www.hahn-informatica.nl/www/Lgl2008/SwiftMailer/lib/classes/Swift/DependencyContainer.php:287
Stack trace:
#0 /usr/home/[*** removed ***]/SwiftMailer/lib/classes/Swift/DependencyContainer.php(287): ReflectionClass->__construct('Swift_Transport...')
#1 /usr/home/[*** removed ***]/SwiftMailer/lib/classes/Swift/DependencyContainer.php(305): Swift_DependencyContainer->_createNewInstance('transport.maili...')
#2 /usr/home/[*** removed ***]/SwiftMailer/lib/classes/Swift/DependencyContainer.php(117): Swift_DependencyContainer->_createSharedInstance('transport.maili...')
#3 /usr/home/[*** removed ***]/SwiftMailer/lib/classes/Swift/DependencyContainer.php(355): Swift_DependencyContainer->lookup('transport.maili...')
#4 [color=#FF0000]/usr/hom[/color] in <b>/usr/home/[*** removed ***]/SwiftMailer/lib/classes/Swift/DependencyContainer.php</b> on line <b>287</b><br />
 
Note that the part marked red in the last line seems to be incomplete!

Just wondering: Do you actually use the fifth parameter for mail() anywhere in SwiftMailer? If not, why bothering at all to specify it?

On the other hand, object orientation is just a technology, not a "religion". So why should it be necessary to include this mail() method into a SwiftMailer class? Couldn't you simply invoke the mail() function directly instead of including it in your class?

Re: Problem with Safe mode

Posted: Mon Apr 06, 2009 4:31 am
by Chris Corbyn
Yes, the 5th parameter gets used. The default is Null, but the Transport itself passes in "-fsender@address.tld".

The class existed at the time you posted this error:
Warning: mail() [function.mail]: SAFE MODE Restriction in effect. The fifth parameter is disabled in SAFE MODE. in /usr/(... removed ...)/SwiftMailer/lib/classes/Swift/Transport/SimpleMailInvoker.php on line 48
I'm not sure why it's suddenly disappeared after your edits. What exactly got changed?

Re: Problem with Safe mode

Posted: Mon Apr 06, 2009 4:34 am
by Chris Corbyn
HHahn wrote:On the other hand, object orientation is just a technology, not a "religion". So why should it be necessary to include this mail() method into a SwiftMailer class? Couldn't you simply invoke the mail() function directly instead of including it in your class?
The indirection this class provides makes the unit tests possible without sending real email. It also abstracts away such behaviour as this safe_mode check.

Re: Problem with Safe mode

Posted: Mon Apr 06, 2009 5:27 am
by HHahn
Chris Corbyn wrote:Yes, the 5th parameter gets used. The default is Null, but the Transport itself passes in "-fsender@address.tld".

The class existed at the time you posted this error:
Warning: mail() [function.mail]: SAFE MODE Restriction in effect. The fifth parameter is disabled in SAFE MODE. in /usr/(... removed ...)/SwiftMailer/lib/classes/Swift/Transport/SimpleMailInvoker.php on line 48
I'm not sure why it's suddenly disappeared after your edits. What exactly got changed?
Strange. Something went wromg at uploading the edited file. On the server, the file turned out to be empty. I tried it several times, all with the same result. When I renamed and edited the backup copy of the original file, it worked.

However, I am now getting another problem: there is no more error message, but the $Mailer->batchSend($Msg) function seems to return without any value, and no message is sent. I am testing its return value and write something to the screen, but actually the screen remains empty. It does not hang or so, I am just getting an empty screen.

[edit:]
I use

Code: Select all

$Mailer = Swift_Mailer::newInstance($Transport);
$Number = $Mailer->batchSend($Msg);
 
printf ("...done");          // for debugging only! (to make sure something is written independently of $Number)
if ($Number )
{
  printf ("<p>%u messages sent</p>\n", Number );
}
else
{
  printf ("<p>Sending failed...</p>\n");
}
 

Re: Problem with Safe mode

Posted: Mon Apr 06, 2009 5:49 am
by Chris Corbyn
Sorry, you'll need to add "return" to the start of each of those lines, so that the mail() function return value is provided. I suspect this is the cause.

I've patched this code into my repository so it will be available in the next release. Here's my entire file:

Code: Select all

<?php
 
/*
 Invokes the mail() function in Swift Mailer.
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 
//@require 'Swift/Transport/MailInvoker.php';
 
/**
 * This is the implementation class for {@link Swift_Transport_MailInvoker}.
 * 
 * @package Swift
 * @subpackage Transport
 * @author Chris Corbyn
 */
class Swift_Transport_SimpleMailInvoker implements Swift_Transport_MailInvoker
{
  
  /**
   * Send mail via the mail() function.
   * 
   * This method takes the same arguments as PHP mail().
   * 
   * @param string $to
   * @param string $subject
   * @param string $body
   * @param string $headers
   * @param string $extraParams
   * 
   * @return boolean
   */
  public function mail($to, $subject, $body, $headers = null, $extraParams = null)
  {
    if (!ini_get('safe_mode'))
    {
      return mail($to, $subject, $body, $headers, $extraParams);
    }
    else
    {
      return mail($to, $subject, $body, $headers);      
    }
  }
  
}

Re: Problem with Safe mode

Posted: Mon Apr 06, 2009 10:14 am
by HHahn
Thanks for the addition. I already noticed this point and corrected it myself.
Nevertheless the problem mentioned occurred.

However, there must have been something quite different (no udea what) that went wrong, as it now works. Sorry to have bothered you.