how to include php html file in PEAR mail

╄→гoц情女王★ 提交于 2020-01-05 03:46:11

问题


I have a php file that I use to send newsletters. 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. But it's still not working. I get the TXT version not the HTML version.

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.

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.'');

回答1:


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. 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)).

The name of the function implies that it should make this part of the 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. 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). 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). Long story straight here: the client's user agent (browser, email application, etc) should be handling the compression, not you.

PHP ob stuff is a bit convoluted. 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. 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. 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).

So I think your ob code is messing up the parser 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. https://www.mail-tester.com/ helped me address some issues related to email (it's not related to this issue).



来源:https://stackoverflow.com/questions/58309553/how-to-include-php-html-file-in-pear-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!