|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Mail attachment problems
Can anyone out there work out why this code won't work with attachments? It'll post mail without attachments though.
Code: $form = " <form action="formmd.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="seenform" value="y"> <b>Email the Committee</b><br><br> <table border=0 align=center cellpadding=0 cellspacing=0 marginwidth=0 marginheight=0 width=100% bgcolor=#996699> <tr> <td valign=top width=15%>Your Name: </td> <input type="hidden" name="name" value="$user[1] $user[3]"> <td valign=top>$user[1] $user[3]</td> </tr> <tr> <td valign=top>Your Email: </td> <input type="hidden" name="email" value="$login@wycliffe.co.uk"> <td valign=top>$login@wycliffe.co.uk</td> </tr> <tr> <td valign=top>Subject: </td> <td valign=top><input type="text" name="subject" size="50" maxlength="50" value="$subject"></td> </tr> <tr> <td valign=top>Message: </td> <td valign=top><textarea name="message" rows="10" cols="45"></textarea></td> </tr> <tr> <td valign=top>Picture:</td> <td valign=top><input type=file name="attachment"></td> </tr> <tr> <td colspan=2><input type="submit" style="background-color:transparent" value="Go Mail!"></td> </tr> </table> </form>"; if ($seenform != "y") : print($form); else: // check for valid file if attachment exists if ($attachment_name && $attachment_size <= 0) { echo "Bad Attachment<br>"; } // build message headers $headers = "From: $name <$email>"; // if attachments exist if($attachment_name || sizeof($amsg) > 0) { // create a MIME boundary string $boundary = "=_" . md5(uniqid(time())) . "_="; echo "$boundary"; // add MIME data to the message headers $headers .= "MIME-Version:1.0n"; $headers .= "Content-Type: multipart/mixed; tboundary="$boundary"nn"; // start building a MIME message // first part is always the message body // encode as 7-bit text $str = "--" . $boundary . "n"; $str .= "Content-Type: text/plain;tcharset="us-ascii"n"; $str .= "Content-Transfer-Encoding: 7bitn"; $str .= "$messagenn"; // if an uploaded attachment exists // encode it and attach it as a MIME-encoded section if ($attachment_name) { $fp = fopen($attachment, "rb"); $data = fread($fp, filesize($attachment)); $data = chunk_split(base64_encode($data)); fclose($fp); // add the MIME data $str .= "--" . $boundary . "n"; $str .= "Content-Type: " . $attachment_type . ";tname="" . $attachment_name . ""n"; $str .= "Content-Transfer-Encoding: base64n"; $str .= "Content-Disposition: attachment; tfilename="" . $attachment_name . ""nn"; $str .= $data . "n"; } // all done // add the final MIME boundary $str .= "n--$boundary--n"; // assign the contents of $str to $body // note that the original contents of $body will be lost $message = $str; } // send out the message $to = "yb@wycliffe.co.uk"; echo "To: $to"; echo "Subject: $subject"; echo "Final Message: $message"; echo "Final Headers: $headers"; $mail = mail($to, $subject, $message, $headers); if(mail($to, $subject, $message, $headers)) { $status = "Your message was successfully sent."; } else { $status = "An error occurred while sending your message."; } echo "$status<br>"; echo "$mail<br>"; endif; The variable $user is read from a MYSQL database. The variable $login is passed by the script which accesses this one. The script is called formmd.php and calls itself once the submit button is clicked. Thanks, John |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Mail attachment problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|