I'm trying to create a multipiart mime email message, but I seem to be failing miserably.
I really want to do this without extra functions, and classes. It's not that I want to reinvent the wheel. I jsut want to learn how to create my own wheel.
I've read other peoples work, and thought I had an idea how to do it. It does work in the sense that emails do go out. It just doesn't come through the way I expect.
I'm trying to create an email that sends out as plain text and HTML so incompatible mail viewers can read it.
THE CODE:
<?php
###################################
## Set mode ##
## Use either "test" or "live" ##
###################################
#$mode = "live";
$mode = "test";
######################################################
## Check to see if this is the first time through ##
## and make sure both the email and the question ##
## contain something. ##
## If they do, move the contents to the scalar ##
######################################################
if (isset($_POST['emailaddress']) && isset($_POST['question'])){
$emailAddress = $_POST['emailaddress'];
$question = stripslashes($_POST['question']);
##########################################################
## Make sure the email address is formatted correctly ##
##########################################################
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $emailAddress)) {
$error = "Your email address does not appear to be formatted correctly. Your email address must contain the @ the . and no spaces.";
require 'Bible-Questions.php';
exit;
}//end eregi
####################################
## Break the email into 2 parts ##
####################################
list( $Username, $Domain )=split("@",$emailAddress);
#############################################
## Check for a Mail eXchange (MX) record ##
#############################################
if(getmxrr($Domain, $MXHost)){
#################################################
## Declare variables to be used in the email ##
#################################################
if ($mode == "live"){
$gotquestionsEmail = "questions@gotquestions.org";
$confirmationEmailFileHTML = "_email/confirm.html";
$confirmationEmailFileTXT = "_email/confirm.txt";
$refresh = "0";
}elseif ($mode == "test"){
$gotquestionsEmail = "sparctacus@gmail.com";
$confirmationEmailFileHTML = "../_email/confirm.html";
$confirmationEmailFileTXT = "../_email/confirm.txt";
$refresh = "30";
}else{
echo "Error: Mode isn't set.";
}
########################
## Confirmation Email ##
########################
$confirmationEmailTo = "$emailAddress";
$confirmationEmailSubject = "Question received";
$mimeBoundary = "---=gotquestions-MIME-boundary=---";
$openFileHTML = fopen($confirmationEmailFileHTML, "r");
$openFileTXT = fopen($confirmationEmailFileTXT, "r");
$confirmationEmailHTML = fread($openFileHTML, filesize($confirmationEmailFileHTML));
$confirmationEmailTXT = fread($openFileTXT, filesize($confirmationEmailFileTXT));
fclose($openFileHTML);
fclose($openFileTXT);
$confirmationEmailFrom = "$gotquestionsEmail";
$confirmationEmailExtraHeaders = "From: \"Gotquestions\" <$gotquestionsEmail>\n";
$confirmationEmailExtraHeaders .= "X-Sender: $gotquestionsEmail\n";
$confirmationEmailExtraHeaders .="X-Mailer: Questions_V2\r\n";
$confirmationEmailExtraHeaders .="Reply-To: $gotquestionsEmail\r\n";
$confirmationEmailExtraHeaders .="Return-Path: <$gotquestionsEmail>\r\n";
$confirmationEmailExtraHeaders .="MIME-Version: 1.0\r\n";
$confirmationEmailExtraHeaders .="Organization: GotQuestions\r\n";
$confirmationEmailExtraHeaders .="Content-Language: en\r\n";
$confirmationEmailExtraHeaders .="Content-type: multipart/mixed; boundary=$mimeBoundary; charset=iso-8859-1\r\n";
$confirmationEmailExtraHeaders .="Content-Transfer-Encoding: 7bit\r\n";
$confirmationEmailExtraHeaders .="If you are reading this, then your e-mail client does not support MIME.\r\n";
$confirmationEmailExtraHeaders .="$mimeBoundary";
$confirmationEmailExtraHeaders .="Content-type: text/plain; boundary=$mimeBoundary" . "2\r\n";
$confirmationEmailExtraHeaders .="Content-Transfer-Encoding: 7bit\r\n";
$confirmationEmailExtraHeaders .="$confirmationEmailTXT\r\n";
$confirmationEmailExtraHeaders .="$mimeBoundary" . "2\r\n";
$confirmationEmailExtraHeaders .="Content-type: text/html; boundary=$mimeBoundary" . "3\r\n";
$confirmationEmailExtraHeaders .="Content-Transfer-Encoding: 7bit\r\n";
$confirmationEmailExtraHeaders .="$confirmationEmailHTML\r\n";
$confirmationEmailExtraHeaders .="$mimeBoundary" . "3\r\n";
##########################
## New Question Email ##
##########################
$newEmailQuestionTo = "$gotquestionsEmail";
$newEmailQuestionSubject = "I've Got a Question!";
$newEmailQuestionBody = "Email Address: $emailAddress\n\n\n" .
"Question: $question";
#######################
## Send the emails ##
#######################
if ($newEmailQuestion = mail("$newEmailQuestionTo",
"$newEmailQuestionSubject",
"$newEmailQuestionBody",
"$newEmailQuestionExtraHeaders")){
##################################################
## If the question was submitted successfully ##
## Then send a confirmation email ##
##################################################
#there is a bug here
#there should be a check to make sure the confirmation email sent
$confirmationEmail = mail("$confirmationEmailTo",
"$confirmationEmailSubject",
"$confirmationEmailHTML",
"$confirmationEmailExtraHeaders");
#Do a a meta refresh = 0 to the success page!
echo "<META HTTP-EQUIV=Refresh CONTENT=\"$refresh; URL=confirm.html\">";
}//End newEmailQuestion
#this part is a bug
#if the emails failed it will bomb here in a big confusing way
#leaving confusing errors on a confusing screen, with a confused user
#staring at the screen in confusion
#confused?
#oh ya, and the question they typed so tirelessly might be lost if the browser they use
#doesn't keep it when they hit the back button (most do now)
}else{
$error = "$Domain doesn\'t seem to have a mail server. Please double check your email address.";
require 'Bible-Questions.php';
exit;//The script should end here anyway. Just in case we'll force it.
}//End getmxrr
}else{
####################################
## If none of the above applies ##
## just print the form ##
####################################
print "<form action='newquestion.php' name='emailchecker' method='post'>
Email address: <input type='text' value='$emailAddress' name='emailaddress'>
<br>
Question: <br><br>
<textarea rows='6' cols='34' name='question'>$question</textarea>
<input type=submit>
</form>
";
}//End isset
?>