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.

If I send it as a normal, non html email, all the $fields in the array are emailed but as one gigantic list, so if maybe I can format the array into two colums this will also do also as html seems to have some serious issues here

darker angel, any help is appreciated as somehow my end there are serious problems, yet my script is fine according to many in here. I'm new to php and all the books i've worked through and got everything working, only this script i've written with loads of help fails. Can you let me see your script please

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 <[email protected]>", "You <[email protected]>", "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.

thank-you, but are you able to help me through it if i get stuck? Like I said all i want it to do is send my form fields which are set-up with .js server-side for validation before submit will work, so it doesn't have to check any data, only POST it. I will see if I can get it to work for me at least

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.