Modify the contact form

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
100rk

Re: Hi

Post by 100rk »

I am so sorry - forgot about it and take a look here:

http://wiki.cmsmadesimple.org/pmwiki.ph ... tedModules

and try module 'FeedbackForm' - with all Your needs it will be good point.
Indrek

Hi

Post by Indrek »

I think that I had damaged my CMS somewhere.

I reinstalled my CMS and now your code works very well.

Big big thanks to you. :D

But I have one little problem.

I want to use letters õ ä ö ü in the form, but it shows errors insted.

http://www.balti.ee/blc/index.php?page=Kontakt

Do you now how to fix that?
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm

Modify the contact form

Post by Ted »

Change the encoding for your template to ISO-8859-1. It's at the bottom of the edit template screen.
Indrek

Hi

Post by Indrek »

I did as you sed, but now the õ ä ö ü letters in menu show errors.

http://www.balti.ee/blc/index.php?page=Kontakt
jah
Forum Members
Forum Members
Posts: 147
Joined: Thu Dec 30, 2004 9:09 am

Modify the contact form

Post by jah »

There are a number of requests for encoding and it is not so obvious for everyone what the code should be. Maybe a dropdown list would make it simpler.

E.g like this:

Image
Indrek

Hi

Post by Indrek »

I had errors with letters õ ä ö ü in my contact form, but no errors in menu.

When I changed the encoding to ISO-8859-1 in template page, then I have no errors in contact form but there are errors in menu.

http://www.balti.ee/blc/index.php?page=Kontakt
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm

Modify the contact form

Post by Ted »

jah, that's a really good idea. I think I might do that.
Indrek

Hi

Post by Indrek »

Your solution solved my problem also.
Thanks. :D
SimonSchaufi

Re: Modify the contact form

Post by SimonSchaufi »

this is almost the same file but not encoded in hex and xhtml valid :)

Code: Select all

<?php
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function smarty_cms_function_contact_form_attachment($params, &$smarty) {
	global $gCms;

	if (empty($params['email'])) {
		echo "Missing parameter 'email' - check plugin call";
		return;
	}

	if (isset($_POST["cf_submit"])) {

		$err = '';

		$to = $params['email'];

		$email = '';
		if (!empty($_POST['cf_email'])) {
			$email = trim($_POST['cf_email']);
			if (!sendmail::Validate_email($email)) $err .= '<li>Wrong format of e-mail adress!</li>';
		}
		else $err .= '<li>Where is Your e-mail?</li>';

		$name = '';
		if (!empty($_POST['cf_name'])) $name = trim($_POST['cf_name']);
		else $err .= '<li>Where is Your name?</li>';

		$subject = '';
		if (!empty($_POST['cf_subject'])) $subject = trim($_POST['cf_subject']);
		else $err .= '<li>Where is subject of Your message?</li>';

		$msg_part1 = '';
		if (!empty($_POST['cf_msg_part1'])) $msg_part1 = trim($_POST['cf_msg_part1']);
		else $err .= '<li>Where is part_1 of Your message?</li>';

		$msg_part2 = '';
		if (!empty($_POST['cf_msg_part2'])) $msg_part2 = trim($_POST['cf_msg_part2']);
//		else $err .= '<li>Where is part_2 of Your message?</li>';

		$msg_part3 = '';
		if (!empty($_POST['cf_msg_part3'])) $msg_part3 = trim($_POST['cf_msg_part3']);
		else $err .= '<li>Where is part_3 of Your message?</li>';

		$file_name = '';
		if (!empty($_FILES['cf_file'])) {
			$file_name = "_cf_".$_FILES['cf_file']['name'];
			$dest_file = $gCms->config["uploads_path"]."/".$file_name;
			$try = 0;
			while (file_exists($dest_file)) {
				$file_name = $try."_cf_".$_FILES['cf_file']['name'];
				$dest_file = $gCms->config["uploads_path"]."/".$file_name;
				$try++;
			}
			$file_name = $dest_file;
			if (!move_uploaded_file($_FILES['cf_file']['tmp_name'], $file_name)) {
		        $err .= "<li>Cannot move uploaded file to upload destination.</li>";
			}
		}
		if (!empty($file_name)) {
			if (($_FILES['cf_file']['error'])>0){
	        	$err .= '<li>File is larger then limit 100kB, or there is other error with upload.</li>';
			}
		}
		else $err .= '<li>Where is Your file?</li>';

		if (empty($err)) {
			$message_body = "Message part 1: ".$msg_part1;
			$message_body .= ", message part 2: ".$msg_part2;
			$message_body .= ", message part 3: ".$msg_part3;
			$sendmail = new sendmail();
			$sendmail->SetCharSet("ISO-8859-1");
			$sendmail->from($name,$email);
			$sendmail->to($to);
			$sendmail->subject($subject);
			$sendmail->text($message_body);
			if (!empty($file_name)) $sendmail->attachment($file_name);
			if ($sendmail->send()) {
				echo '<div class="contactForm">';
				echo 'Thank You for Your message.';
				echo '</div>';
			}
			else echo '<ul class="error">Problem with sending e-mail message.</ul>';
			return;
		}
		else {
			echo '<ul class="error">'.$err.'</ul>';
		}
	}

	echo '<div class="contactForm">';
	echo '<form action="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'" method="post" enctype="multipart/form-data">';
	echo '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />';
	echo '<p>Your name</p>';
	echo '<input type="text" name="cf_name" value="'.$name.'" size="50" />';
	echo '<p>Your e-mail</p>';
	echo '<input type="text" name="cf_email" value="'.$email.'" size="50" />';
	echo '<p>Subject</p>';
	echo '<input type="text" name="cf_subject" value="'.$subject.'" size="50" />';
	echo '<p>part_1 of message</p>';
	echo '<input type="text" name="cf_msg_part1" value="'.$msg_part1.'" size="50" />';
	echo '<p>part_2 of message</p>';
	echo '<input type="text" name="cf_msg_part2" value="'.$msg_part2.'" size="50" />';
	echo '<p>part_3 of message</p>';
	echo '<textarea name="cf_msg_part3" cols="50" rows="10">'.$msg_part3.'</textarea>';
	echo '<p>file for upload</p>';
	echo '<input type="file" name="cf_file">';
	echo '<p>';
	echo "<input type=\"checkbox\" onclick=\"document.getElementById('cf_submit').disabled=!document.getElementById('cf_submit').disabled;\" />  ";
	echo 'YES, I agree with BLABLABLABLA</p>';
	echo '<input type="submit" id="cf_submit" name="cf_submit" value="SEND" disabled="disabled" />';
	echo '</form>';
	echo '</div>';
}

