Hi! I have builded (with code found in internet and with code writted for me) 2 script for sending email.
FORM:
<html>
<head>
<title>Modulo per email</title>
<script language="javascript" type="text/javascript">
<!--
function Modulo() {
if ((document.section.nome.value == "")) {
alert("Il campo Nome è obbligatorio.");
document.section.nome.focus();
return false;
}
else if ((document.section.email.value == "")) {
alert("Il campo Email è obbligatorio.");
document.section.email.focus();
return false;
}
else if ((document.section.messaggio.value == "")) {
alert("Il campo Messaggio è obbligatorio.");
document.section.messaggio.focus();
return false;
}
else if ((document.section.attachment.value == "")) {
alert("Il campo X è obbligatorio.");
document.section.messaggio.focus();
return false;
}
else {
document.section.action = "formail.php";
document.section.submit();
}
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<div align="center">
<form method="post" name="section" action="formail.php">
<table border="0" bgcolor="#EEEEEE" cellspacing="2" cellpadding="2">
<tr>
<td>Nickname:</td>
<td><input type="text" size="56" name="nome" maxlength="30"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" size="56" name="email" maxlength="30"></td>
</tr>
<tr>
<td>Oggetto:</td>
<td><input type="text" size="56" value="help" name="oggetto" readonly></td>
</tr>
<tr>
<td>Descrizione:</td>
<td><textarea rows="25" cols="70" name="messaggio"></textarea></td>
</tr>
<tr>
<td>Attach:</td>
<td><input type="file" size="71" name="attachment"></td>
</tr>
<tr>
<td> </td>
<td> <input type="button" value="Invia" onClick="Modulo()"> <input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
<br>
<font size="1" face="verdana"><b>Powered By HELP ME</a></b></font>
</div>
</body>
</html>
The script for sending mail is:
<?php
$webMaster = "mail@mail.it";
$flStrip = (ini_get("magic_quotes_sybase") != true && get_magic_quotes_gpc() == true) ? true : false;
if ($flStrip)
{
$nome = trim(stripslashes($POST['nome']));
$email = trim(stripslashes($POST['email']));
$oggetto = trim(stripslashes($POST['oggetto']));
$messaggio = trim(stripslashes($POST['messaggio']));
}
else
{
$nome = trim($POST['nome']);
$email = trim($POST['email']);
$oggetto = trim($POST['oggetto']);
$messaggio = trim($POST['messaggio']);
}
$pagina_thanks = "http://www.site.com";
$pagina_error_empty = "http://www.site.org";
$pagina_error_email = "http://www.site.tk";
if (preg_match("|[!#$&'+/-9=?A-Z-~-]+(\.[!#$&'+/-9=?A-Z-~-]+)@[!#$&'+/-9=?A-Z-~-]+(\.[!#$&'*+/-9=?A-Z-~-]+)+$|i", $email))
{
if (strlen($nome) <= 0 || strlen($oggetto) <= 0) header ("Location: $pagina_error_empty");
else
{
$ora = date("H:i:s");
$data = date("d/m/Y");
$headers = "";
$body = "Email inviata il $data alle ore $ora
_______Riepilogo dati:_______
Nickname....................: $nome
Email mittente..............: $email
Oggetto.....................: $oggetto
Descrizione:
$messaggio";
if ($_FILES['attachment']['error'] === UPLOAD_ERR_OK)
{
$data = file_get_contents($_FILES['attachment']['tmp_name']);
if (strlen($data) > 0)
{
$boundary = md5(uniqid(time()));
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n\tboundary=\"$boundary\"\r\n";
$preBody = "This is a multi-part message in MIME format.\r\n\r\n";
$preBody .= "--$boundary\r\n";
$preBody .= "Content-Type: text/plain;\r\n\tcharset=\"iso-8859-1\"\r\n";
$preBody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body = $preBody . $body;
$body .= "\r\n\r\n--$boundary\r\n";
$body .= "Content-Type: " . $_FILES['attachment']['type'] . ";\r\n\t";
$body .= "name: \"" . $_FILES['attachment']['name'] . "\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment;\r\n\t";
$body .= "filename=\"" . $_FILES['attachment']['name'] . "\"\r\n\r\n";
$body .= chunk_split(base64_encode($data));
$body .= "\r\n\r\n--$boundary--\r\n";
}
}
// Seleziona l'entry 'Return-Path' dell'header
ini_set("sendmail_from", $webMaster);
// Invio delle emails
mail($webMaster, "Email dal sito", $body, $headers . "From: \"$nome\" <$email>");
header("Location: $pagina_thanks");
}
}
else header("Location: $pagina_error_email");
?>
So, sending mail work properly but don't attach any file!!!! The mail is testual not in multipart (MIME!!). I receive a mail but the attachment isn't in mail.
this is correctly?
if ($_FILES['attachment']['error'] === UPLOAD_ERR_OK)
Seems to be that is skipped and all code for multipart message is skipped also.
"attachment" is "name" in form:
<input type="file" size="71" name="attachment">
why don't work?!?!
TNX!!!!