php - 用 PHP 发送电子邮件 - 接收空白电子邮件

标签 php email

我正在尝试使用 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/

相关文章:

php - 单选按钮无法选择

spring - Thymeleaf Spring Mail 上下文错误

ruby-on-rails - ruby rails : Why doesn't my embedded image display in Gmail or Office 365?

java - 如何从FTP服务器获取文件并转换为附件文件?

php - 发送带附件的电子邮件之前未保存到硬盘驱动器

php - 哪种方法更适合搜索引擎优化 (SEO)?

php - css 样式表位置 ubuntu linux

php - 自定义滚动条随选项卡消失

php - 使用 DBO 连接插入或更新具有 json 值的 MySQL 表失败

wordpress - 如何在 Wordpress 电子邮件的回复标题中更改电子邮件地址