Search code examples
phpnginxsendmailrhel7

Sending mail via sendmail command line from PHP


sendmail -s me@example.com
Subject:Salem
This is body of email
Ctrl+D

Shell script above works fine under RHEL-7 .

We need now to wrap this command line ( sendmail ) with php to get something like :

  <?php 
       sendmail('from@example.com',"this is title","blablalb","to@example.com") 
   ?>

How to ? Is there PHP library should be installed under RHEL to be able to send email by PHP relaying on sendmail commmand line ?


Known that the same question has been posted HOWEVER , in the context of Python programming language , and this is the best answer until now :

def sendMail():
    sendmail_location = "/usr/sbin/sendmail" # sendmail location
    p = os.popen("%s -t" % sendmail_location, "w")
    p.write("From: %s\n" % "from@somewhere.com")
    p.write("To: %s\n" % "to@somewhereelse.com")
    p.write("Subject: thesubject\n")
    p.write("\n") # blank line separating headers from body
    p.write("body of the mail")
    status = p.close()
    if status != 0:
           print "Sendmail exit status", status

Solution

  • You can use system

    $command = '/usr/sbin/sendmail -t -f sender@example.com < /email.txt';
    system($command);