如何将PHP脚本包含为电子邮件正文

时间:2012-07-18 16:17:21

标签: php html email joomla include

我有一个php脚本,它会生成一些MySQL查询,只是将一些HTML(带有动态数据)写入页面。我想要做的是发送一封包含此HTML的电子邮件。可以说我想要插入到电子邮件正文中的HTML代码包含在my_html_script.php中,我还想发送2 $_GET个参数,以使动态内容正确显示。

我在Joomla框架中,因此发送电子邮件的代码如下所示:

$mailer =& JFactory::getMailer();
$sender = $from_email;
$mailer->setSender($sender);

$email = explode(';',$email);
for ($i=0;$i<count($email);$i++){
    $mailer->addRecipient(trim($email[$i]));
}

$body   = "




";


$mailer->setSubject('This is the subject');
$mailer->setBody($body);

// Optional file attached
//$mailer->addAttachment();

$send =& $mailer->Send();
if ( $send !== true ) {
    //die($send->message);
    echo "<p>email FAILED".$recip."</p>";
} else {
    //mail sent
    echo "Emailed successfully.";
}

所以,基本上我需要将my_html_script.php的HTML输出包含到$body字符串变量中。我该怎么做?

谢谢!

4 个答案:

答案 0 :(得分:5)

您只需在此块之前声明变量并使用:

ob_start();
include ('my_html_script.php');
$body = ob_get_clean();

答案 1 :(得分:3)

你可以这样做:

$body = file_get_contents("http://example.com/my_html_script.php?getvar=foo&othervar=bar");

答案 2 :(得分:0)

我认为你需要这个,但是......

$body = file_get_contents("path_to_file.php?param1=content&param2=content");

您可以使用params发送http请求:param1,param2或任何其他。

答案 3 :(得分:0)

$filename_xml="E-factuur ".$factuurnummer.".xml";
ob_start();
$_GET['knr']=$klantnummer;
$_GET['bnr']=$bestelnummer;
$_GET['key']=$_SESSION['user_id'];
include("basisUBL_xml.php");//inside the above $_GET['..'] are parsed 
$content_xml = ob_get_clean();
$content_xml = chunk_split(base64_encode($content_xml));
$name_xml = basename($filename_xml);
$uid="=_Part_2408_".md5(uniqid(time()));
$logopad_emails="https://www.yourdomainhere.nl/images/logo.png";
$headerx="MIME-Version: 1.0\r\n";
$headerx.="From: webshop www.yourdomainhere.nl <info@yourdomainhere.nl>\r\n";
$headerx.="Reply-To: info@yourdomainhere.nl \r\n";
$headerx.="Errors-To: info@yourdomainhere.nl \r\n";
$headerx.="X-Mailer: yourdomainhere Mailscript \r\n";
$headerx.="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// message & attachment
$messagex = "--".$uid."\r\n";
$messagex .= "Content-type:text/html; charset=iso-8859-1\r\n\r\n"; //2x \r\n important for OXS email and iphone!!!
$messagex .= $htmlbody3."\r\n\r\n";
$messagex .= "--".$uid."\r\n";
$messagex .= "Content-Type: application/octet-stream; name=\"".$name_pdf."\"\r\n";
$messagex .= "Content-Transfer-Encoding: base64\r\n";
$messagex .= "Content-Disposition: attachment; filename=\"".$name_pdf."\"\r\n\r\n";
$messagex .= $content_pdf."\r\n\r\n";
$messagex .= "--".$uid."\r\n";
$messagex .= "Content-Type: application/octet-stream; name=\"".$name_xml."\"\r\n";
$messagex .= "Content-Transfer-Encoding: base64\r\n";
$messagex .= "Content-Disposition: attachment; filename=\"".$name_xml."\"\r\n\r\n";
$messagex .= $content_xml."\r\n\r\n";
$messagex .= "--".$uid."--";
$mailtox='info@yourdomainhere.nl';
$subjectx='UBL test'.$factuurnummer;