Jump to content

header() won't work when using custom function


BluMess

Recommended Posts

Hi all,

 

Just before I start: Searched the internet + forums, and couldn't find an answer.

 

Ok, I've made my own email script which sends a nice HTML formatted email to a user, but when I include it in a PHP script and call the function, it stops header redirects from working.

 

Here's the email script, I've shortened it to cut out the formatting of the email etc.

 

<?php
function sendEmail($email, $to, $author, $title, $body, $link, $link_title, $unsubscribe_link){

$headers = "From: NocturnalHaunt <mailer@nocturnalhaunt.com>\r\nReply-To: BluMess@nocturnalhaunt.com";//put you own stuff here or use a variable
$subject = $author." - ".$title; //Defined in the function
$html ='all html goes here (shortened)';//make up your own html or use an include
//the below is your own plain text message (all the $message(x))
$message0 = 'Hey '.$to.',';// or make up your own for plain text message
$message1 = $body.' - click the link below to view it';
$message2 = $link;
$message3 = 'Thanks!';
$message4 = '~The NH Team';
$message5 = 'You are recieving these emails because you have subscribed to '.$author.'. If you would like to unsubscribe, click this link: '.$unsubscribe_link;

// Generate a boundary string that is unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"<font face=Arial>" .
$html."\r\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message0 . "\n\n" .
$message1 . "\n" .
$message2 . "\n\n" .
$message3 . "\n" .
$message4 . "\n\n" .
$message5;

// Send the message
$send = mail($email, $subject, $message, $headers);
?>

 

Does anyone have any ideas? I know header doesn't work if some information has been displayed, but the redirect used to work perfectly until I started using this.

I use an include to import it into other PHP scripts if that helps.

 

Thanks in advance

~Chris

Link to comment
Share on other sites

What's up with the user function on line one? Should definitely result in a syntax error. And where's $email defined?

 

Edit: Sorry, just saw that you cut the script. But still - be sure that $email is a valid address.

 

2nd edit: Do you get any error messages? Is error reporting turned on?

Link to comment
Share on other sites

Most of the variables are in the $html string, but I've shortened it so all the HTML doesn't clutter up the forum :)

They get tossed in $html, and then that gets put with the MIME types and then sent.

 

Have I written the function wrongly? Could that be why the headers don't work? I really don't have a clue. Why should it cause an error? :S

Link to comment
Share on other sites

Ok:

 

<?
include("../Scripts/sendemail.php");

//Further down the file - no echos, prints, returns etc. which caused any problems before

while ($row = mysql_fetch_array($result))    //<-- LOOP THE RESULTS
    {

sendEmail($row['email'], $row['username'], $author, "New Portal Game", "$author has just submitted a new game to the NocturnalHaunt portal", "http://www.nocturnalhaunt.com/portal/game$id", $title, "http://www.nocturnalhaunt.com/login/s_delete.php?id=$row[id]");

    }
?>

Link to comment
Share on other sites

You never close your function '}'...

 

Oops, sorry about that - I didn't copy it right :) Thanks for pointing that out.

 

Just to make this clear - the function ALL WORKS PERFECTLY, it sends the email(s), but for some reason it stops any headers working =/

 

If no one can figure out what's going on I suppose a HTML redirect could be used instead. Headers don't work if something has been outputted right? So, I thought perhaps something in the sendEmail script (like an echo or something) would prevent the headers from working properly. They work OK when I remove the include("../Scripts/sendemail.php"), but not when that file is included.

Link to comment
Share on other sites

Do you have error reporting on?

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

The only other thing I can think of is whitespace issues, which it looks like you don't have any, but if you do the display errors should tell you, good luck.

Link to comment
Share on other sites

Oh, thanks for that tip - I didn't know about that.

 

Whitespace issues? :S

 

ENTIRE SENDEMAIL SCRIPT:

 

