通过sendmail命令行从PHP发送邮件

时间:2016-05-26 14:29:21

标签: php nginx sendmail rhel7

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

RHEL-7下的上面的Shell脚本正常工作

我们现在需要用php包装这个命令行(sendmail)来得到类似的东西:

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

怎么样? 是否应该在RHEL下安装PHP库才能通过sendmail命令行上的PHP中继发送电子邮件?

我知道在Python编程语言的上下文中已经发布了同样的问题,直到现在这是the best answer

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

1 个答案:

答案 0 :(得分:0)

您可以使用系统

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