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?

Recommended Answers

All 15 Replies

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 :-]

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?

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

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.

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 :]

commented: Good advice :) +3
commented: Excellent! Fixed the problem with finality. +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.

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.

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

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.

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

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.

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.

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?

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.

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.