function smarty_cms_help_function_contact_form_attachment() {
   ?>
   <h3>What does this do?</h3>
   <p>Simple contact form with attachment. This can be used to allow others to send
   an email message to the address specified.</p>
   <h3>How do I use it?</h3>
   <p>Just insert the tag into your template/page like: <code>{contact_form_attachment email="anybody@anywhere.org"}</code></p>
   <h3>What parameters does it take?</h3>
   <ul>
      <li>email - The email address that the message will be sent to.</li>
   </ul>
   </p>
   <?php
}

function smarty_cms_about_function_contact_form_attachment() {
   ?>
   <p>Version: 1.0</p>
   <?php
}

class sendmail {
	var $emailheader = "";
	var $textheader = "";
	var $textboundary = "";
	var $emailboundary = "";
	var $charset = "";
	var $emailsubject = "";
	var $emailto = "";
	var $attachment = array();
	var $cc = array();
	var $bcc = array();

	function sendmail() {
		$this->textboundary = uniqid(time());
		$this->emailboundary = uniqid(time());
		$this->charset = "ISO-8859-1";
	}

	function SetCharSet($char) {
		$this->charset = $char;
	}

	function Validate_email($email) {
		if (!preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",$email)) {
			return false;
		}
		return $email;
	}

	function from($name,$email) {
		$this->emailheader .= 'From: '.$name.'<'.$email.'>'."\r\n";
	}

	function to($to) {
		$this->emailto = $this->Validate_email($to);
	}

	function cc($cc) {
		$this->cc[] = $cc;
	}

	function bcc($bcc) {
		$this->bcc[] = $bcc;
	}

