简体   繁体   English

如何在 PEAR 邮件中包含 php html 文件

[英]how to include php html file in PEAR mail

I have a php file that I use to send newsletters.我有一个 php 文件,用于发送时事通讯。 Recently I moved to a new server and they use PEAR Mail instead of the default PHP mail to send mails and I had to update my script to function.最近我搬到了一个新服务器,他们使用 PEAR Mail 而不是默认的 PHP 邮件来发送邮件,我不得不将我的脚本更新为 function。 But it's still not working.但它仍然无法正常工作。 I get the TXT version not the HTML version.我得到的是 TXT 版本而不是 HTML 版本。

If I manually enter the html codes inside the setHTMLBody() it works but when I replace it with my ob_start $output_string variable it doesn't work.如果我在 setHTMLBody() 中手动输入 html 代码,它可以工作,但是当我用我的 ob_start $output_string变量替换它时它不起作用。

Here is my script;这是我的脚本;

ob_start();
include "URL/To/File.php";
$output_string = ob_get_contents();
ob_end_clean();

$headers['From'] = 'from@email.com';
$headers['Subject'] = 'Newsletter Subject';

require_once('Mail.php');
require_once('Mail/mime.php');

$message = new Mail_mime();
$message->setTXTBody("Your client doesn't support HTML.");

$message->setHTMLBody(''.$output_string.'');

$mail =& Mail::factory('mail');

$result = $mail->send('myemailaddress@gmail.com', $message->headers($headers), $message->get());

if (PEAR::isError($result)) {
echo("<span>" . $result->getMessage() . "</span>");
} else {
echo("<span style='color: #f7941c; font-weight: bold'>Congratulations! 
Your mail has been sent successfully</span>");
}

how do I correctly input the line below correctly?如何正确输入下面的行? It's not working as is right now.它现在无法正常工作。

$message->setHTMLBody(''.$output_string.'');

So I'm cold on this subject right now (working on mobile) though let's see if I can help you out.所以我现在对这个主题很冷(在移动设备上工作),但让我们看看我是否可以帮助你。 So I looked up the setHTMLBody function .所以我查找了setHTMLBody function It's a little fuzzy on the type that the expected parameters should be.预期参数应该是什么类型有点模糊。 In PHP you can get the type using gettype($example) (like console.log(typeof example); in JavaScript though PHP is generally more forgiving about types (calculating a number that has a string type will work in PHP, not JavaScript)). In PHP you can get the type using gettype($example) (like console.log(typeof example); in JavaScript though PHP is generally more forgiving about types (calculating a number that has a string type will work in PHP, not JavaScript) )。

The name of the function implies that it should make this part of the email HTML. function 的名称暗示它应该使这部分 email HTML。 Now of all the modules I've built on my web platform email has been the most challenging not because it's inherently complex though because it's very subjective.现在,我在 web 平台上构建的所有模块中,email 是最具挑战性的,不是因为它本身就很复杂,而是因为它非常主观。 In example some servers might expect you to serve an <html> element, others a <body> element and others won't care if you omit it (and I'm not sure what if any specifications declare what is "proper" here).例如,某些服务器可能希望您提供<html>元素,其他服务器可能希望您提供 <body <body>元素,而其他服务器则不在乎您是否省略它(而且我不确定是否有任何规范在这里声明什么是“正确的”) . I've not intentionally worked with compressing data in emails (just output in web mail though it's technical context is lost at that point).我没有故意压缩电子邮件中的数据(只是 web 邮件中的 output 虽然它的技术背景在那时丢失了)。 Long story straight here: the client's user agent (browser, email application, etc) should be handling the compression, not you.长话短说:客户端的用户代理(浏览器、email 应用程序等)应该处理压缩,而不是你。

PHP ob stuff is a bit convoluted. PHP ob 的东西有点复杂。 I dislike the same function/method being used for both compression and being able to capture and do find/replace with the output before sending it to a client.我不喜欢在将 output 发送给客户端之前将其用于压缩和能够捕获并查找/替换 Z78E6221F6393D14CE6DZ 的相同功能/方法。 I think you're using it for compression though you could also be using it to replace bits of code for whatever reason.认为您正在使用它进行压缩,尽管您也可以出于任何原因使用它来替换代码位。 In this case your best bet for troubleshooting (presuming that your ob should work, most likely for replacing bits of code) is to use the string and test it outside of this environment.在这种情况下,您最好的故障排除方法(假设您的 ob 应该可以工作,最有可能用于替换代码位)是使用该字符串并在此环境之外对其进行测试。 When I test cron jobs I always test them in normal environments first (though keep in mind cron jobs run in a much more limited environment so for debugging there I just have print_r($_SERVER) send me information via email).当我测试 cron 作业时,我总是首先在正常环境中测试它们(但请记住,cron 作业在更有限的环境中运行,因此为了在那里进行调试,我只需print_r($_SERVER)通过电子邮件向我发送信息)。

So I think your ob code is messing up the parser setHTMLBody() function.所以我认为你的 ob 代码弄乱了解析器setHTMLBody() function。 Break your code down until you have working bits and then add your necessary and increasingly complex bits to it until you hit a problem and then because you know exactly what you just added you'll be able to single out the issue much easier.分解你的代码,直到你有工作位,然后添加你必要的和越来越复杂的位,直到你遇到问题,然后因为你确切地知道你刚刚添加的内容,你将能够更容易地找出问题。

I'd need further clarification though I can edit this answer later.尽管我可以稍后编辑此答案,但我需要进一步澄清。 Let me know where you're at, I always check notifications even if it takes a day.让我知道你在哪里,即使需要一天,我也会经常查看通知。

I have a few dozen tools that I use when I develop.我在开发时使用了几十种工具。 I'm not sure if this tool will validate though it may help you somehow since you are working on email.我不确定这个工具是否会验证,尽管它可能会以某种方式帮助你,因为你正在研究 email。 https://www.mail-tester.com/ helped me address some issues related to email (it's not related to this issue). https://www.mail-tester.com/帮助我解决了一些与 email 相关的问题(与此问题无关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM