RealWorldDevelopers
11/23/2017 - 2:57 AM

PHP Email Function

PHP Email Function

	/**
	 * Send Email via PHP
	 */
	function sendEmail($email_to, $email_from, $email_subject, $email_body)
	{
		
		function clean_string($string)
		{
			$bad = array("content-type","bcc:","to:","cc:","href");
			return str_replace($bad, "", $string);
		}
			
		$email_body .= "Subject: ".clean_string($email_subject)."<br>";
		$email_body .= "From: ".clean_string($email_from)."<br>";
		$email_body .= "Message: ".clean_string($message)."<br>";
					
		// create email headers
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers  = "Content-type: text/plain; charset=utf-8\r\n";
		$headers .= 'From: '.$email_from . "\r\n";
		$headers .= 'Reply-To: '.$email_from . "\r\n";
		$headers .= 'X-Mailer: PHP/' . phpversion();
		$headers .= "X-Sender-IP: ".$_SERVER['REMOTE_ADDR']."\n";
		$headers .= "Date: ".date("r")."\n";

		// send email
		mail($email_to, $email_subject, $email_body, $headers);
	}