Jump to content

need help with why php script sending as html email fails


stealthmode666

Recommended Posts

I really am stuck and I know this is not impossible but my script runs, yet it only sends a certain amount of fields from my form. Form field names are identical in my script, so no typo errors causing the problem.

I want the form data emailed to me in a table in two columns. I got some help but I need to be taken through it till it works.

Someone mentioned $_POST can have problems in emails but I don't know how to make the predefined variable $_POST as another variable in my script, or where to place it to see if this will work. Please help. I've been asking for days at this and I know someone reading this can help me. I've been through a few php books now and to no avail.

Link to comment
Share on other sites

emailer.class.php

<?php
/**
* Simple E-Mailer
* @copyright 2008 DarkAngelArts
* @author Brandon LaRue <darkerangel (at) gmail (dot) com>
* @version 2.0.0.0
* 
*
This Application runs under the MIT License Copyright (c) 2008 DarkAngelArts

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


class simple_email {
public $from_email, $to_email, $body, $subject, $template, $landing_page, $error_page;

/**
 * Simple Emailer Construct
 *
 * @param string $from Address that email will appear to be from, "Name <email>" format or just "email"
 * @param string $to Address that the email will be sent to in, "Name <email>" format or just "email"
 * @param string $landing_page Page that users will be directed to on successful send.
 * @param string $error_page Page that users will be directed to on the event there is an error.
 * @param string $template [Optional] Loads the template file from the construct.
 * @return simple_email
 */
public function simple_email($from, $to, $landing_page, $error_page, $template = false) {
	$this->to_email = $to;
	$this->from_email = $from;
	$this->landing_page = $landing_page;
	$this->error_page = $error_page;
	if($template) {
		$this->load_template($template);
	}
}

/**
 * Template Loader
 * Loads the template into the class.
 *
 * @param string $template_file File Name of the template file.
 */
public function load_template($template_file) {
	$this->template = file_get_contents($template_file);
	preg_match("#<subject>(.*)</subject>#i", $this->template, $this->subject);
	preg_match("#<body>(.*)</body>#is", $this->template, $this->body);
	$this->subject = $this->subject[1];
	$this->body = $this->body[1];
}

/**
 * Template Data Parser
 * Generates the email based on form method used.
 *
 * @param string $method GET | POST on false uses the $_REQUEST method (not recommended)
 */
public function parse_template($method = false) {
	$method = strtolower($method);
	if($method == "post") {
		foreach($_POST as $key => $value) {
			$this->subject = str_replace('{$'.$key.'}', addslashes(strip_tags($value)), $this->subject);
			$this->body = str_replace('{$'.$key.'}', addslashes(strip_tags($value)), $this->body);
		}
	} elseif($method == "get") {
		foreach($_GET as $key => $value) {
			$this->subject = str_replace('{$'.$key.'}', addslashes(strip_tags($value)), $this->subject);
			$this->body = str_replace('{$'.$key.'}', addslashes(strip_tags($value)), $this->body);

		}
	} else {	
		foreach($_REQUEST as $key => $value) {
			$this->subject = str_replace('{$'.$key.'}', addslashes(strip_tags($value)), $this->subject);
			$this->body = str_replace('{$'.$key.'}', addslashes(strip_tags($value)), $this->body);
		}
	}
}

public function custom_parse($key, $value) {
	$this->subject = str_replace('{$'.$key.'}', $value, $this->subject);
	$this->body = str_replace('{$'.$key.'}', $value, $this->body);
}

/**
 * Sends email and redirects based on settings
 *
 */
public function send_email() {
	if(mail($this->to, $this->subject, $this->body, $this->email_headers())) {
		header("Locarion: {$this->landing_page}");
	} else {
		header("Location: {$this->error_page}");
	}
}

/**
 * Email Checker
 *
 * @param string $email
 * @return bool
 */
public function checkemail($email) {
	if(preg_match('#[a-zA-Z0-9][a-zA-Z0-9\.\-\_]*@[a-zA-Z0-9\-\_]*\.[a-zA-Z0-9\_\.\-]*[a-zA-Z0-9]#', $email))
		return true;
	else
		return false;
}

/**
 * Generates Standard required headers for HTML emails.
 *
 * @return string Header String.
 */
private function email_headers() {
	$headers = array();
	$headers[] = "From: {$this->from_email}";
	$headers[] = "Reply-To: {$this->from_email}";
	$headers[] = "Return-Path: {$this->from_email}";
	$headers[] = "X-Mailer: PHP/".phpversion();
	$headers[] = "MIME-Version: 1.0";
	$headers[] = "Content-type: text/html; charset=iso-8859-1";
	$headers = implode("\r\n", $headers);
	return $headers;
}
}
?>

 

Sample Email Code:

<?php
/**
* Simple Emailer Sample configuration page.
*/
require_once("emailer.class.php");

$emailer = new simple_email("Me <me@mysite.com>", "You <you@yoursite.com>", "email_sent.html", "email_error.html", "email.tpl");
/** class simple_email( Sent_From , Sending_To , Success_Page , Error_Page , Email_Template ) **/

$emailer->parse_template("post");
/** Parses Template based on Post Data (or GET) **/
/**
* ->custom_parse( replace, value );
* This will custom replace {$replace} with value
*/

$emailer->send_email();
/** Sends the Email **/
?>

 

Sample Template

<subject>Thank You {$first_name} for your submission</subject>
<body><a href="http://blah.com">Text</a>
{$some_field}
</body>

 

Its really easy to use all you do is match {$field_name} to the <input name="field_name">

 

It works similar to smarty in a way.

 

You can try it if you want. It's just something i came up with in my spare time, I gave it a MIT license, so feel free to mod it any way you wish.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.