发送电子邮件PHP - 空白电子邮件接收 [英] Send an email in PHP - Blank email receive

查看:182
本文介绍了发送电子邮件PHP - 空白电子邮件接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send an email with PHP.

我的问题其实是发送的电子邮件空白...

My problem is actually, the email sent is blank...

我的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页面:

My template.php page:

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

我打电话给这样的功能:

I call the function like this:

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

当我回复 $ message ,它不回应我主题是:... 。它回应我主题是:'$ _ POST ['subject']。'。喜欢PHP不工作...

What is strange is when I echo the $message, it do not echo me Subject is : .... It echoes me Subject is : '.$_POST['subject'].'. Like if PHP dont't work...

有人帮我吗?

谢谢。 p>

Thanks.

推荐答案

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

If you're just trying to send an email, why are you using stream-contexts and $_POST? This should be done using output buffering (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中有任何内容,您仍然可以使用此解决方案。

In your template you can use all variables that are available in the sendMail function, so use $Email_Subject instead of $_POST. Obviously, if there is anything in $_POST you'd like to print you can still do that using this solution.

这篇关于发送电子邮件PHP - 空白电子邮件接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