Premailer + PHP + login shell @ DreamHost shared




I’ve created a website that send information to users by e-mail (SMTP)
so, to more acceptable, I made a beatifull HTML template. Who worked with newsletter or e-mail marketing
knows that the mail clients (gmail, hotmail, yahoo, outlook, thunderbird, etc) has a lot of restrictions
width non inline css and this is easly solved by Premailer lib (Ruby) (

There is a install version of this lib that I already installed:

	:~$ gem install addressable -v 2.2.7
	:~$ gem install premailer -v 1.7.1

Running this lib via SSH (using shell as a login shell), is working as expected.

so I made a little PHP script to run the command:

<?php $body = base64_encode(file_get_contents('mail.html')); $output = array(); $returnVar = 0; $command = "echo $body | base64 -d | premailer "; $response = exec($command, $output, $returnVar); if ( $returnVar === 1 ) { throw new Exception('Algo deu errado'); } foreach ( $output as $line ) { $line = trim($line); echo $line ? $line . PHP_EOL : ''; } ?>

and than run php premailer.php via PHP CLI and works 100%
so running the same script via browser (here), nothing works.

I tried to login via su <username> and the script is not working, but login in like su -l <username>
my script runs as well,

I guess that is related width “shell as login shell”

There is a way to PHP run a command as login shell?


exec() and shell_exec() are two ways to invoke the command line from within a PHP script


Yes, I know it, but both doesn’t work becaus they don’t use shell as “login shell” so they don’t “see” my local environment variables that I created to use via ssh



The problem was related shell and user environment. PHP-CGI doesn’t load the same user PATH and environment variables that is loaded when user login via ssh,
so I exported 3 basic vars to “build” the same environment used on ssh logins

to understand the differences between both environment (browser and ssh) just do:

[php]<?php print(shell_exec('printenv')); ?>[/php]

My php script now:

<?php ini_set('display_errors', TRUE); error_reporting(E_ALL | E_STRICT | E_WARNING); $body = base64_encode(file_get_contents('mail.html')); $output = array(); $returnVar = 0; $command = implode(' ', array( 'export PATH=/home/mobiliscar/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/local/games:/usr/games;', // HERE IS MY SOLUTION 'export GET_PATH=/home/mobiliscar/.gems:/usr/lib/ruby/gems/1.8;', // HERE IS MY SOLUTION 'export GEM_HOME=/home/mobiliscar/.gems;', // HERE IS MY SOLUTION "echo $body", "| base64 -d", "| premailer", )); $response = exec($command, $output, $returnVar); foreach ( $output as $line ) { $line = trim($line); echo $line ? $line . PHP_EOL : ''; } ?>


About different between login shell and non-login shell
Link 1
Link 2

I hope to help

if I made mistakes, please fell free to correct me. :slight_smile: