email sending function with email address validator. sends HTML email, so
you can style your email message with inline styles and everything you can do to
a webpage.
01<?php 02 03// checks the syntax of the emailaddress 04functioncheck_email($emaildddress) 05{ 06$goodchars='^[A-Za-z0-9\._-]+@([A-Za-z][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$'; 07 08$isvalid=true; 09if(!ereg($goodchars,$emaildddress)) 10{ 11$isvalid=false; 12} 13return$isvalid; 14} 15 16// sends the HTML email 17functionhtml_email($recipient,$subject,$message,$from,$replyto) 18{ 19$array=array("\'"=>"'"); 20$message=strtr($message,$array); 21$message='<html><body>'.$message.'</body></html>'."\r\n\r\n"; 22$extra='From: '.$from.' <'.$replyto.'>'."\r\n"; 23$extra.='Content-Type: text/html; charset="ISO-8859-1"'."\r\n"; 24$extra.='Content-Transfer-Encoding: quoted-printable'."\n\r\n"; 25mail($recipient,$subject,$message,$extra); 26} 27 28// use like this 29// specify the address the mail will be sent from 30$mail_sender='sender@example.com'; 31// specify the name of the sender 32$mail_sender_name='example sender'; 33// specify the recipient email address 34$mail_recipient='recipient@example.com'; 35// specify the subject of the email (no HTML here!) 36$mail_subject='HTML email test'; 37// the body of the email - you can use HTML here 38$mail_body=' 39<h1 style="color: red;">Hello there</h1> 40<p>this email should be styled HTML...</p>'; 41 42// if email addresses are valid 43if(check_email($mail_recipient)&&check_email($mail_sender)) 44{ 45// send HTML mail with PHP 46html_email($mail_recipient,$mail_subject,$mail_body,$mail_sender_name,$mail_sender); 47echo' 48 <p>the email "'.$mail_subject.'" was successfully sent to '.$mail_recipient.'</p>'; 49}else{ 50echo' 51 <p>invalid email address - email was not sent</p>'; 52} 53 54?>