|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>HTML Email mit Dateianhang</title> </head> <body> <form action="mail.php" method="post" enctype="multipart/form-data" name="textmail"> Name: <input type="text" name="name" id="name" /><br /> Email: <input type="text" name="email" id="email" /><br /> Betreff: <input type="text" name="betreff" id="betreff" /><br /> Nachricht: <textarea name="nachricht" id="nachricht" cols="20" rows="2"></textarea><br /> Anhang: <input name="datei" id="datei" type="file" /><br /> Anhang: <input name="datei2" id="datei2" type="file" /><br /> <input name="senden" id="senden" type="submit" value="Formular senden" /> </form> </body> </html> |
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Empfänger Email
$empfaenger = 'giulion@hotmail.de';
// Prüfen ob das Formular abgeschickt wurde
if (isset($_POST['senden']) && $_FILES['datei']['size'] > 0 && $_FILES['datei2']['size'] > 0)
{
// Funktionen einbinden
include( 'funktionen.inc.php' );
// Benutzereingaben bereinigen und auf Injection prüfen
cleanInput();
// Name prüfen
$name = checkName( $_POST['name'] );
// Email prüfen
$email = checkEmail( $_POST['email'] );
// Betreff und Nachricht prüfen
if ((strlen( $_POST['betreff'] ) < 5) || (strlen( $_POST['nachricht'] ) < 5))
{
die( 'Bitte füllen Sie alle Felder aus!' );
}
else
{
$betreff = $_POST['betreff'];
$nachricht = $_POST['nachricht'];
}
// Upload prüfen
$uploadname = checkFile();
$uploadname2 = checkFile2();
// --------------------------------------------------------------------------------
// Wurde das Script bisher nicht abgebrochen, wurde das Formular korrekt ausgefüllt
// --------------------------------------------------------------------------------
// Template mit dem Mailbody laden
$template = file_get_contents( 'mailbody.txt' );
// Trenner für den Anhang
$trenner = md5( time() );
// Platzhalter mit den Benutzereingaben ersetzen
$template = str_replace( '###NAME###', htmlspecialchars( $name ), $template );
$template = str_replace( '###EMAIL###', $email, $template );
$template = str_replace( '###NACHRICHT###', nl2br( htmlspecialchars( $nachricht ) ), $template );
// Mail Header erstellen
$mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
$mailheader .= "Return-Path: noreply@" .$_SERVER['SERVER_NAME']. "\r\n";
$mailheader .= "Message-ID: <" .time(). " noreply@" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n";
$mailheader .= "From: PHP Email Tutorial<noreply@" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: multipart/mixed;\r\n";
$mailheader .= " boundary = " .$trenner;
$mailheader .= "\r\n\r\n";
// Mailbody vorbereiten
$mailbody = "This is a multi-part message in MIME format\r\n";
$mailbody .= "--" .$trenner. "\r\n";
$mailbody .= "Content-Type: text/html; charset=UTF-8\r\n";
$mailbody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$mailbody .= $template. "\r\n\r\n";
// Anhang anfügen
$mailbody .= "--" .$trenner. "\r\n";
$mailbody .= "Content-Type: image/jpeg; name=\"" .$uploadname. "\"\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n";
$mailbody .= "Content-Disposition: attachment; filename=\"" .$uploadname. "\"\r\n\r\n";
$mailbody .= chunk_split( base64_encode( file_get_contents( $_FILES['datei']['tmp_name'] ) ) );
$mailbody .= "\n";
// Anhang anfügen 2
$mailbody .= "--" .$trenner. "\r\n";
$mailbody .= "Content-Type: image/jpeg; name=\"" .$uploadname2. "\"\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n";
$mailbody .= "Content-Disposition: attachment; filename=\"" .$uploadname2. "\"\r\n\r\n";
$mailbody .= chunk_split( base64_encode( file_get_contents( $_FILES['datei2']['tmp_name'] ) ) );
$mailbody .= "\n";
// Email versenden
if (@mail( $empfaenger, htmlspecialchars( $betreff ), $mailbody, $mailheader ))
{
// Bei erfolgreichem Versand Danke-Seite anzeigen
echo 'Danke, die Email wurde verschickt!';
}
}
?>
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Benutzereingabe bereinigen (trimmen, Slashes entfernen)
function cleanInput()
{
checkInjection();
if (get_magic_quotes_gpc()) $_POST = array_map( 'stripslashes', $_POST );
$_POST = array_map( 'trim', $_POST );
}
// Name auf Gültigkeit prüfen
function checkName( $name )
{
$muster_name = '/^([a-zA-ZäÄöÖüÜß\xc0-\xc2\xc8-\xcf\xd2-\xd4\xd9-\xdb\xe0-\xe2\xe8-\xef\xf2-\xf4\xf9-\xfb\x9f\xff\.\'\-_]?(\s)?)+$/';
if (preg_match( $muster_name, $name ))
{
return $name;
}
else
{
die( 'Der eingegebene Name enthält nicht erlaubte Zeichen!' );
}
}
// Email auf korrektes Format prüfen
function checkEmail( $email )
{
$nonascii = "\x80-\xff";
$nqtext = "[^\\\\$nonascii\015\012\"]";
$qchar = "\\\\[^$nonascii]";
$normuser = '[a-zA-Z0-9][a-zA-Z0-9_.-]*';
$quotedstring = "\"(?:$nqtext|$qchar)+\"";
$user_part = "(?:$normuser|$quotedstring)";
$dom_mainpart = '[a-zA-Z0-9][a-zA-Z0-9._-]*\\.';
$dom_subpart = '(?:[a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*';
$dom_tldpart = '[a-zA-Z]{2,5}';
$domain_part = "$dom_subpart$dom_mainpart$dom_tldpart";
$pattern = "$user_part\@$domain_part";
$muster_email = "/^{$pattern}$/";
if (preg_match( $muster_email, $email ))
{
return $email;
}
else
{
die( 'Die eingegebene Email-Adresse hat kein gültiges Format!' );
}
}
// Dateianhang prüfen
function checkFile()
{
if ($_FILES['datei']['error'] == 0 &&
$_FILES['datei']['type'] == 'image/jpeg')
{
return $_FILES['datei']['name'];
}
else
{
die( 'Bitte eine gültige JPG Datei anhängen!' );
}
}
function checkFile2()
{
if ($_FILES['datei2']['error'] == 0 &&
$_FILES['datei2']['type'] == 'image/jpeg')
{
return $_FILES['datei2']['name'];
}
else
{
die( 'Bitte eine gültige JPG Datei anhängen!' );
}
}
// Benutzereingaben auf mögliche Injection prüfen
function checkInjection()
{
$email_injection = array( 'bcc:', 'boundary', 'cc:', 'content-transfer-encoding:', 'content-type:', 'mime-version:', 'subject:' );
// Auf potentielle Email Injections prüfen
foreach ($email_injection as $injection)
{
foreach ($_POST as $feld => $inhalt)
{
if (preg_match( "/{$injection}/i", $inhalt ))
{
header( 'location: http://www.google.com/search?hl=en&q=how+to+become+a+better+hacker' );
exit;
}
}
}
return true;
}
?>
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email als HTML mit Anhang</title>
<style type="text/css">
body { font: normal 12px Verdana, Arial, Helvetica, sans-serif; }
a { color: blue; text-decoration: none; }
h2 { font-size: 16px; font-weight: bold; }
.gruen { color: green; }
</style>
</head>
<body>
<h2>Soeben ist eine Nachricht von <span class="gruen">###NAME###</span> eingetroffen.</h2>
<p>Als Antwortadresse wurde <a href="mailto:###EMAIL###">###EMAIL###</a> angegeben.</p>
<p>Die Nachricht die gesendet wurde lautet:<br />
###NACHRICHT###</p>
</body>
</html>
|

This post has been edited 1 times, last edit by "binneuhier" (Jan 23rd 2010, 7:24pm)