PHP Classes

File: sendmail/class.sendemail.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of MarPlo  >  PHP Contact Form Script  >  sendmail/class.sendemail.php  >  Download  
File: sendmail/class.sendemail.php
Role: Class source
Content type: text/plain
Description: php code to send the mail via GMail
Class: PHP Contact Form Script
Send messages using contact forms
Author: By
Last change: Added code in construct.
Date: 2 years ago
Size: 4,586 bytes


Class file image Download
// class to send email from a contact form, from:
class sendEMail {
$re; // store message returned by this class

public function __construct($to) {
// Check the session that limits to can be send only an e-mail in 5 minutes, also useful anti-refresh
if(isset($_SESSION['limit_contact']) && $_SESSION['limit_contact']>(time()-300)) {
$this->re = 'Error: You can send only a message at 5 minutes.<br />Wait '.($_SESSION['limit_contact']-time()+300).' seconds';

// Check the anti-spamm code
if(isset($_POST['anti_spam']) && isset($_POST['anti_spam1']) && $_POST['anti_spam']==$_POST['anti_spam1']) {
// Check if all necessary data are received by post
if(isset($_POST['nume']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])) {
// removes external whitespace and tags
$_POST = array_map("trim", $_POST);
$_POST = array_map("strip_tags", $_POST);

// sets email data
$cmail['to'] = $to;
$cmail['nume'] = $_POST['nume'];
$cmail['from'] = $_POST['email'];
$cmail['subject'] = $_POST['subject'];
$cmail['body'] = 'E-mail from Contact form, sent by: '. $cmail['nume'] ."\n His /Her e-mail address: ". $cmail['from']. "\n IP: ". $_SERVER['REMOTE_ADDR']. "\n\n"
.'message: '. $_POST['message'];

// calls the method with data to send the email
$this->re = $this->sendMail($cmail);
$this->re = 'Error: not all form fields.';
$this->re = 'Error: Incorrect verification code.';

// method that receives data for mail, and accesses the function to send the email
protected function sendMail($cmail) {
// if GMAIL=1, uses the gmail_sender() function (of the phpmailer class) to send the email via GMail
    // Otherwise, uses the PHP mail() function
if(GMAIL === 1) {
// Calls the gmail_sender() function, and store its response
$send = $this->gmailSender($cmail);
    else {
$cmail['from'] = 'From: '. $cmail['from'];
// uses the PHP mail() function, If sending successfully, set $send='sent', otherwise defines 'Error'
if(mail($cmail['to'], $cmail['subject'], $cmail['body'], $cmail['from'])) $send = 'sent';
$send = 'Error: The server could not send the email.';

// If the email is sent, display the confirmation. Otherwise returns the error
if($send == 'sent') {
// Set a session used to block the re-sending to an eventual refresh or in less than 5 minutes
$_SESSION['limit_contact'] = time();

'<p><b>Your message was successfully sent, will be answered as soon as possible.</b></p>
      <p><b><i>Thank you for your interest.</i></b></p>
              <p>If you wish, visit: <a href=""></a></p>'
    else return

// send email via GMail SMTP, with phpmailer class
protected function gmailSender($cmail) {

//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();

$body = nl2br($cmail['body']);
$body = str_replace('\\','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = ""; // SMTP server
    // $mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
                                               // 2 = errors and messages
                                               // 1 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = ""; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = GMAIL_USER; // GMAIL username
$mail->Password = GMAIL_PASS; // GMAIL password

$mail->SetFrom($cmail['from'], $cmail['nume']);

$mail->AddReplyTo($cmail['from'], $cmail['nume']);

$mail->Subject = $cmail['subject'];

$mail->AddAddress($cmail['to'], "Admin");

$mail->Send()) return 'Error: '. $mail->ErrorInfo;
    else return