php mail sending problems |
To send email using PHP there is one handy function - mail(). I had some troubles with headers, because needed to send e-mails with attachments, and message encoding should be
windows-1257 (its Lithuanian encoding). So I went to php manual page and read all user comments.
I used this piece of code and modified to my needs.
There is final code:
<?php
// Do not use extract() when doing public accessed script
// Read http://lt.php.net/manual/en/function.extract.php WARNING
if (!empty($_POST)) extract($_POST);
// if submitted to send mail
if ($dosend == "send_mail") {
// Define file types
$type = array('gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'swf' => 'application/x-shockwave-flash',
'doc' => 'application/x-msword',
'xls' => 'application/x-msexel',
'all' => '"application/x-unknown-content-type"');
// Some self-explanatory variables
$header = "";
$message = "";
$boundary = '--' . md5(uniqid("yourboundary"));
$priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
$priority = $priorities[2];
// Change to your charset, I use Lithuanian ;)
$charset = "windows-1257";
$ctencoding = "8bit";
$subject = $tema;
$body = $tekstas;
$to = $to;
$from = "from@somewhere.com";
$cc = $cc;
$bcc = $bcc;
$sep = chr(13) . chr(10);
$ctype = $type["all"];
$path[] = "/path/to/temp/dir/where/moved/uploaded/files/" . $fname;
$disposition = "inline";
// if file is attached
if ($fname != "") {
for($i = 0;$i < count($path);$i++) {
$message .= "This is a multi-part message in MIME format.\n--$boundary\n";
$message .= "Content-Type: text/plain; charset=$charset\n";
$message .= "Content-Transfer-Encoding: $ctencoding\n\n" . $body . "\n";
$basename = basename($path[$i]);
$message .= "--$boundary\nContent-type: $ctype;\n name=\"$basename\"\n";
$message .= "Content-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n";
$linesz = filesize($path[$i]) + 1;
$fp = fopen($path[$i], 'r');
$content = chunk_split(base64_encode(fread($fp, $linesz)));
fclose($fp);
$message .= $sep . $content;
}
// If file is not attached, we send simple mail
} else {
$message .= "\n" . $body . "\n";
$message .= $sep . $content;
}
// _________________________________header_____________________
$header .= "From: $from\nX-Priority: $priority\nCC: $cc\nBCC: $bcc\n";
// Headers depend on attachment
if ($fname != "") {
$header .= "Mime-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"$boundary\"\n";
} else {
$header .= "Mime-Version: 1.0\nContent-Type: text/plain; charset=$charset\n";
}
// mailer and encoding headers
$header .= "Content-Transfer-Encoding: $ctencoding\nX-Mailer: azeroMailv1.0\n";
// _________________________________header_____________________
if (mail($to, $subject, $message . "\n", $header)) {
echo "<b>Message successfuly sent</b>";
} else {
echo "<b>Message sending failed!</b>";
}
}
// uploading file (attachment) to server
if ($dosend == "failas") {
$userfile = $_POST['userfile'];
$uploaddir = "/path/to/temp/dir/where/moved/uploaded/files/";
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$fname = basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File succesfuly sent<br>\n";
} else {
echo "Error on sending file!\n";
exit();
}
}
?>
<!-- Mail sending form -->
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];
?>">
<table border=0 align="center">
<tr align="left">
<td>To:</td>
<td><input type="text" name="to" value="" size="107"/></td>
</tr>
<tr align="left">
<td>CC:</td>
<td><input type="text" name="cc" value="" size="107"/></td>
</tr>
<tr align="left">
<td>BCC:</td>
<td><input type="text" name="bcc" value="" size="107"/></td>
</tr>
<tr align="left">
<td>Subject:</td>
<td><input type="text" name="tema" value="" size="107"/></td>
</tr>
<tr align="left">
<td>Message Body:</td>
<td><textarea name="tekstas" rows="20" cols="80"></textarea></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="action" value="e-mail" />
<input type="reset" value="Reset"/>
<input type="hidden" name="dosend" value="send_mail"/>
<input type="hidden" name="fname" value="<?php echo $fname?>"/>
<br>
<br>
</form>
</td>
</tr>
<tr align="left">
<td>File:</td>
<td>
<?php
// if file is attached, just write its name, else - show form
if ($fname != "") {
echo("Attachment: $fname");
} else {
echo("<form enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\" method=\"POST\">
<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"50000\" />
<input name=\"userfile\" type=\"file\" /><input type=\"submit\" name=\"action\" value=\"Attach\" />
<input type=\"hidden\" name=\"dosend\" value=\"failas\" />
</form>");
}
?>
</td>
</tr>
</table>
There is no multi-attachment function (I didn't need it) and there is no unattach function. Everything else works fine.
P.S. Do not use it for spam sending, I hate spam. Thank you ;)




