【问题标题】:Send an email in PHP - Blank email receive用 PHP 发送电子邮件 - 接收空白电子邮件
【发布时间】:2015-06-28 02:02:08
【问题描述】:

我正在尝试使用 PHP 发送电子邮件。

我的问题其实是,发送的邮件是空白的……

我的 PHP 函数:

function sendMail($template, $Email_Subject, $USR_Id, $USR_Email) {

    $postdata = http_build_query(
        array(
            'subject' => $Email_Subject
        )
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    $message = file_get_contents('../../mail/'.$template.'.php', false, $context);

    // Start configuring the email
    $headers .= 'From: Company <noreply@company.com>' . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    mail($USR_Email, $Email_Subject, $message, $headers); 
}

我的 template.php 页面:

$message = 
'<html>
...
<h1>Subject is : '.$_POST['subject'].'</h1>
...
<\html>';
echo $message;

我这样调用函数:

sendMail("template", "Account Activation", $USR_Id, $USR_Email);

奇怪的是,当我回显$message 时,它没有回显我Subject is : ...。它呼应了我Subject is : '.$_POST['subject'].'。就像如果 PHP 不工作...

有人帮帮我吗?

谢谢。

【问题讨论】:

  • 你在 template.php 中回显$message 吗?好像只有一个变量!!!
  • 是的。我也尝试过返回。
  • echo试试吧...返回不适合这种情况...
  • 我尝试了 echo $message。同样... :(
  • 可能您的文件路径不正确。 ../../你能提供你的文件结构吗?尝试使用此工具帮助您在文本上创建它:filestructuregenerator.com

标签: php email


【解决方案1】:

如果您只是想发送一封电子邮件,为什么要使用流上下文和 $_POST?这应该使用输出缓冲 (http://php.net/manual/en/book.outcontrol.php):

function sendMail($template, $Email_Subject, $USR_Id, $USR_Email) {

    // everything output between ob_start and ob_end_clean will be stored
    // in a temporary buffer, instead of being send the browser
    ob_start();
    require('../../mail/'.$template.'.php');
    $message = ob_get_clean();

    // Start configuring the email
    $headers  = 'From: Company <noreply@email.com>' . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    mail($USR_Email, $Email_Subject, $message, $headers); 
}

在您的模板中,您可以使用 sendMail 函数中可用的所有变量,因此请使用 $Email_Subject 而不是 $_POST。显然,如果您想要打印 $_POST 中的任何内容,您仍然可以使用此解决方案来执行此操作。

【讨论】:

    【解决方案2】:

    我创建了一个发送电子邮件的类。如果对你有用,请告诉我

        <?php
    class scSendMail
    {
        protected $from;
        protected $toList;
        protected $replyTo;
        protected $subject;
        protected $message;
        public function __construct()
        {   
            register_shutdown_function(array($this,'__destruct'));
            $this->setFrom("updates@planetonnet.com");
            $this->setReplyTo("noreply@planetonnet.com");
            $this->setSubject("Update from PlanetOnNet.com");
        }
    
    
    
        public function __destruct()
        {
            unset($this->from);
            unset($this->toList);
            unset($this->replyTo);
            unset($this->subject);
            unset($this->message);
        }
    
    
        public function sendMail()
        {
            $return =NULL;
            $headers ='From: '.$this->getFrom()."\r\n" .
                      'Reply-To: '.$this->getReplyTo(). "\r\n" .
                      'MIME-Version: 1.0' . "\r\n".
                      'Content-type: text/html; charset=iso-8859-1' . "\r\n".
                      'X-Mailer: PHP/' . phpversion();
            foreach($this->toList as $to)
            {
                if(mail($to, $this->getSubject(), $this->getMessage(), $headers)) 
                {
                    $return.='<br>mail sent to: '. $to;
                }
                else 
                {
                    $return.='<br>mail couldnt sent to: '. $to;
                }
            }
            return $return;
        }
    
        public function setFrom ($tmpFrom )
        {
            $this->from = $tmpFrom ;
        }
        public function getFrom ()
        {
            return $this->from ;
        }
    
        public function addInToList ($tmpTo )
        {
            $this->toList[] = $tmpTo ;
        }
    
        public function setReplyTo ($tmpReplyTo )
        {
            $this->replyTo = $tmpReplyTo ;
        }
        public function getReplyTo ()
        {
            return $this->replyTo  ;
        }
    
        public function setSubject ($tmpSubject )
        {
            $this->subject= $tmpSubject ;
        }
        public function getSubject ()
        {
            return $this->subject ;
        }
    
        public function setMessage ($tmpMessage )
        {
            $tmpMessage.='<br>You are getting this message on behalf of 
                            <a href="http://planetonnet.com/">planetonnet.com</a><br>
                          login to your account area for more ';
            $this->message = stripslashes($tmpMessage) ;
        }
        public function getMessage ()
        {
            return  $this->message ;
        }
    
    
    }
    ?>
    

    而且在使用的时候,按照下面的方式使用就行了

    <?php
    include_once("scSendMail.php");
    $test1=new scSendMail();
    $test1->addInToList("abc@example.com");
    $test1->addInToList("abc@anotherexample.com");
    $test1->setSubject("Hi! This is test email");
    echo $test1->sendMail();
    ?>
    

    【讨论】: