Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子邮件模板中未正确显示url_Php_Html - Fatal编程技术网

Php 电子邮件模板中未正确显示url

Php 电子邮件模板中未正确显示url,php,html,Php,Html,我已经进行了以下设置,$url取自 通过旋度进行ping,旋度很好,加载时间长,但速度快。正在ping的160个站点 <?php $url = $row['url']; $ch = curl_init($url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); $retcode =

我已经进行了以下设置,$url取自

通过旋度进行ping,旋度很好,加载时间长,但速度快。正在ping的160个站点

<?php
    $url = $row['url'];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if (200==$retcode) {
        echo "<td><span class='badge badge-success'>LIVE</span></td>";
    } else {
        echo "<td><span class='badge badge-danger'>DOWN</span></td>";
        $path_to_file = './emailtemplate.html';
        $file_contents = file_get_contents($path_to_file);
        $file_contents = str_replace("depplaceholder","$url",$file_contents);
        file_put_contents($path_to_file,$file_contents);

        $to = "--";
        $subject = "$url down";
        $headers = "From:Deployment Monitor <-->" . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $message = file_get_contents('./emailtemplate.html');

        mail($to,$subject,$message,$headers);
    }
?>
当ping返回时,会自动发送一封电子邮件,如上面的代码所示,这部分起作用,如图所示,emailtemplate.html中的单词depplaceholder需要替换为$url,因为第一个网站已关闭

i、 e.电子邮件:

电子邮件1:标题:服务器1关闭 正文:服务器1关闭

电子邮件2:标题:服务器2关闭 正文:服务器1关闭

电子邮件3:标题:服务器3关闭 正文:服务器1关闭


由于某种原因,正文没有像标题那样改变,这是因为标题是从同一页而不是从emailtemplate.html中提取的。好的,因为评论太少了,我正在写一个完整的答案

假设,正如您在评论中所说,您的模板文件如下所示:

<body> <h1> depplaceholder is down </h1> </body>
然后用

此时,您将发送内容为emailtemplate.html的电子邮件

然后ping另一台服务器并加载模板,此时,由于您重写了模板包含的内容

<body> <h1> 1 is down </h1> </body>
但是在你的文件里已经没有占位符了!所以这条线实际上什么都没做!最终你的邮件中包含

<body> <h1> 1 is down </h1> </body>
并更改将消息设置为的部分

$message = $file_contents;

问题解决了

那么160次ping,所有ping都写入同一个文件?@FedericoklezCulloca当服务器启动时,页面显示为LIVE,什么都没有发生,当服务器关闭时,电子邮件被发送出去,这不是问题,这是有效的,问题是如果5台服务器关闭,我收到的邮件会说服务器1,2,3,4,5关闭,但邮件正文会说服务器1,1,1,1,1关闭。你能显示你的模板文件吗?另外,当您再次运行此程序并重写模板时会发生什么情况?@FedericoklezCulloca当前模板非常基本,主体是deppocholder,因此该deppocholder将服务器1的值返回到160,但也就是说,如果服务器35和服务器68都处于脱机状态,它将拾取第一个显示为down的服务器。@SergiuParaschiv所有160个URL都是从数据库中提取的,这一切正常,它们显示正确,ping正确,但电子邮件模板仅拾取第一个脱机部署,其余部分将保留在模板正文中,但标题显示正确。我理解你的意思,因为对于电子邮件1,depplaceholder被$url替换,所以在生成电子邮件2时,depplaceholder不再存在,因此再次使用电子邮件1中的$url。@Jeffrey我还添加了一个解决方案,如果您需要的话。
<body> <h1> 1 is down </h1> </body>
$file_contents = str_replace("depplaceholder", $url, $file_contents);
<body> <h1> 1 is down </h1> </body>
file_put_contents($path_to_file, $file_contents);
$message = $file_contents;