I am attempting to create an email program using PHP and have so far managed to get it to send emails and have basic formatting, etc but the main reason I created it was to us attachments and that I cannot get to work.
In all honesty I am a beginner at PHP and the code I am using has come from the book "Beginning PHP4" by Wrox Press Ltd in Chapter 15.
Below is the code I am using and I would be very grateful to anyone who can tell me what is wrong, bearing in mind I'm a beginning. You can also see the code in action so to speak at http://www.2312.co.uk/email/email.php
Code: Select all
<?php
//simple_mailer.php
function mailer_header()
{
?>
<html>
<head>
<title>Emailer</title>
</head> //10
<body>
<?php
}
function mailer_footer()
{
?>
</body>
</html>
<?php //20
}
function error_message($msg)
{
mailer_header();
echo "<script>alert("Error: $msg");history.go(-1)</script>";
mailer_footer();
exit;
}
//30
function user_message($msg)
{
mailer_header();
echo "<script>alert("$msg");history.go(-1)</script>";
mailer_footer();
exit;
}
function mail_form()
{ //40
global $PHP_SELF;
?>
<form method="post" name="email" enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>">
<input type="hidden" name="action" value="send_mail">
<div align="center ">
<table cellspacing="2" cellpadding="5" width="90%" border="1">
<tr>
<th align="center" width="30%">To</th>
<td width="70%"><input name="mail_to" size="20"> <A HREF="Javascript:void(0)" onClick="window.open('http://www.2312.co.uk/email/addressbook.html', 'address', 'width=210,height=250,toolbar=no,status=no,location=no,menubar=no,directories=no,scrollbars=yes,resizable=no')">Address Book</a><input type="hidden" value="Stuart Leyland" name="mail_from"><input type="hidden" value="stuart@2312.co.uk" name="mail_reply_to"></td>
</tr> //50
<tr>
<th align="center" width="30%">Attachment</th>
<td width="70%"><input type="file" name="mail_userfile" size="20"></td>
</tr>
<tr>
<th align="center" width="30%">Type</th>
<td width="70%">
<input type="radio" value="html" name="mail_type" checked>HTML
<input type="radio" value="text" name="mail_type">Text
</td> //60
</tr>
<tr>
<th align="center" width="30%">Encoding</th>
<td width="70%">
<input type="radio" value="7bit" name="mail_encoding" checked>7Bit
<input type="radio" value="8bit" name="mail_encoding">8BIT
</td>
</tr>
<tr>
<th align="center" width="30%">Character Set</th> //70
<td width="70%">
<input type="radio" value="us-ascii" name="mail_charset" checked>US-ASCII
</td>
</tr>
<tr>
<th align="center" width="30%">Subject</th>
<td width="70%"><input name="mail_subject" size="20"></td>
</tr>
<tr>
<th align="center" width="30%">Change the appearance</th> //80
<td width="70%">
<script language=Javascript>
<!--
{
document.write('<img border="0" src="http://www.2312.co.uk/email/images/bold.gif" onClick=f1()><img border="0" src="http://www.2312.co.uk/email/images/italic.gif" onClick=f2()><img border="0" src="http://www.2312.co.uk/email/images/underline.gif" onClick=f3()><img border="0" src="http://www.2312.co.uk/email/images/hyperlink.gif" onClick=f4()><img border="0" src="http://www.2312.co.uk/email/images/image.gif" onClick=f5()>');
}
-->
</script>
<A HREF="Javascript:void(0)" onClick="window.open('http://www.2312.co.uk/email/font.html', 'font', 'width=200,height=220,toolbar=no,status=no,location=no,menubar=no,directories=no,scrollbars=yes,resizable=no')">Change the font</a>
<A HREF="Javascript:void(0)" onClick="window.open('http://www.2312.co.uk/email/bgcolour.html', 'bgcolour', 'width=220,height=220,toolbar=no,status=no,location=no,menubar=no,directories=no,scrollbars=yes,resizable=no')">Change the background colour</a>
<script language=Javascript>
function f1(){
document.email.mail_body.value= document.email.mail_body.value+ "<b>Text to be bold here</b>"
}
function f2(){
document.email.mail_body.value= document.email.mail_body.value+ "<i>Text to be italic here</i>"
}
function f3(){
document.email.mail_body.value= document.email.mail_body.value+ "<u>Text to be underlined here</u>"
}
function f4(){
document.email.mail_body.value= document.email.mail_body.value+ "<a href='link here'>Text here</a>"
}
function f5(){
document.email.mail_body.value= document.email.mail_body.value+ "<img src='URL to image here'>"
}
</script>
</td>
</tr>
<tr>
<th align="center" width="30%">Body</th>
<td width="70%"><textarea name="mail_body" rows="16" cols="70"></textarea></td>
</tr>
<tr>
<th width="100%" colspan="2" align="center">
<input type="submit" value="Send" name="submit"> //90
<input type="reset" value="Reset" name="reset">
</th>
</tr>
</table>
</div>
</form>
<?php
}
function send_mail() //100
{
global $mail_to, $mail_from, $mail_reply_to;
global $mail_body, $mail_subject;
global $userfile, $userfile_type, $userfile_name, $userfile_size;
global $mail_type, $mail_charset, $mail_encoding;
$mail_parts["mail_to"] = $mail_to;
$mail_parts["mail_from"] = $mail_from;
$mail_parts["mail_reply_to"] = $mail_reply_to;
$mail_parts["mail_subject"] = trim($mail_subject);
$mail_parts["mail_body"] = $mail_body;
//110
$mail_parts["mail_type"] = $mail_type;
$mail_parts["mail_charset"] = $mail_charset;
$mail_parts["mail_encoding"] = $mail_encoding;
$mail_parts["userfile"] = $userfile;
$mail_parts["userfile_type"] = $userfile_type;
$mail_parts["userfile_name"] = $userfile_name;
$mail_parts["userfile_size"] = $userfile_size;
if(my_mail($mail_parts)) //120
user_message("Successfully sent an email entitled '$mail_subject'.");
else error_message("An unknown error occurred while attempting to
send an e-mail titled '$mail_subject'.");
}
function my_mail($mail_parts)
{
$mail_to = $mail_parts["mail_to"];
$mail_from = $mail_parts["mail_from"];
$mail_reply_to = $mail_parts["mail_reply_to"];
$mail_subject = $mail_parts["mail_subject"];
$mail_body = $mail_parts["mail_body"];
//130
$mail_type = $mail_parts["mail_type"];
$mail_charset = $mail_parts["mail_charset"];
$mail_encoding = $mail_parts["mail_encoding"];
$userfile = $mail_parts["userfile"];
$userfile_type = $mail_parts["userfile_type"];
$userfile_name = $mail_parts["userfile_name"];
$userfile_size = $mail_parts["userfile_size"];
if(empty($mail_to)) error_message("Fill in the To field!"); //140
if(empty($mail_subject)) error_message("Please give this email a subject!");
$mail_to = str_replace(";", ",", $mail_to);
$mail_headers = '';
if(!empty($mail_from)) $mail_headers .= "From: $mail_from\n";
if(!empty($mail_reply_to)) $mail_headers .= "Reply-to: $mail_reply_to\n";
$mail_subject = stripslashes($mail_subject);
$mail_body = stripslashes($mail_body);
if($userfile_size > 0) //150
{
$mail_boundary = md5(uniqid(time()));
$mail_headers .= "MIME-Version: 1.0\r\n";
$mail_headers .= "Content-type: multipart/mixed;
boundary="$mail_boundary"\r\n\r\n";
$mail_headers .= "This is a multi-part message in MIME format.\r\n\r\n";
$fp = fopen($userfile, "r");
$file = fread($fp, filesize($userfile));
$file = chunk_split(base64_encode($file)); //160
$new_mail_body = "--$mail_boundary\r\n";
$new_mail_body .= "Content-type:text/plain;charset=$mail_charset\r\n";
$new_mail_body .= "Content-transfer-encoding:$mail-encoding\r\n\r\n";
$new_mail_body .= "$mail_body\r\n";
$new_mail_body .= "--$mail_boundary\r\n";
if(!empty($userfile_type)) $mime_type = $userfile_type;
else $mime_type = "application/octet-streram";
$new_mail_body .= "Content-type:$mime_type;name=$userfile_name\r\n";
$new_mail_body .= "Content-transfer-encoding:base64\r\n\r\n";
$new_mail_body .= $file . "\r\n\r\n"; //170
$new_mail_body .= "--$mail_boundary--";
$mail_body = $new_mail_body;
}
else if($mail_type == 'html')
{
$mail_headers .= "content-type: text/html; charset=$mail_charset\r\n";
$mail_headers .= "Content-transfer-encoding:$mail_encoding\r\n\r\n";
}
else
{
$mail_headers .= "Content-type: text/plain; charset=$mail_charset\r\n";
$mail_headers .= "Content-transfer-encoding:$mail_encoding\r\n\r\n";
}
return mail($mail_to,$mail_subject,"$mail_body",$mail_headers);
}
switch ($action)
{
case "send_mail":
mailer_header();
send_mail();
mailer_footer();
break;
case "mail_form":
mailer_header();
mail_form();
mailer_footer();
break;
default:
mailer_header();
mail_form();
mailer_footer();
break;
}
?>Stuart