2015-06-28 289 views
4

我想發送一封電子郵件與PHP。發送電子郵件在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 <[email protected]>' . "\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不工作...

任何人都可以幫我嗎?

謝謝。

+0

你在的template.php回聲'$ message'?這似乎只是一個變量! – Kiyarash

+0

是的。我也試着回報。 – teamo

+0

用'echo'試一下...返回不適合這種情況... – Kiyarash

回答

0

如果你只是想發送電子郵件,爲什麼你使用流上下文和$ _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 <[email protected]>' . "\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中有任何內容,您仍然可以使用此解決方案執行此操作。

0

我創建了一個類來發送電子郵件。請讓我知道,如果它是你

<?php 
class scSendMail 
{ 
    protected $from; 
    protected $toList; 
    protected $replyTo; 
    protected $subject; 
    protected $message; 
    public function __construct() 
    { 
     register_shutdown_function(array($this,'__destruct')); 
     $this->setFrom("[email protected]"); 
     $this->setReplyTo("[email protected]"); 
     $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("[email protected]"); 
$test1->addInToList("[email protected]"); 
$test1->setSubject("Hi! This is test email"); 
echo $test1->sendMail(); 
?>