This line attempts to send an email using the $smtp object.....
$mail = $smtp->send($email_to, $email_subject, $email_content);
These lines create the $smtp object.
$smtp = Mail::factory("smtp",
array(
"host" => $server,
"username" => $username,
"password" => $password,
"auth" => true,
"port" => $port
)
);
The problem is, in your code, the line that uses the $smtp object comes before the lines that actually create this object. Meaning the object doesn't even exist when you attempt to use it. So, the first line needs to go after the second series of lines.