0

Hello guys, I'm trying to make my PHP mailing script mail an html webpage. I've pasted the page code but I've been getting alot of error pages complaining about the tags in the html code (style, table width, etc) How do I get around this?

3
Contributors
15
Replies
16
Views
8 Years
Discussion Span
Last Post by Atli
Featured Replies
  • You've indented the closing [B]MAILHTML;[/B] identifier. You need to leave this as I showed in the code. No indents allowed. Also, your mail function has an extra comma in it [CODE]if(mail($to,$subject,$message,, $headers, "From: $sendermail\n"))[/CODE] Remove that as well. Problem solved. Also, I suggest that you move all that HTML to … Read More

0

Hey.

Could you please post the code you are using, and the error messages?

It helps with the debugging process, actually seeing what you are debugging :-]

0

Hey.

Could you please post the code you are using, and the error messages?

It helps with the debugging process, actually seeing what you are debugging :-]

The code's quite alot (cuz of the html file I added)

Well the first error went like this:

Parse error: syntax error, unexpected T_STRING in /websites/LinuxPackage02/fe/es/li/feeslimited.net/public_html/newsletters/crystal_training/quickmailer.php on line 96

It referred to this line of code:

<style type="text/css">

I deleted that line from the code (yeah, not very bright) then got this error on the next run:

Parse error: syntax error, unexpected T_LNUMBER in /websites/LinuxPackage02/fe/es/li/feeslimited.net/public_html/newsletters/crystal_training/quickmailer.php on line 146

that refered to this line:

<table width="100%" cellpadding="10" cellspacing="0" bgcolor='#99CC00' >

I have a feeling it'll keep giving me errors like this and mess up the whole code. What am I doing wrong? :-/

I'm not sure you want to see the whole thing, but here's a peice chunk of the included html part of the code:

$message ="
<html>
<style type="text/css">
<!--
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #996600;
}
.style12 {
	color: #333333;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
.style13 {font-size: 20px; font-family: Arial, Helvetica, sans-serif; color: #006699;}
.style15 {color: #006699; font-size: 14px; }
.style16 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style17 {font-size: 10px}
.style18 {font-size: 11px}
.style19 {
	color: #996600;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 11px;
}
.style20 {font-family: Arial, Helvetica, sans-serif}
.style23 {font-family: Arial, Helvetica, sans-serif; font-size: 11px; }
.style25 {color: #006699}
.style26 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #006699; }
.style27 {color: #1B61A6}
.style28 {font-size: 14px}
-->
</style>
<title>Crystal Reports Newsletter</title><body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor='#99CC00' >

<STYLE>
 .headerTop { background-color:#FFCC66; border-top:0px solid #000000; border-bottom:1px solid #FFFFFF; text-align:center; }
 .adminText { font-size:10px; color:#996600; line-height:200%; font-family:verdana; text-decoration:none; }
 .headerBar { background-color:#FFFFFF; border-top:0px solid #333333; border-bottom:10px solid #FFFFFF; }
 .title { font-size:20px; font-weight:bold; color:#CC6600; font-family:arial; line-height:150%; }
 .subTitle { font-size:11px; font-weight:normal; color:#666666; font-style:italic; font-family:arial; }
 td { font-size:12px; color:#000000; line-height:150%; font-family:trebuchet ms; }
 .sideColumn { background-color:#FFFFFF; border-left:1px dashed #CCCCCC; text-align:left; }
 .sideColumnText { font-size:11px; font-weight:normal; color:#999999; font-family:arial; line-height:150%; }
 .sideColumnTitle { font-size:15px; font-weight:bold; color:#333333; font-family:arial; line-height:150%; }
 .footerRow { background-color:#FFFFCC; border-top:10px solid #FFFFFF; }
 .footerText { font-size:10px; color:#996600; line-height:150%; font-family:verdana; }
 a { color:#FF6600; color:#FF6600; color:#FF6600; }
</STYLE>



<table width="100%" cellpadding="10" cellspacing="0" bgcolor='#99CC00' >
<tr>
<td valign="top" align="center">

<table border="0" cellpadding="0" cellspacing="0">
<tr>

It goes on like that.
Hope there's a neat solution to this. Let me know if you want to see the whole thing okay?

0

Ouch. You're trying to assign a whole bunch of uncleared HTML into that variable. Since you've wrapped it with double quotes and the HTML itself has double quotes you'll keep getting errors. A better way to do it would be to use heredocs.

Change your code to this,

$message  = <<<MAILHTML
<html>
<style type="text/css">
<!--
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
rest of your code goes here
MAILHTML;

Note when using heredocs, remember to use the same identifier to close (in this case MAILHTML, but you can use anything you want) the string. And don't put anything apart from a semicolon after the closing identifier (MAILHTML). Also remember to have a newline immediately after the opening identifier (<<<MAILHTML).

0

Hi jomanlk. Thanks, seems the error's been fixed but I'm having an new error coming up:

Parse error: syntax error, unexpected $end in /websites/LinuxPackage02/fe/es/li/feeslimited.net/public_html/newsletters/crystal_training/quickmailer.php on line 303

And it's refering to this line:

</html>

Maybe I should include the whole code for you to peruse?

Here it is attached anyways.

Attachments
<? 
$sendername=$_POST['sendername'];
$sendermail=$_POST['sendermail'];
$salutation=$_POST['salutation'];
$presalutation=$_POST['presalutation'];
$recipientname=$_POST['recipientname'];
$recipientemail=$_POST['recipientemail'];
$recipientcompanyname=$_POST['recipientcompanyname'];
$recipientaddress=$_POST['recipientaddress'];
$sendercomment=$_POST['sendercomment'];
$formname=$_POST['formname'];
$subject="Good day $presalutation $salutation $recipientname. You should see this.";
$recipientform=$_POST['recipientform'];

$to="$recipientemail";
$to2="o.oloyede@feeslimited.net";
$subject2=$_POST['recipientfinderreport'];

$headers = 'From: info@feeslimited.net' . "\r\n" .
        'Reply-To: info@feeslimited.net' . "\r\n" .
        "X-Mailer: PHP/" . phpversion()."\r\n";
 
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
$message = <<<MAILHTML
		
		<html>
<style type="text/css">
<!--
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #996600;
}
.style12 {
	color: #333333;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
.style13 {font-size: 20px; font-family: Arial, Helvetica, sans-serif; color: #006699;}
.style15 {color: #006699; font-size: 14px; }
.style16 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style17 {font-size: 10px}
.style18 {font-size: 11px}
.style19 {
	color: #996600;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 11px;
}
.style20 {font-family: Arial, Helvetica, sans-serif}
.style23 {font-family: Arial, Helvetica, sans-serif; font-size: 11px; }
.style25 {color: #006699}
.style26 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #006699; }
.style27 {color: #1B61A6}
.style28 {font-size: 14px}
-->
</style>
<title>Crystal Reports Newsletter</title><body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor='#99CC00' >

<STYLE>
 .headerTop { background-color:#FFCC66; border-top:0px solid #000000; border-bottom:1px solid #FFFFFF; text-align:center; }
 .adminText { font-size:10px; color:#996600; line-height:200%; font-family:verdana; text-decoration:none; }
 .headerBar { background-color:#FFFFFF; border-top:0px solid #333333; border-bottom:10px solid #FFFFFF; }
 .title { font-size:20px; font-weight:bold; color:#CC6600; font-family:arial; line-height:150%; }
 .subTitle { font-size:11px; font-weight:normal; color:#666666; font-style:italic; font-family:arial; }
 td { font-size:12px; color:#000000; line-height:150%; font-family:trebuchet ms; }
 .sideColumn { background-color:#FFFFFF; border-left:1px dashed #CCCCCC; text-align:left; }
 .sideColumnText { font-size:11px; font-weight:normal; color:#999999; font-family:arial; line-height:150%; }
 .sideColumnTitle { font-size:15px; font-weight:bold; color:#333333; font-family:arial; line-height:150%; }
 .footerRow { background-color:#FFFFCC; border-top:10px solid #FFFFFF; }
 .footerText { font-size:10px; color:#996600; line-height:150%; font-family:verdana; }
 a { color:#FF6600; color:#FF6600; color:#FF6600; }
</STYLE>



<table width="100%" cellpadding="10" cellspacing="0" bgcolor='#99CC00' >
<tr>
<td valign="top" align="center">

<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="49" align="center" style="background-color:#FFCC66;border-top:0px solid #000000;border-bottom:1px solid #FFFFFF;text-align:center;"><p style="font-size:10px;color:#996600;line-height:200%;font-family:verdana;text-decoration:none;">Email not displaying correctly? <a href="http://www.feeslimited.net/newsletters/crystal_training/crystal_newsletter.htm" target="_blank" style="font-size:10px;color:#996600;line-height:200%;font-family:verdana;text-decoration:none;">View it in your browser.</a> <br>
  <a href="http://www.feeslimited.net/newsletters/crystal_training/friendforwarder.htm" target="_blank" style="font-size:10px;color:#996600;line-height:200%;font-family:verdana;text-decoration:none;">Please Tell a Friend!</a></p>
  </td>

</tr>

<tr>
<td align="left" valign="middle" style="background-color:#FFFFFF;border-top:0px solid #333333;border-bottom:10px solid #FFFFFF;"><center><a href=""><IMG SRC="http://www.feeslimited.net/newsletters/crystal_training/img/logo_2column.jpg"  alt="Your Company" name="editableImg1" BORDER="0" align="center" id=editableImg1 title="Your Company"></a>
</center></td>
</tr>


</table>

<table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
  <tr>
    <td align="center" bgcolor="#FFFFFF"><p style="font-size:12px;color:#006699;line-height:150%;font-family:trebuchet ms;">Report Design&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp;&nbsp;Report Management &amp; Delivery&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp;&nbsp;Flexible Application Development <br>
    </p>
      </td>
  </tr>
</table>
<table width="600" cellpadding="20" cellspacing="0" bgcolor="#FFFFFF">

<tr>

<td width="386" valign="top" bgcolor="#FFFFFF" style="font-size:12px;color:#000000;line-height:150%;font-family:trebuchet ms;"><p><img src="http://www.feeslimited.net/newsletters/crystal_training/img/cr_header.jpg" alt="Crystal Reports Design" width="350" height="70"></p>
<p>The leaders of tomorrow&rsquo;s economy are those who have access to  up-to-date information and effective analytical tools that empower them to take  better decisions leading to higher margins, cost visibility/reduction and  sustained business success.</p>
<p><br>
  The ability to use Crystal Reports to easily access complete  business data, turn it into actionable business information, manage and share  relevant reports securely with do-it-yourself capabilities for business users  is critical to growth &amp; profitability of many businesses.<br>
  FEES Limited is pleased to offer its Crystal Reports Design -  Business Reporting Solution to strengthen the knowledge base of your workforce  and gain requisite skills needed to unlock full value of your investments in  the software.</p>
<p><br>
  This&nbsp;<strong>two-day</strong>&nbsp;instructor-led course is designed to  equip participants with the following skills among others:<br>
    <br>
    <strong class="style15">Powerful Report Design</strong>&nbsp;&ndash; design powerful, dynamic, actionable reporting solutions that  business users can explore and visualize interactively; deliver the reports via  the web or embedded in enterprise applications. Take advantage of complete set  of layout and design controls to create highly formatted, interactive, and  professional-looking reports. Learn the skills to create compelling reports  with stunning visualizations and business dashboards.<br>
    <br>
    <strong class="style15">Report Management and Delivery</strong>&nbsp;- gain the skills needed to deliver relevant business reports to targeted  users, publish to the Web for easy and controlled sharing, enabling better  business decisions at all levels of the organization. Learn to take full  advantage different reporting formats to deliver business information in the  most appropriate and non-obstructive format. Empower end users to explore  reports interactively.<br>
    <br>
    <span class="style15"><strong>Flexible Application Development</strong>&nbsp;</span>- learn how to leverage cross-platform support for both Java  and.NET. and gain the advantage of focusing on application business logic,  rather than tedious, time-intensive hand coding.&nbsp;<br>
&middot;&nbsp;&nbsp;&nbsp;&nbsp;Save valuable report design time.&nbsp;<br>
&middot;&nbsp;&nbsp;&nbsp;&nbsp;Enjoy comprehensive report deployment options and an  improved report designer.</p>
<p><strong>Additional Planned Runs:</strong><strong>&nbsp;</strong>Nov 12-13, Nov 19-20 and Dec 10-11</p>
<hr>
<p><span class="style27"><span class="style25"><strong>Crystal Reports</strong>&nbsp;is a powerful, dynamic, actionable  reporting solution that helps you design, explore, visualize, and deliver  reports via the web or embedded in enterprise applications.&nbsp;</span>&nbsp;</span>&nbsp;</p>
<hr>
<p>For further information,  please contact:&nbsp;<br>
    <strong>Adewuyi,  Oluwatosin.</strong><strong><br>
    </strong>Coordinator, Learning &amp; Growth.<br>
    <strong>Mobile:</strong> +234 803 482 0279 <br>
    <strong>Email:</strong>  <a href="mailto:o.adewuyi@feeslimited.net">o.adewuyi@feeslimited.net</a></p>
</td>


<td width="132" valign="top" style="background-color:#FFFFFF;border-left:1px dashed #CCCCCC;text-align:left;"><p class="style17"><span class="style18"><span class="style16"><span class="style20"><strong>1st Run:&nbsp;</strong>Oct.22-23, 2009 <br>
            <br>
            <strong>Promo Price: </strong>=<strong>N= 70,000 </strong>per delegate<br> 
    (This represents  a substantial discount off regular course fee of =N=84,893:00) <br>
            <br>
    </span></span></span><span class="style19">Register before Oct.16 and save  an additional 10%. <br>
    <span class="style25"><br>
          </span></span><span class="style26"><strong>Group discounts</strong>: </span><span class="style23">Further 10% for 2 or more delegates.</span></p>
  <p class="style17"><span class="style23"><strong>Delivery: </strong>Instructor-Led; by Certified Crystal Professionals.</span></p>
  <p class="style17"><span class="style23"><strong>Contact: </strong><a href="mailto:o.adewuyi@feeslimited.net">o.adewuyi@feeslimited.net</a></span></p>
  <p align="center" class="style12"><br>
    <a href="ttp://www.feeslimited.net"><img src="http://www.feeslimited.net/newsletters/crystal_training/img/sidebar_ad.jpg" alt="sidebar" width="120" height="650" border="0"></a></p></td>
</tr>
<tr>
  <td colspan="2" align="center" valign="top" bgcolor="#FFFFFF" style="font-size:12px;color:#000000;line-height:150%;font-family:trebuchet ms;"><img src="http://www.feeslimited.net/newsletters/crystal_training/img/company_email_footer.jpg" alt="footer" width="550" height="85"></td>
  </tr>


<tr>
<td colspan="2" valign="top" style="backgr
2

You've indented the closing MAILHTML; identifier. You need to leave this as I showed in the code. No indents allowed. Also, your mail function has an extra comma in it

if(mail($to,$subject,$message,, $headers, "From: $sendermail\n"))

Remove that as well. Problem solved.

Also, I suggest that you move all that HTML to an external file and use something like file_get_contents() to extract the HTML. Much cleaner all around. Just a thought.

Hope this helps, close if successful. I'm off for some much needed sleep :]

Votes + Comments
Excellent! Fixed the problem with finality.
Good advice :)
0

Also, I suggest that you move all that HTML to an external file and use something like file_get_contents() to extract the HTML. Much cleaner all around. Just a thought.

Agreed. There is nothing more annoying then having loads of HTML in your PHP scripts.
If you need to replace variables in your HTML, just do something like:

<?php
/*** mailHTML.php ***/
$str =<<<HTML
<html>
  <body>
    <h1>$someVariable</h1>
  </body>
</html>
HTML;

return $str;
?>
<?php
/*** index.php ***/
$someVariable = "Hello, world!";
$mailBody = include("mailHTML.php");

// Etc, using $mailBody as the mail body (obviously xD)
?>

I would also suggest that you abandon the old mail function altogether and try one of the free Mailer classes available. Like PHPMailer or Swift Mailer. They are usually much easier to work with, especially if you are sending HTML mails or attachments.

0

You've indented the closing MAILHTML; identifier. You need to leave this as I showed in the code. No indents allowed. Also, your mail function has an extra comma in it. Remove that as well. Problem solved.

Man real swell! Dude, you rock! Thanks, works like a dream now. I'll get around to the external file arrangement next. But it feels so good to see it finally doing what I need it to do now. Be in touch.

0

...just do something like:

<?php
/*** index.php ***/
$someVariable = "Hello, world!";
$mailBody = include("mailHTML.php");

// Etc, using $mailBody as the mail body (obviously xD)
?>

Hey Atli,
I tried what you suggested but the php file echoes the external web page within itself instead of sending it as the email I wanted. The section of the script in question goes thus:

$message = include("web_version.htm");

if(mail($to,$subject,$message,$headers)) {
echo "<b><font size=+2>Newsletter Sent!</font></b><p> \n\n\nA copy of the current $formname has been sent to $presalutation $salutation $recipientname, with your name ($sendername) as sender. \n\n";
} else {
echo "There was a problem sending the Newsletter. Please check that you filled in the form correctly. \n\n\n";
}

How do I get around this? I've attached the whole script for you to see, what's going wrong with it?

Edited by kayblack: Grammatical errors

Attachments
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>


<? 
$sendername=$_POST['sendername'];
$sendermail=$_POST['sendermail'];
$salutation=$_POST['salutation'];
$presalutation=$_POST['presalutation'];
$recipientname=$_POST['recipientname'];
$recipientemail=$_POST['recipientemail'];
$recipientcompanyname=$_POST['recipientcompanyname'];
$recipientaddress=$_POST['recipientaddress'];
$sendercomment=$_POST['sendercomment'];
$formname=$_POST['formname'];
$subject="Good day $presalutation $salutation $recipientname. You should see this.";
$recipientform=$_POST['recipientform'];

$to="$recipientemail";
$headers = 'From: info@feeslimited.net' . "\r\n" .
        'Reply-To: info@feeslimited.net' . "\r\n" .
        "X-Mailer: PHP/" . phpversion()."\r\n";
 
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
$message = include("web_version.htm");

if(mail($to,$subject,$message,$headers)) {
echo "<b><font size=+2>Newsletter Sent!</font></b><p> \n\n\nA copy of the current $formname has been sent to $presalutation $salutation $recipientname, with your name ($sendername) as sender. \n\n";
} else {
echo "There was a problem sending the Newsletter. Please check that you filled in the form correctly. \n\n\n";
}

?>


<body>
</body>
</html>
0

Er, sorry, I didn't realize this post would reopen the thread. The original problem is really solved, but this issue with the 'include' function suggested by Atli involves making the application better.

0

Hey Atli,
I tried what you suggested but the php file echoes the external web page within itself instead of sending it as the email I wanted.

Hey.

To include it as a string, the included script must return the value. If you just include a static HTML file, or a PHP file without a return value, the include function just prints the output of the included file directly into the output buffer.

<?php
echo "Prints this into the page when this is included.";
?>
<?php
return "Returns this as a string when included, allowing you to store it in a variable.";
?>

Also, check out Example #5 in the include manual entry. Explains this a bit better than I am :-)

0

Including an HTML will automatically output it to the browser since include() and require() just executes the added page immediately. Just use something like file_get_contents.

Instead of

$message = include("web_version.htm");

Use

$message = file_get_contents("web_version.htm");

Problem solved.

0

Just use something like file_get_contents.
[...]
Problem solved.

Not really.

The point of including the file rather than reading it was that you could inject values into it. If you use file_get_contents you would have to do string replacement to achieve that.

If you don't need to inject values into it, using file_get_contents to read it statically works just fine tho.

0

Can you show me exactly the include/return code should look?
I tried:

$message = include("web_version.htm");
return;

But there's no change, it's still outputting the included file in the browser. And of course, using:

$message = include("web_version.htm");
return();

gives me a straight up parse error (syntax error).
What's the code supposed to look like?

0

What Atil meant with returning the value was that in the included file (has to be a PHP file, say mailcontent.php) you return a value.

//mailcontent.php
$dynamicVariable = date();
$message = <<<MAILHTML
Some message contnet {$dynamicVariable}
MAILHTML;

return $message;

//sendmail.php
$content = include 'mailcontent.php';
//do something with $content

Like Atil said, which function you want to use (file_get_contents, include) depends on what kind of content you are reading from the file.

0

Yes, exactly what jomanlk said.

The include function is meant to include code into the script. If you want to print or fetch a static file (like a HTML file) you are better of using readfile or file_get_contents, respectively.

Can you show me exactly the include/return code should look?

This is a working example of how a included file using a return value works:

<?php
/**
 * File: inc.messages.php
 */

// Compose the message, using variables set in the file that includes
// this one. ($userName, in this case)
$message =<<<HTML
<!DOCTYPE html>
<html>
<body>
    <p>
        Thank you, {$userName}, for doing whatever you did to
        trigger this automated email response.
    </p>
</body>
</html>
HTML;

// Return the message to the page that called the include as a variable.
return $message;
?>
<?php
/**
 * File: index.php
 */

if(isset($_POST['user_name']))
{
    // Set the $userName variable that will be used by the code
    // in the included file.
    $userName = htmlentities($_POST['user_name']);

    // Include the file and capture the return value.
    $message = include("inc.message.php");

    // Send the email
    // ... which I leave to your imagination.
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
    <form action="?" method="post">
        <input type="text" name="user_name" /><br />
        <input type="submit" />
    </form>
    <?php
    // See if a message was sent and print it.
    // Just for demonstration purposes. You wouln't actually want todo this live.
    if(isset($message))
    {
        echo "<pre>", htmlentities($message), "</pre>";
    }
    ?>
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.