<?php
################################################################################
###########
### An email script that will attach images inline in an HTML and plain text e-mail ###
### Just enter you own email infomation and file names with paths where indicated in ###
### the script. Have fun with it alter it add to it or whatever you want. When you are ###
### done reading all the confusing tutorials about this kind of using mail to send your ###
### own without PHPmailer then you can just use this file to suit your self. ###
################################################################################
###########
//From http://www.astahost.com/info.php/Php-Mail-Function-Images-Attachments_t12553.html
function sendEmail($email, $to, $author, $title, $body, $link, $link_title, $unsubscribe_link){

$headers = "From: NocturnalHaunt <mailer@nocturnalhaunt.com>\r\nReply-To: BluMess@nocturnalhaunt.com";//put you own stuff here or use a variable
$subject = $author." - ".$title; //Defined in the function
$html ='<style type="text/css">
@charset "utf-8";
body{
background-image:url(http://www.nocturnalhaunt.com/Images/background.png);
background-repeat:repeat-x;
background-color:#02101B;
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
color: #D7EAFF;
}
a:link {
color: #E9821B;
text-decoration: none;
}
a:visited {
color: #D07D33;
text-decoration: none;
}
a:hover {
color: #ED9A47;
text-decoration: underline;
}
a:active {
color: #E9821B;
text-decoration: none;
}
.normal {
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
color: #D7EAFF;
}
.normalBold {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #FFF3DD;
}
.newsHeader {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
color: #F5C24B;
}
.portalHeader {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold;
color: #FFFFFF;
}
.faint {
color:#7F7F7F;
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
}
</style>
<table width="100%" height="700" background="http://www.nocturnalhaunt.com/Images/email_bg.png"><!--DWLayoutTable-->
  <tr>
    <td height="82" colspan="4" align="center" valign="top"><a href="http://www.nocturnalhaunt.com"><img src="http://www.nocturnalhaunt.com/Images/email_logo.png" width="300" height="80" /></a></td>
  </tr>
  <tr>
    <td height="16" colspan="4" align="center"><a href="http://www.nocturnalhaunt.com">www.nocturnalhaunt.com</a></td>
  </tr>
  <tr>
    <td width="862" colspan="3" align="center" valign="top" style="padding-top:50px;"><table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="500" height="20" background="http://www.nocturnalhaunt.com/Images/email_top.png" style="background-repeat:no-repeat;"></td>
      </tr>
      <tr>
        <td align="left" valign="top" bgcolor="#131820" class="newsHeader" style="padding:0 20px;"><p class="portalHeader">Hey '.$to.'!</p>
          <blockquote>
            <p class="normal"><span class="newsHeader" style="text-transform:capitalize;">'.$title.'</span><br />
              <span class="normalBold">'.$body.':</span></p>
            <p class="pj_memo">"<a href="'.$link.'">'.$link_title.'</a>"</p>
            <p class="normal"> </p>
            <p class="faint">';

		if($unsubscribe_link){
		$html .= 'You are recieving these emails because you have subscribed to '.$author.'. If you would like to unsubscribe, <a href="'.$unsubscribe_link.'">click here</a></p>';
		}
		$html .='<p class="normal">Thanks,<br />
              ~The NH Team<br />
            </p>
            </blockquote></td>
      </tr>
      <tr>
        <td width="500" height="20" background="http://www.nocturnalhaunt.com/Images/email_bottom.png" style="background-repeat:no-repeat;"> </td>
      </tr>
    </table>
    <br />
    <img src="http://www.nocturnalhaunt.com/Images/NHcharacter2.png" width="137" height="199" />';//make up your own html or use an include
//the below is your own plain text message (all the $message(x))
$message0 = 'Hey '.$to.',';// or make up your own for plain text message
$message1 = $body.' - click the link below to view it';
$message2 = $link;
$message3 = 'Thanks!';
$message4 = '~The NH Team';
$message5 = 'You are recieving these emails because you have subscribed to '.$author.'. If you would like to unsubscribe, click this link: '.$unsubscribe_link;

// Generate a boundary string that is unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"<font face=Arial>" .
$html."\r\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message0 . "\n\n" .
$message1 . "\n" .
$message2 . "\n\n" .
$message3 . "\n" .
$message4 . "\n\n" .
$message5;

// Send the message
$send = mail($email, $subject, $message, $headers);
//if ($send) {
//return "<p>Email Sent to intended recipients successfully!</p>";
//} else {
//return "<p>Mail could not be sent. You missed something in the script. Sorry!</p>";
//}
}


?> 

 

Thanks for all your replies, guys. I'm just sorry I don't know what's going on :(

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.