Я пытаюсь отправить электронное письмо с 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 не работает …
Кто-нибудь, чтобы помочь мне, пожалуйста?
Благодарю.
Если вы просто пытаетесь отправить электронное письмо, почему вы используете потоковые контексты и $ _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, вы все равно можете это сделать, используя это решение.
Я создал класс для отправки электронной почты. Пожалуйста, дайте мне знать, если это вам полезно
<?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 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 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 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 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 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(); ?>