Hi

I made a function which is suppose to send email, only problem is that I can send the email but its blank so can anyone tell me what I am going wrong.

//Function that send multipart emails
function email2($TO, $PREMADE, $FROM = false)
{
 //headers for emails
 
 //create a boundary string. It must be unique 
 //so we use the MD5 algorithm to generate a random hash
 $random_hash = md5(date('r', time()));
 
 if (!$FROM)
 {
  $Header = 'From: '.EMAIL_FROM. "\r\n";
 }
 else
 {
  $Header = 'From: '.$FROM. "\r\n";
 }
 
 $Header .= "\r\nContent-Type: multipart/alternative; boundary=\"alt-".$random_hash."\"";
 
 
 ob_start(); //Turn on output buffering
 switch($PREMADE)
 {
  case 'signup':
   $SUBJECT = "Thank you for signing up with test.com";
   ?>
--alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
Dear <?php echo $_POST[name];?>,
Thank you for submitting your company information to test.com.
Information you provided will be reviewed, on approval you will be sent a E-Mail with your password to access our website.
It generally takes one to two business days for approval.
Thanks and regards,
test
Note: Please do not reply to this message, as we will not be able to respond. 
--alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
<font face="Arial" size="2">Dear <?php echo $_POST[name];?>,<br>
<br>
Thank you for submitting your company information 
to test.com.<br>
Information you provided will be reviewed, on approval you will be sent a E-Mail 
with your password to access our website.<br>
It generally takes one to two business days for approval.<br>
<br>
Thanks and regards,<br>
test<br>
<font color="#008000"><b>Note:</b> Please do not reply to this message, as we 
will not be able to respond.</font></font>
--alt-<?php echo $random_hash; ?>--
<?php
   break;
}
 //copy current buffer contents into $message variable and delete current output buffer
 $MESSAGE = ob_get_contents();
 ob_get_clean();
 $mail_sent = mail($TO, $SUBJECT, $MESSAGE, $Header);
 
 return $mail_sent;
 
 
 
 
}
$MESSAGE = ob_get_contents();

Please a var_dump($MESSAGE); after that to see if you actually get the msg saved to that variable. I believe your PHP install doesn't support output buffering or its turned off.

I did set

$MESSAGE = ob_get_contents();

and did

var_dump($MESSAGE);

it displayed the message of screen.
I have no idea what is wrong here.

Ok looks like my server had a problem. I talked to my server ppl and they fixed the problem.

Now I get the message and all I see is

Content-Type: multipart/alternative;
boundary="alt-0d384c9fef45078c0aa547592bd6fb46"
 
--alt-0d384c9fef45078c0aa547592bd6fb46 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
 
Dear test,
 
As requested your password has arrived, if you did not authorize this please use
the information below to login and change your password.
Please use the fallowing information to login:
 
Your User Name: [EMAIL="test@test.com"]test@test.com[/EMAIL]
Password: test
Suggestion: Copy and
paste your password as it appears in gray box above.
 
Please login in to your account anytime you like.
 
Thanks and regards,
test
Note: Please do not reply to this message, as we will not be able to respond. 
 
--alt-0d384c9fef45078c0aa547592bd6fb46 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
 
<font face="Arial" size="2">Dear test<br>
<br>
As requested your password has arrived, if you did not authorize this please 
use the information below to login and change your password.<br>
Please use the fallowing information to login:<br>
<br>
Your User Name: <span style="background-color:
#C0C0C0">test@test.com</span><br>
Your Password:&nbsp; test <span style="background-color: #C0C0C0"></span><br>
</font><font face="Arial" size="1"><span style="background-color: #99CCFF"><b>
Suggestion:</b> Copy and paste your password as it appears in gray box
above.<br>
</span></font><font face="Arial" size="2"><br>
Please login in to your account anytime you like.<br>
<br>
Thanks and regards,<br>
test<br>
<font color="#008000"><b>Note:</b> Please do not reply to this message, as we 
will not be able to respond.</font></font>
 
--alt-0d384c9fef45078c0aa547592bd6fb46--

One thing I have the hardest time with is sending emails through php as I cant send pure html because half of the people which will get this email use some odd email client which cant read HTML.

Also I have no experince in writing multipart and I am just modifying a example which I got of the net. so if any one can tell me how to successfully send multipart emails would be nice.

I was trying your function, it worked for me when I moved ob_get_clean() to the end, after return $mail_sent. I believe that ob_get_clean was empting the variable $MESSAGE before the script could send the email.

You can try with this little function:

function hello($name) {
	ob_start();
   	echo 'hello ' . $name;
   	$message = ob_get_contents();
   	return $message;
   	ob_get_clean();
     }
   
     hello("world");

If you put ob_get_clean() before of return $message, you won't get anything

ok Thank You,
I see what the problem is. there were few more bugs which I found out and fixed.

I was trying your function, it worked for me when I moved ob_get_clean() to the end, after return $mail_sent. I believe that ob_get_clean was empting the variable $MESSAGE before the script could send the email.

You can try with this little function:

function hello($name) {
	ob_start();
   	echo 'hello ' . $name;
   	$message = ob_get_contents();
   	return $message;
   	ob_get_clean();
     }
   
     hello("world");

If you put ob_get_clean() before of return $message, you won't get anything

It could be that the php you tested this code on also doesn't support output buffering.

Try this test:

if (ob_start()) {
echo 'Output buffering supported';
} else {
echo 'Output buffering not supported';
}
ob_flush();

in the current function ob_get_clean(); will never be called in the function as it comes after return.
The echo 'hello ' . $name; will be printed to screen if buffering isn't supported, making it look like the function worked.

If our server does support output buffering, then this might be a PHP bug.
ob_get_clean() should not unset a variable.

I believe it's best not to use output buffering for creating strings. You can do it with the literal string (quotes) syntax or the heredoc syntax.

eg: from php manual

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

One thing I have the hardest time with is sending emails through php as I cant send pure html because half of the people which will get this email use some odd email client which cant read HTML.

Also I have no experince in writing multipart and I am just modifying a example which I got of the net. so if any one can tell me how to successfully send multipart emails would be nice.

Basically what you want to do is provide different alternatives for the email reader (client). That way if they don't read HTML mail, then they will revert to the plain-text version.

If you want to learn the mime encoding format, then you could read the RFC.
http://www.ietf.org/rfc/rfc2387.txt
http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

You could however use an existing Open Source PHP mailer class. This will have a mime parser and mime encoder. So you don't have to worry about learning the protocol.

eg: http://www.swiftmailer.org/

You could also use a mime encoder class, handle the mailing of the encoded mime messages with the mail() class, or some other mailer.

eg: http://pear.php.net/package/Mail_Mime

Its best not to use mail() if you will be sending large amounts of mail, or large attachments.
Eg: swiftmailer, will make sure the large attachments are streamed it small parts, so that the whole file does not have to pass through the server memory at once. mail() will have the whole mime message loaded to memory before sending it on.

It could be that the php you tested this code on also doesn't support output buffering.

Try this test:

if (ob_start()) {
echo 'Output buffering supported';
} else {
echo 'Output buffering not supported';
}
ob_flush();

I've tried your test. In my case buffer works fine. And you are right, ob_get_clean can't unset a variable. I've made a big mistake, by no echoing my test function: echo hello("world"); Thanks for the clear explanation digital-ether, it is a very pleasure to read your posts :)

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.