13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sending Mail<br />

683<br />

Clicking the Send Message button invokes the ‘send-message’ action, which executes<br />

the following code:<br />

case 'send-message':<br />

if(send_message($to, $cc, $subject, $message)) {<br />

echo "Message sent.";<br />

} else {<br />

echo "Could not send message.";<br />

}<br />

This code calls the send_message() function, which actually sends the mail.This function<br />

is shown in Listing 29.12.<br />

Listing 29.12 send_message() Function from mail_fns.php—This Function Sends<br />

the Message That the User Has Typed In<br />

function send_message($to, $cc, $subject, $message) {<br />

// send one email via <strong>PHP</strong><br />

if (!$conn=db_connect()) {<br />

return false;<br />

}<br />

$query = "select address from users where<br />

username='".$_SESSION['auth_user']."'";<br />

$result = $conn->query($query);<br />

if (!$result) {<br />

return false;<br />

} else if ($result->num_rows==0) {<br />

return false;<br />

} else {<br />

$row = $result->fetch_object();<br />

$other = 'From: '.$row->address;<br />

if (!empty($cc)) {<br />

$other.="\r\nCc: $cc";<br />

}<br />

if (mail($to, $subject, $message, $other)) {<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

}<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!