2 classes (server, email) instead of 1 #2552
Replies: 3 comments 1 reply
-
I do not like it, its ugly but does what I want... <?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
class MailServerSettings {
public $SMTPDebug = SMTP::DEBUG_SERVER;
public $Host = "localhost";
public $SMTPAuth = false;
public $Username = "";
public $Password = "";
public $SMTPSecure = 'ssl';
public $Port = 465;
}
class Email {
public $to_addresses = [];
public $from_address = "";
public $from_name = "";
public $Subject;
public $Body;
public $Altbody = "";
public $Attachments;
function __construct($subject, $body, $to, $name = '') {
$this->addAddress($to, $name);
$this->Subject = $subject;
$this->Body = $body;
$this->AltBody = $body;
}
function addAddress($address, $name = "") {
$this->to_addresses[$address] = $name;
}
function addAttachment($file) {
$this->Attachments[] = $file;
}
}
class EmailServer extends PHPMailer {
function __construct($exceptions = null, $settings) {
parent::__construct($exceptions);
$this->isSMTP();
$settings_vars = get_class_vars(get_class($settings));
foreach ($settings_vars as $name => $value) {
$this->$name = $settings->$name;
}
}
function send_email($email) {
if ($email->from_address != "")
$this->setFrom($email->from_address, $email->from_name);
else
$this->setFrom($this->Username);
foreach ($email->to_addresses as $to_address => $to_name) {
$this->addAddress($to_address, $to_name);
}
$this->isHTML(true);
$this->Subject = $email->Subject;
$this->Body = $email->Body;
$this->AltBody = $email->AltBody;
$this->send();
}
}
?>
// Above code will be used with a require...
<?php
$server = new EmailServer(false, new MailServerSettings());
$email = new Email("Testing", "Everything", "to_someone@somewhere.org");
$server->send_email($email);
?> |
Beta Was this translation helpful? Give feedback.
-
I recommend SwiftMailer if you want to work that way. I'd be interested to hear about how you would propose to deal with mail servers that require changes in message formatting that you can't know about until sending time. |
Beta Was this translation helpful? Give feedback.
-
I'm glad you've built a wrapper that works the way you like – that's the point of open source. The problem area is SMTPUTF8. It requires very different message formatting and encoding, and you can't know whether a server supports it until you connect, so you can't render the message before you connect. One problem is that you can't start out with an SMTPUTF8 message and then re-encode it because some things are simply incompatible – for example there is no way to have header labels use UTF-8 without SMTPUTF8. |
Beta Was this translation helpful? Give feedback.
-
Dear,
I just downloaded PHPMailer, and yes it does what I need. But I cant say it is practical.
I would expect 2 classes:
1)
class MailServer {
...
}
2)
class Email {
...
}
$email_server = new MailServer("settings...");
$my_email = new Email($to, $from, $subject, $body); // these are the 4 required ones
$my_email->addAddress($to2);
$my_email->addAttachment($file);
etc...
$email_server->send($my_email);
Now for me to use it I will make some encapsulation... But even conceptually an email and email_server are 2 different things. So the should be 2 different classes.
Cheers
ps: if I like my encapsulation I will share it...
Beta Was this translation helpful? Give feedback.
All reactions