0

I am trying to make a php script that uses the exec() function to run a command that sends an email.

I am looking at something like this:

<?php
$sendTo = 'RECEPIENT';
$subject = "SUBJECT";
$message = "MESSAGE";

exec('/bin/mail -s "$sendTo" "$sendTo" < $message');
?>

I am not sure however if the variables I have declared in php can be used in the exec() function. The command however also does not seem to be correct.

4
  • 2
    any reason you don't use php's built in mail functions? Commented May 22, 2013 at 19:31
  • Yes, I have issues with the mail(), it executes for almost 1 minute and that could not be resolved so far. Commented May 22, 2013 at 19:47
  • well generally speaking php's mail function does the exact same thing that you are trying to do (depending upon how it is configured, it is probably piping to sendmail). So if it is having issues, running mail from the CLI , probably be the same. Also it is normally easier to use popen, to get a pipe to the mail or sendmail program and then just echo or print the contents of the message into it, since unless $message is a file. your can't shell redirect Commented May 22, 2013 at 19:52
  • Why not use swiftmailer.org? Commented May 22, 2013 at 20:01

2 Answers 2

1

When enclosing strings in single-quotes ('), the variables are not expanded.
You can try something like this:

exec('/bin/mail -s "' . $sendTo . '" "' . $sendTo . '" < ' . $message);

Take a look at the PHP Manual for more details.

Sign up to request clarification or add additional context in comments.

7 Comments

escapeshellarg() should do it too.
I don't really see how escapeshellarg() good be useful here. Could you give an example ?
It'd escape the ' in the variables too for example.
I get the following when using the exec() that you suggested: sh: MESSAGE: No such file or directory
@Jason Carter: Have you tried executing the command directly (i.e. from the shell, without PHP) ? It could be a problem with your command itself and not the way you issue it from within PHP.
|
1

The < is a shell redirect, and is expecting a filename .

you can do something like this, (although I think there are other issues causing mail to be slow)

<?php
   $mail_command = "/bin/mail -s \"$subject\" $sendTo";
   $fd = popen($mail_command, 'w');
   fputs($fd,$message);
   pclose($fd);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.