Skip to content

Instantly share code, notes, and snippets.

@DraftProducts
Created January 23, 2018 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DraftProducts/f1c2a2045634af808f6ed965f7bc0cc6 to your computer and use it in GitHub Desktop.
Save DraftProducts/f1c2a2045634af808f6ed965f7bc0cc6 to your computer and use it in GitHub Desktop.
Contact Page to send mail with files with PHP
<?php
if( !defined( 'PHP_EOL') ) {
if( strtoupper( substr( PHP_OS, 0, 3 ) ) == 'WIN' ) {
define( 'PHP_EOL', "\r\n" );
} else {
define( 'PHP_EOL', "\n" );
}
}
if( isset( $_POST['mailform'])) {
$_err = array();
if(!empty($_POST['objet']) && !empty($_POST['author']) && !empty($_POST['email']) && !empty($_POST['message'])) {
$contenuHtml = '<html>
<body>
<table style="width:100%;">
<tr>
<td colspan="2">
Vous avez reçu un message le ' . date( 'd/m/Y' ) . ' à ' . date( 'H:i:s' ) . '<br>
<hr>
</td>
</tr>
<tr>
<td colspan="2">' . nl2br( $_POST['message'] ) . '</td><br>
</tr>
<tr>
<td colspan="2">Mail : ' . nl2br( $_POST['email'] ) . '</td>
</tr><br>
<tr>
<td colspan="2">Cordialement ' . nl2br( $_POST['author'] ) . '.</td>
</tr>
</table>
</body>
</html>';
move_uploaded_file( $_FILES['fichier']['tmp_name'], 'uploads/' . $_FILES['fichier']['name'] );
$contenuFic = file_get_contents( 'uploads/' . $_FILES['fichier']['name'] );
$splitBase64Fic = chunk_split( base64_encode( $contenuFic ), 70, PHP_EOL );
unlink( 'uploads/' . $_FILES['fichier']['name'] );
$boundary = '-----=' . md5( uniqid( microtime(), TRUE ) );
$boundary2 = '-----=' . md5( uniqid( microtime(), TRUE ) );
$headers = 'From: mail@exemple.fr' . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Priority: normal' . PHP_EOL;
$headers .= 'Reply-To: contact@localhost' . PHP_EOL;
$headers .= 'X-Confirm-Reading-To: contact@localhost' . PHP_EOL;
$headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
$headers .= 'X-Priority: 3' . PHP_EOL;
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"" . PHP_EOL;
$message = PHP_EOL . '--' . $boundary . PHP_EOL;
$message .= "Content-Type: multipart/alternative; boundary=\"$boundary2\"" . PHP_EOL;
$message .= PHP_EOL . '--' . $boundary2 . PHP_EOL;
$message .= 'Content-Type: text/plain; charset=utf-8' . PHP_EOL;
$message .= 'Content-Transfer-Encoding: quoted-printable' . PHP_EOL;
$message .= PHP_EOL . $contenuTxt . PHP_EOL;
$message .= PHP_EOL . '--' . $boundary2 . PHP_EOL;
$message .= 'Content-Type: text/html; charset=utf-8' . PHP_EOL;
$message .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL;
$message .= PHP_EOL . $contenuHtml . PHP_EOL;
$message .= PHP_EOL . '--' . $boundary2 . '--' . PHP_EOL;
$message .= PHP_EOL . '--' . $boundary . PHP_EOL;
$message .= 'Content-Type : ' . $_FILES['fichier']['type'] . '; name=' . $_FILES['fichier']['name'] . PHP_EOL;
$message .= 'Content-Transfer-Encoding: base64' . PHP_EOL;
$message .= 'Content-Disposition: attachment; filename=' . $_FILES['fichier']['name'] . PHP_EOL;
$message .= PHP_EOL . $splitBase64Fic . PHP_EOL . PHP_EOL;
$message .= PHP_EOL . '--' . $boundary . '--' . PHP_EOL;
if( mail( "mail@exemple.com", nl2br( $_POST['objet'] ), $message, $headers ) ){
$_err = array( 'code'=>'done', 'msg'=>array( '<span class="titre-action vert">Message envoyé !</span><br>' ) );
}else{
$_err['code'] = 'incomplete';
$_err['msg'][] ='<span class="titre-action rouge">Échec de l\'envoi !<br />Veuillez nous excuser pour ce désagrément.</span><br>';
}
}else{
$_err['code'] = 'incomplete';
$_err['msg'][] ='<span class="titre-action rouge">Envoi interrompu !<br />Veuillez completer tous les champs.</span><br>';
}
}
?>
<html>
<head>
<title>Contact Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form id="formulaire" method="POST" action="#envoie" data-role="formulaire" enctype="multipart/form-data" name="mailform">
<h2>Formulaire de Contact</h2>
<input maxlength="50" name="author" type="text" placeholder="Votre nom" value="<?php if(isset($_POST['author'])) { echo $_POST['author']; } ?>" /><br>
<input maxlength="50" name="email" type="email" placeholder="Votre mail" value="<?php if(isset($_POST['email'])) { echo $_POST['email']; } ?>" /><br/>
<input maxlength="50" name="objet" type="text" placeholder="Sujet" value="<?php if( isset( $_POST['objet'])) { echo $_POST['objet']; } ?>"/><br>
<input name="fichier" type="file" /><br>
<textarea name="message" placeholder="Message" rows="4" cols="50" class="message"><?php if(isset($_POST['message'])) { echo $_POST['message']; } ?></textarea><br/>
<?php
if( isset( $_err ) && array_key_exists( 'code', $_err ) ){
if( array_key_exists( 'msg', $_err ) ){
foreach( $_err['msg'] as $value ){
echo $value;
}
}
}
?>
<input data-role="submit" name="mailform" type="submit" value="Envoyer !" /><br><br>
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment