PHP MVC CodeIgniter Laravel Core PHP MORE

How send email in PHP MVC with example


In this example we using Views and Controller Structure for send email.

Here is the Controller file hello.php which inside controllers folder

controllers/hello.php

<?php
class Hello extends Controller {
    function __construct() {
		parent::__construct();
	}
	function send_mail(){
		if(isset($_POST['send']))
        {
		$to_email=$_POST['to'];
		$subject=$_POST['subject'];
		$message=$_POST['message'];
			
		$to = $to_email;
        $subject = $subject;
        $txt = $message;
        $headers = "From: admin@gmail.com" . "\r\n" .
        "CC: anymail@example.com";
		mail($to,$subject,$txt,$headers);
		}
        $this->view->render('hello/send_mail');
	}
}
?>

Here is the view file send_mail.php which inside views folder contains the form.

views/hello/send_mail.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="send_mail"> 
	To:<br>
	<input type="email" name="to"><br> 
	Subject:<br>
	<input type="text" name="subject"><br><br>
	Message:
	<textarea rows="4" cols="40" name="message"> </textarea> 
	<input type="submit" name="send">
</form>
</html>
</body>
Path to run : localhost/project_folder_name/view_folder_name/view_filename Example: localhost/mvc/hello/send_mail