Section B handles the sending of email messages. This is where we will use the freely available PHP Mailer class mentioned in the first article. The first thing it does is to retrieve the user connection details: $query_email = "SELECT * FROM user WHERE user_id = '".$_SESSION $result_email= mysql_query($query_email); $num_email= mysql_num_rows($result_email); if($num_email > 0 ){ while($res = mysql_fetch_array($result_email)){ $email = $res['email']; $user = $res['user_id']; $remuser = $res['remuser']; $rempass = $res['rempass']; $smtp = $res['smtp']; } These details are needed to connect to the mail server. Then we instantiate and fill the PHPMailer class with the user's connection details: $mail = new PHPMailer(); $mail->IsSMTP(); // set $mail->Host = $smtp; // specify main and backup server $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $remuser; // SMTP username $mail->Password = $rempass; // SMTP password //check that the headers are filled $from=$_POST['from']; $cc=$_POST['cc']; $bcc=$_POST['bcc']; $to=$_POST['to']; $subject=$_POST['sub']; $msg=$_POST['msg']; Here, we check to see whether the user wants the message to be saved to the "Sent" folder. If so, we insert the message details in to the sent table: //checkbox if($_POST['save'] !== ""){ $query_sent = "INSERT INTO sent SET to = '$to', from = '$from',subject = '$subject',"; $query_sent .= "msg_body = '$msg', userid = '".$_SESSION['userid']."',cc ='".$_POST['']."',"; $query_sent .="bcc ='".$_POST['bcc']."',date_sent mysql_query($query_sent); } Now we add the message details to the PHPMailer class. I highly recommend that you take a look at the links I provided in the first article to learn more about the PHPMailer class: // put headers in mail object $mail->From = $from; $mail->AddAddress($to); $mail->AddCC($cc); $mail->AddBCC($bcc); $mail->Subject = $subject; $mail->Body = $msg; $mail->AltBody = $msg; $mail->AddReplyTo($from); $mailer->ConfirmReadingTo = $from; Another important aspect I think needs highlighting is the attachment. Here you can see how I get the file into the class by using part of PHP's upload code: if($_FILES['userfile']['name'] !== ""){ $mailer->AddAttachment($_FILES['userfile']['tmp_name'], $_FILES } Finally, we send the message off, using the Send() function. This function is a wrapper for PHP's mail() function. We also make a provision for errors. If any errors occur the user is sent to the results page, which is used to display error messages: if(!$mail->Send()) { $msg2 =$mail->ErrorInfo; header("location:resultpage.php?msg=1&error=$msg2"); exit; }else{ $msg2="Reply sent to $to"; header("location:resultpage.php?msg=2&res=$to"); exit; } } Section C deals with what happens when the "Save As Drafts" button is pressed. The code starts by checking whether that button is pressed, and if so, it inserts the message details into the drafts table. On successful insertion the user is redirected to the results page.
blog comments powered by Disqus |
|
|
|
|
|
|
|