Jump to content

email class behaving strangly


gabrielkolbe

Recommended Posts

hi, I am new to oo, created a simple email class which does not behave.

 

When I run the class (below) so:

if ($mail->sendMail($email, $_POST["subject"], $_POST["body"], $_POST["emailsender"])) {

//to decode the above class it's (sendMail(to_emailaddress, subject, body, from_emailadress_or_name))

$message="Success!!";
	} else {
$error= "There was an error... ";
	}

 

The email does go through BUT I get the error message the whole time, does anyone has any advice on this matter?

 

<?PHP

class email 
{

var $to;
var $from;
var $subject;
var $body;

function validateEmail($testemail){

	$regexp ="^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
	if (eregi($regexp, $testemail)){return true;} else { return false;}

	}



function sendMail( $to, $subject, $body, $from ) {

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers = "From: {$from} \r\n";

if ($this->validateEmail($to) == false)
	{$error =  "email address {$to} is an invalid";} 
		else { 
				if ($this->validateEmail($from) == false)
					{$error = "email address {$from} is an invalid";} 
						else { 
								$body=stripslashes($body);
								$subject=stripslashes($subject);

									if (mail ($to, $subject, $body, $headers))
									{$message =  "Email {$subject} was send"; }

									else { $error = "There was an error in sending an email to {$to}";
									}
									mail ('[email protected]', $subject, $body, $headers);
							}
				}		

print $message;
print $error;

	}	
}

?>

Link to comment
https://forums.phpfreaks.com/topic/41786-email-class-behaving-strangly/
Share on other sites

<?PHP

class email
{

var $to;
var $from;
var $subject;
var $body;

function validateEmail($testemail){

	$regexp ="^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
	if (eregi($regexp, $testemail)){return true;} else { return false;}

}



function sendMail( $to, $subject, $body, $from ) {

	$headers = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
	$headers .= "Content-Transfer-Encoding: 7bit\r\n";
	$headers = "From: {$from} \r\n";

	if ($this->validateEmail($to) == false)
	{$error =  "email address {$to} is an invalid";}
	else {
		if ($this->validateEmail($from) == false)
		{$error = "email address {$from} is an invalid";}
		else {
			$body=stripslashes($body);
			$subject=stripslashes($subject);

			if (mail ($to, $subject, $body, $headers)){return true; }
			else {mail ('[email protected]', $subject, $body, $headers);return false;}

		}
	}

}
}

$mail = new email();
$mail->sendMail($email, $_POST["subject"], $_POST["body"], $_POST["emailsender"]);

?>

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.