Outils pour utilisateurs

Outils du site


Panneau latéral

webdev:php:snippets:basic_mail_form

Basic mail form

Including a honeypot

<?php
if(count($_POST) > 0)
{
	//	echo '<pre>' . print_r($_POST, true) . '</pre>';

	// Send the email
	if( $_POST['plopt'] != '' )
	{
		// Honeypot filled, we don't send anything
		$result = true;
	} else {
		$message = 	"Message du formulaire de contact \r\n" .
					"Téléphone : " . $_POST['phone'] . "\r\n\r\n" .
					$_POST['message'];
		// We can skip the htmlentities($_POST['message']) because we send the message in plain text

		$headers = 	"Content-type: text/plain; charset=utf-8 \r\n" .
					'From: from@lul.con' . "\r\n" .
					'Reply-To: ' . $_POST['email'] . "\r\n" .
					'X-Mailer: PHP/' . phpversion();
		
		$result = mail( "zed@lul.con", "Message du formulaire", $message, $headers );
	}
}
?>
		<?php
			if( isset($result) )
			{
				if( $result == true )
				{
					echo '<div class="mail_confirm">L\'email a bien été envoyé</div>';
				} else {
					echo '<div class="mail_error">Il y a eu une erreur lors de l\'envoi de l\'email.<br>Veuillez réessayer plus tard.</div>';
				}
			}
		?>	

		<form class="mail main-padding" method="POST">
			<label>Email</label><input type="email" name="email" required/><br>
			<label>Téléphone</label><input type="tel" name="phone"/><br>
			<div style="opacity: 0; position: absolute; z-index: -1;"><label>Important</label><input type="text" name="plopt" value=""/><br></div>
			<label>Message</label><textarea name="message" required></textarea><br>
			<label></label><input type="submit" value="Envoyer">
		</form>
webdev/php/snippets/basic_mail_form.txt · Dernière modification: 23/04/2019 20:24 de dolo