Running script that sends an html formatted email fails when its run as cronjob

Hi

Im very new at working with unix and this problem I simply can not understand. I know there are a lot of threads about problems with shell scripts behaving differently when run from a terminal and from a cronjob. I have tried everything(almost) but I still havent cracked this problem.

Im working with typo3 and an extension called aux_newsmailer. The extension, when working properly, is suppose to send out newsletter automatically to users. Either formatted as plain text or html. My problem is that the extension has a backend userface where you manually can send the newsletters which works. Also, running the mailer script from a terminal works fine. When I, however, tries to run the script from a cronjob the html mails are somehow not received by all users and those mails that does are not formatted correctly, images are missing and links are not formatted the right way.

I have concluded that the error has something to do with the sending/formatting process but thats all I know.

Has anyone any idea what might be the problem?

/Nightowl

The usual problem when a cron job script doesn't work, but the script works from the command line is that the environment is different. Usually, the PATH must be defined in the script.

If the problem is not the PATH, check other variables that the script depends on.

Ok, I have been around this before and the environment is quite different, though, Im not sure which variables the script depends on or how to change the variables like PATH. How is this done?

I have tried to type:
PATH=/root/bin etc, but this returns an error

Can you help?

What is the error that u r getting?

PS:U can place 2>error.log in ur cron script to get the error messages logged to a file.

Read the script and see what variables it uses but does not set.

Where did you type it?

What, exactly, did you type?

What is the error?

I dont think any of the variables are actually used or I just dont get it :slight_smile:

Here is the shell script written in php:

#! /usr/bin/php -q
<?php

// set typo path to point to the root of the T3 installation
$typopath='/var/www/typo3/mysite/';

// create a BE user called _cli_auxnewsmailer (must not be admin), for better security change it to somethine else.
$MCONF['name'] = '_CLI_auxnewsmailer';

// *****************************************

// Standard initialization of a CLI module:

// *****************************************

if (@is_file($typopath.'typo3conf/ext/aux_newsmailer/mod1/index.php'))
$modulepath=$typopath.'typo3conf/ext/aux_newsmailer/';
else
$modulepath=$typopath.'typo3/ext/aux_newsmailer/';

// Defining circumstances for CLI mode:

define('TYPO3_cliMode', TRUE);

//$BACK_PATH = '../../../../typo3/';
define('TYPO3_mainDir', 'typo3/');
define('PATH_thisScript',$typopath.'typo3/typo3');
define('TYPO3_MOD_PATH', $modulepath.'/mailer/');
// Include init file:
require($typopath.'typo3/init.php');
require($typopath.'typo3/sysext/lang/lang.php');

$LANG=t3lib_div::makeInstance('language');
$LANG->init('default');

require($modulepath.'mod1/class_auxnewsmailer_core.php');

$mailer=new tx_auxnewsmailer_core;
$mailer->init();

$mailer->batch($argv[1],'','');

?>

I typed in the PATH=/root/bin in the top of this document and the error returned was
PHP Warning: Cannot modify header information - headers already sent by (output started at /var/www/typo3/mydomain/typo3conf/ext/aux_newsmailer/mailer/mailer.sh:3) in /var/www/typo3/mydomain/typo3_src-4.2.1/t3lib/class.t3lib_userauth.php on line 278

That's a PHP script, not a shell script.

You cannot use shell syntax in PHP.

According to different articles on the web, its still a shell script even though its written in php or are they wrong?

I guess I can conclude it has nothing to do with the environment variables after all?

They are quite wrong. It would be interesting to get links to those different articles.

Here you go:
PHPBuilder.com, the best resource for PHP tutorials, templates, PHP manuals, content management systems, scripts, classes and more.

The point of that article is that you can use PHP to write other things than web pages. If you put #!/usr/local/bin/php -q as the first line of the script, you can use PHP as a regular command-line scripting language. Page 5 of the article explains how you can mix PHP and sh in the same file by embedding a PHP script inside a bash script, or vice versa; but that still doesn't mean you can mix them freely as if they were the same language.

(The author seems to fail to realize that he could single-quote the here document so that he would not have to backslash-escape the variable names in the embedded PHP script:

#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << 'EOF'
<?php
	$myVar = "PHP";
	print("This is the $myVar section of the code.\n");
?>
EOF

The same techniques are routinely used for embedding sed or awk or Perl or SQL inside shell scripts.)

Thank you, but with lines like "While PHP as a shell script..." its easy to get confused right?

Anyway, we are getting side tracked here. We have concluded that its not a real shell script, so the shell environment variables are not really important and can't cause the problem.

The environment that the PHP script receives might very well still be the problem. You can create a sh wrapper to set up things the way you like, and invoke that from cron.

#!/bin/sh
PATH=/ick/y/poo:$PATH
export PATH
/path/to/mailer.php

I dont know what a sh wrapper is, can you explain? Do I need to create a new file, fx wrapper.sh and insert code like the sample in your post and the call that script before my other script?

Yes, so the idea is to create a script which runs your script. The new script is a "wrapper" which sets up things so they are correct for your script to run, then runs it. You would invoke the wrapper from cron instead of invoking the original script directly.

Thank you for helping - I tried to copy all the environment variables from the environment when I run the script manuel(which works) but it didnt help.

I have just tried to install the application on another webserver and what do you know, here it works even though the env. variables are the same as on the other server(the original variables).

So it must be something else - I don't have a clue, since I know almost nothing about unix servers, but I guess it must be with the process of sending emails.

Is it possible to see a log somewhere and maybe spot why emails are not sent?

Any other ideas?