Reputation: 1999
I need to send emails when users do particular things like fill in a form, submit a request etc. These happen on different pages.
I know the default use of PHPMailer is as below:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'My Friend');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
Is it possible to use the mail() class without having to re-specify the username, password and message to the mail function each time??
Essentially could you have a fuction like:
sendMail($from, $to, $subject, $body);
Which then passes the variables given to an instance of PHPMailer?
Similar to this question.
Upvotes: 0
Views: 3564
Reputation: 1008
require 'vendor/autoload.php';
class MyMail extends PHPMailer
{
private $_host = 'your stmp server name';
private $_user = 'your smtp username';
private $_password = 'your password';
public function __construct($exceptions=true)
{
$this->Host = $this->_host;
$this->Username = $this->_user;
$this->Password = $this->_password;
$this->Port = 465;
$this->SMTPAuth = true;
$this->SMTPSecure = 'ssl';
$this->isSMTP();
parent::__construct($exceptions);
}
public function sendMail($from, $to, $subject, $body)
{
$this->setFrom($from);
$this->addAddress($to);
$this->Subject = $subject;
$this->Body = $body;
return $this->send();
}
}
$m = new MyMail();
$result = $m->sendMail('[email protected]', '[email protected]', 'Test from script', 'test message from script');
Upvotes: 5