Jump to content

OOP email system


141n

Recommended Posts

Hey guys, I'm curently developing a site that is gonna use the php mail function for different stuff, like report faults, repot abuse, general queries. What I was wondering is, would it be possible to have the one email page (say mail.php) that would use a php require() to include whatever file is needed depending on what the user selected?

 

i.e, if the user clicks "Report A Fault" then it would got to mail.php and this page would then require('../src/report_fault.php'); likewise for the other possibilities.

 

Problem is, I'm not too sure how to do this. I was wondering if I could set a variable like $form = '#whatever user selected' when they click the hyperlink and depending on what this variable was set to, then it would open the relevant file?

 

I thought about if I could use $_GET[''] and set the url to something like mail.php?action=report_fault - I've seen stuff like that done before, but have no idea how to do this... can someone please help  ??? ???

Link to comment
Share on other sites

You could do something like this:

<?php
class Mailer
{
function send($to, $from, $subject, $body)
{
	$headers = "From: {$from}\r\n";
	$headers .= 'X-Mailer: PHP/'.phpversion();

	return mail($to, $subject, $body, $headers);
}
}

class FaultReport extends Mailer
{
function __construct($error_id, $error_msg, $file, $line)
{
	$time = date('r');
	$body = <<<EOF
Error [{$error_id]}: {$error_msg}
in {$file} on line {$line}
Time: {$time}
EOF;

	$this->send("admin@example.com", "no-reply@example.com", "Error [{$error_id}]", $body);
}
}

class AbuseReport extends Mailer
{
function __construct($reason, $reporter_uid, $offender_uid)
{
	$body = <<<EOF
User UID {$reporter_uid} reported user UID {$offender_uid} with the following reason:
{$reason}
EOF;

	$this->send("admin@example.com", "no-reply@example.com", "Abuse report", $body);
}
}

new FaultReport(38, "Test Error", __FILE__, __LINE__);

new AbuseReport("Just testing...", 5, 108);
?>

 

Just call the class you want for the type of mail that should be sent.

Link to comment
Share on other sites

:D

 

mate, I took one look at your app and thought "....wtf  ???" lol - I didn't think it would be so complicated as this. I'm very n00b15H with OOP. I'm gonna start gearing my code towards it though - it seems the sensible thing to do for stuff like this

 

Been scouring around this site and others on the net. I'm sure once I get my heard around the logic of it, it'll be easy enough, thanks - I can now pick away at your example as my first proper OOP application :D

Link to comment
Share on other sites

Most of it I can understand, I'm just trying to break away from my bad habit of coding everything using "If()" statements  ::)

 

Is there any particularly good literature I could read up on? I started of learning from Larry Ullman's PHP & MySQL but it wasn't too OOP geared.

 

EDIT:

 

Everything up to the class for ReportFault I haev a fair idea of whats going on - after that it is pretty sketchy. Bearing in mind I have only started looking at OOP the last few days

Link to comment
Share on other sites

mate, I took one look at your app and thought "....wtf  ???" lol - I didn't think it would be so complicated as this. I'm very n00b15H with OOP.

 

Hmm...I see 3 "computer lingo" terms in once sentence...this scares me.

 

On another note, if you go to google and type: OOP mail() you will find some excellent tutorials on using OOP and mail() together, plus it breaks it down.

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.