	function makeMimeMail() {
		if(count($this->cc) > 0) {
			$this->emailheader .= 'Cc: ';
			for($i=0;$i<count($this->cc);$i++) {
				if($i > 0) $this->emailheader .= ',';
				$this->emailheader .= $this->Validate_email($this->cc[$i]);
			}
			$this->emailheader .= "\r\n";
		}

		if(count($this->bcc) > 0) {
			$this->emailheader .= 'Bcc: ';
			for($j=0;$j<count($this->bcc);$j++) {
				if($j > 0) $this->emailheader .= ',';
				$this->emailheader .= $this->Validate_email($this->bcc[$j]);
			}
			$this->emailheader .= "\r\n";
		}
		$this->emailheader .= 'MIME-Version: 1.0'."\r\n";
	}

	function subject($subject) {
		$this->emailsubject = $subject;
	}

	function text($text) {
		$this->textheader .= 'Content-Type: multipart/alternative; boundary="'.$this->textboundary.'"'."\r\n\r\n";
		$this->textheader .= '--'.$this->textboundary."\r\n";
		$this->textheader .= 'Content-Type: text/plain; charset="'.$this->charset.'"'."\r\n";
		$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
		$this->textheader .= strip_tags($text)."\r\n\r\n";
		$this->textheader .= '--'.$this->textboundary."\r\n";
		$this->textheader .= 'Content-Type: text/html; charset="'.$this->charset.'"'."\r\n";
		$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
		$this->textheader .= '<__html></__body>'.$text.'<__body></__html>'."\r\n\r\n";
		$this->textheader .= '--'.$this->textboundary.'--'."\r\n\r\n";
	}

	function attachment($attachfile) {
		if(is_file($attachfile)) {
			$attachment_header = '--'.$this->emailboundary."\r\n" ;
			$attachment_header .= 'Content-Type: application/octet-stream; name="'.basename($attachfile).'"'."\r\n";
			$attachment_header .= 'Content-Transfer-Encoding: base64'."\r\n";
			$attachment_header .= 'Content-Disposition: attachment; filename="'.basename($attachfile).'"'."\r\n\r\n";
			$file['content'] = fread(fopen($attachfile,"rb"),filesize($attachfile));
			$file['content'] = base64_encode($file['content']);
			$file['content'] = chunk_split($file['content'],72);
			$this->attachment[] = $attachment_header.$file['content']."\r\n";
		}
		else {
			die("File doesn't exist'!");
		}
	}

	function send() {
		$this->makeMimeMail();
		$header = $this->emailheader;
		if(count($this->attachment)>0) {
			$header .= 'Content-Type: multipart/mixed; boundary="'.$this->emailboundary.'"'."\r\n\r\n";
			$header .= '--'.$this->emailboundary."\r\n";
			$header .= $this->textheader;
			if(count($this->attachment) > 0) $header .= implode("",$this->attachment);
			$header .= '--'.$this->emailboundary.'--';
		}
		else {
			$header .= $this->textheader;
		}
		$result = mail("$this->emailto",$this->emailsubject,"",$header);
		$this->deletememory();
		return $result;
	}

	function deletememory() {
		unset($this->emailheader);
		unset($this->textheader);
		unset($this->attachment);
	}
}
?>
SimonSchaufi

Re: Modify the contact form

Post by SimonSchaufi »

this is the same file xhtml valid and not encoded

Code: Select all

