我正在尝试使用 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 - 用 PHP 发送电子邮件 - 接收空白电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095436/