Solved

php doesn't work with linux sendmail utility

Posted on 2014-10-12
8
100 Views
Last Modified: 2014-10-20
echo '<pre>';

system("/usr/bin/sendmail -t -i mymail@mydomail.com < /tmp/email.txt", $ret);

// Printing additional info
echo '
</pre>
<hr />Last line of the output: '
?>

Open in new window


Why this doesn't work on php?
0
Comment
Question by:Nusrat Nuriyev
  • 4
  • 3
8 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40376212
If it's shared hosting, you won't have permission to run 'system' commands.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40376394
On my Ubuntu 10.04 system, 'sendmail' is in /usr/sbin/sendmail (note the 'sbin').  This works for me.  I commented out your line.

<?php
echo '<pre>';

//system("/usr/bin/sendmail -t -i mymail@mydomail.com < /tmp/email.txt", $ret);
system("/usr/sbin/sendmail -t -i < emailtext.txt", $ret);
// Printing additional info
echo "$ret
</pre>
<hr />Last line of the output: ";
?>

Open in new window

0
 

Author Comment

by:Nusrat Nuriyev
ID: 40376485
Well site hosting in my server.
This does not work as well.

system("/usr/sbin/sendmail -t -i nnuser@gmail.com < /tmp/email.txt", $ret);
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40376489
Hey, do I need to format it in some way?
I mean does email have special format?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 40376515
Yes it does.  I looked at the source for a small email and copied it for my text file.  You may need to use a valid local email address to send from.  This is about the smallest set of headers you can get away with.  Less and your email might be considered spam.
From: Me <me@myplace.com>
MIME-Version: 1.0
To: You <you@yourplace.com>
Subject: Sendmail email text test
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Dave Baldwin said:
Note the blank line after the headers.

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40376792
By the way, what you are doing is almost exactly what the PHP mail() function does.  It takes the information, formats it, and passes it on to 'sendmail' just like you are doing.  http://us3.php.net/manual/en/function.mail.php
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40376802
we can do it with php mail() function, but it does not work as a `sendmail` system() call.
However system call for `ls` works fine.
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 40377117
1. If you can do an "ls" via a system/shell call, but not another command, then it is likely a permissions issue. In the most common setup with Apache and PHP, your PHP script will run as the same user as Apache, and usually Apache is locked down for security reasons and uses the nearly-permisionless users like "nobody".

2. If you want to allow Apache/PHP to execute sendmail (or any other specific application), usually the best way is through sudo (which temporarily elevates permissions in Linux in a specific situation), or through another permissions-changing module like suPHP. There are many, but most of them are a little tough to set up if you're not used to sysadmin work.

3. Usually if you want the PHP mail() command to make use of the sendmail binary, you need to uncomment and set the value of sendmail_path in your php.ini file and restart the web server.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now