<?php
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function smarty_cms_function_contact_form_attachment($params, &$smarty) {
	global $gCms;

	if (empty($params['email'])) {
		echo "Missing parameter 'email' - check plugin call";
		return;
	}

	if (isset($_POST["cf_submit"])) {

		$err = '';

		$to = $params['email'];

		$email = '';
		if (!empty($_POST['cf_email'])) {
			$email = trim($_POST['cf_email']);
			if (!sendmail::Validate_email($email)) $err .= '<li>Wrong format of e-mail adress!</li>';
		}
		else $err .= '<li>Where is Your e-mail?</li>';

		$name = '';
		if (!empty($_POST['cf_name'])) $name = trim($_POST['cf_name']);
		else $err .= '<li>Where is Your name?</li>';

		$subject = '';
		if (!empty($_POST['cf_subject'])) $subject = trim($_POST['cf_subject']);
		else $err .= '<li>Where is subject of Your message?</li>';

		$msg_part1 = '';
		if (!empty($_POST['cf_msg_part1'])) $msg_part1 = trim($_POST['cf_msg_part1']);
		else $err .= '<li>Where is part_1 of Your message?</li>';

		$msg_part2 = '';
		if (!empty($_POST['cf_msg_part2'])) $msg_part2 = trim($_POST['cf_msg_part2']);
//		else $err .= '<li>Where is part_2 of Your message?</li>';

		$msg_part3 = '';
		if (!empty($_POST['cf_msg_part3'])) $msg_part3 = trim($_POST['cf_msg_part3']);
		else $err .= '<li>Where is part_3 of Your message?</li>';

		$file_name = '';
		if (!empty($_FILES['cf_file'])) {
			$file_name = "_cf_".$_FILES['cf_file']['name'];
			$dest_file = $gCms->config["uploads_path"]."/".$file_name;
			$try = 0;
			while (file_exists($dest_file)) {
				$file_name = $try."_cf_".$_FILES['cf_file']['name'];
				$dest_file = $gCms->config["uploads_path"]."/".$file_name;
				$try++;
			}
			$file_name = $dest_file;
			if (!move_uploaded_file($_FILES['cf_file']['tmp_name'], $file_name)) {
		        $err .= "<li>Cannot move uploaded file to upload destination.</li>";
			}
		}
		if (!empty($file_name)) {
			if (($_FILES['cf_file']['error'])>0){
	        	$err .= '<li>File is larger then limit 100kB, or there is other error with upload.</li>';
			}
		}
		else $err .= '<li>Where is Your file?</li>';

		if (empty($err)) {
			$message_body = "Message part 1: ".$msg_part1;
			$message_body .= ", message part 2: ".$msg_part2;
			$message_body .= ", message part 3: ".$msg_part3;
			$sendmail = new sendmail();
			$sendmail->SetCharSet("ISO-8859-1");
			$sendmail->from($name,$email);
			$sendmail->to($to);
			$sendmail->subject($subject);
			$sendmail->text($message_body);
			if (!empty($file_name)) $sendmail->attachment($file_name);
			if ($sendmail->send()) {
				echo '<div class="contactForm">';
				echo 'Thank You for Your message.';
				echo '</div>';
			}
			else echo '<ul class="error">Problem with sending e-mail message.</ul>';
			return;
		}
		else {
			echo '<ul class="error">'.$err.'</ul>';
		}
	}

	echo '<div class="contactForm">';
	echo '<form action="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'" method="post" enctype="multipart/form-data">';
	echo '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />';
	echo '<p>Your name</p>';
	echo '<input type="text" name="cf_name" value="'.$name.'" size="50" />';
	echo '<p>Your e-mail</p>';
	echo '<input type="text" name="cf_email" value="'.$email.'" size="50" />';
	echo '<p>Subject</p>';
	echo '<input type="text" name="cf_subject" value="'.$subject.'" size="50" />';
	echo '<p>part_1 of message</p>';
	echo '<input type="text" name="cf_msg_part1" value="'.$msg_part1.'" size="50" />';
	echo '<p>part_2 of message</p>';
	echo '<input type="text" name="cf_msg_part2" value="'.$msg_part2.'" size="50" />';
	echo '<p>part_3 of message</p>';
	echo '<textarea name="cf_msg_part3" cols="50" rows="10">'.$msg_part3.'</textarea>';
	echo '<p>file for upload</p>';
	echo '<input type="file" name="cf_file">';
	echo '<p>';
	echo "<input type=\"checkbox\" onclick=\"document.getElementById('cf_submit').disabled=!document.getElementById('cf_submit').disabled;\" />  ";
	echo 'YES, I agree with BLABLABLABLA</p>';
	echo '<input type="submit" id="cf_submit" name="cf_submit" value="SEND" disabled="disabled" />';
	echo '</form>';
	echo '</div>';
}

function smarty_cms_help_function_contact_form_attachment() {
   ?>
   <h3>What does this do?</h3>
   <p>Simple contact form with attachment. This can be used to allow others to send
   an email message to the address specified.</p>
   <h3>How do I use it?</h3>
   <p>Just insert the tag into your template/page like: <code>{contact_form_attachment email="anybody@anywhere.org"}</code></p>
   <h3>What parameters does it take?</h3>
   <ul>
      <li>email - The email address that the message will be sent to.</li>
   </ul>
   </p>
   <?php
}

function smarty_cms_about_function_contact_form_attachment() {
   ?>
   <p>Version: 1.0</p>
   <?php
}

class sendmail {
	var $emailheader = "";
	var $textheader = "";
	var $textboundary = "";
	var $emailboundary = "";
	var $charset = "";
	var $emailsubject = "";
	var $emailto = "";
	var $attachment = array();
	var $cc = array();
	var $bcc = array();

	function sendmail() {
		$this->textboundary = uniqid(time());
		$this->emailboundary = uniqid(time());
		$this->charset = "ISO-8859-1";
	}

	function SetCharSet($char) {
		$this->charset = $char;
	}

	function Validate_email($email) {
		if (!preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",$email)) {
			return false;
		}
		return $email;
	}

	function from($name,$email) {
		$this->emailheader .= 'From: '.$name.'<'.$email.'>'."\r\n";
	}

	function to($to) {
		$this->emailto = $this->Validate_email($to);
	}

	function cc($cc) {
		$this->cc[] = $cc;
	}

	function bcc($bcc) {
		$this->bcc[] = $bcc;
	}

	function makeMimeMail() {
		if(count($this->cc) > 0) {
			$this->emailheader .= 'Cc: ';
			for($i=0;$i<count($this->cc);$i++) {
				if($i > 0) $this->emailheader .= ',';
				$this->emailheader .= $this->Validate_email($this->cc[$i]);
			}
			$this->emailheader .= "\r\n";
		}

		if(count($this->bcc) > 0) {
			$this->emailheader .= 'Bcc: ';
			for($j=0;$j<count($this->bcc);$j++) {
				if($j > 0) $this->emailheader .= ',';
				$this->emailheader .= $this->Validate_email($this->bcc[$j]);
			}
			$this->emailheader .= "\r\n";
		}
		$this->emailheader .= 'MIME-Version: 1.0'."\r\n";
	}

	function subject($subject) {
		$this->emailsubject = $subject;
	}

	function text($text) {
		$this->textheader .= 'Content-Type: multipart/alternative; boundary="'.$this->textboundary.'"'."\r\n\r\n";
		$this->textheader .= '--'.$this->textboundary."\r\n";
		$this->textheader .= 'Content-Type: text/plain; charset="'.$this->charset.'"'."\r\n";
		$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
		$this->textheader .= strip_tags($text)."\r\n\r\n";
		$this->textheader .= '--'.$this->textboundary."\r\n";
		$this->textheader .= 'Content-Type: text/html; charset="'.$this->charset.'"'."\r\n";
		$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
		$this->textheader .= '<__html></__body>'.$text.'<__body></__html>'."\r\n\r\n";
		$this->textheader .= '--'.$this->textboundary.'--'."\r\n\r\n";
	}

	function attachment($attachfile) {
		if(is_file($attachfile)) {
			$attachment_header = '--'.$this->emailboundary."\r\n" ;
			$attachment_header .= 'Content-Type: application/octet-stream; name="'.basename($attachfile).'"'."\r\n";
			$attachment_header .= 'Content-Transfer-Encoding: base64'."\r\n";
			$attachment_header .= 'Content-Disposition: attachment; filename="'.basename($attachfile).'"'."\r\n\r\n";
			$file['content'] = fread(fopen($attachfile,"rb"),filesize($attachfile));
			$file['content'] = base64_encode($file['content']);
			$file['content'] = chunk_split($file['content'],72);
			$this->attachment[] = $attachment_header.$file['content']."\r\n";
		}
		else {
			die("File doesn't exist'!");
		}
	}

	function send() {
		$this->makeMimeMail();
		$header = $this->emailheader;
		if(count($this->attachment)>0) {
			$header .= 'Content-Type: multipart/mixed; boundary="'.$this->emailboundary.'"'."\r\n\r\n";
			$header .= '--'.$this->emailboundary."\r\n";
			$header .= $this->textheader;
			if(count($this->attachment) > 0) $header .= implode("",$this->attachment);
			$header .= '--'.$this->emailboundary.'--';
		}
		else {
			$header .= $this->textheader;
		}
		$result = mail("$this->emailto",$this->emailsubject,"",$header);
		$this->deletememory();
		return $result;
	}

	function deletememory() {
		unset($this->emailheader);
		unset($this->textheader);
		unset($this->attachment);
	}
}
?>
SimonSchaufi

Re: Modify the contact form

Post by SimonSchaufi »

Locked

Return to “CMSMS Core”