Skip to content

Instantly share code, notes, and snippets.

@kenng
Last active February 27, 2019 06:12
Show Gist options
  • Select an option

  • Save kenng/d2a647e4e021962c1fcff55f8ce0e042 to your computer and use it in GitHub Desktop.

Select an option

Save kenng/d2a647e4e021962c1fcff55f8ce0e042 to your computer and use it in GitHub Desktop.
php mail function
<?php
class IwMail {
protected $subject;
protected $headers;
protected $content;
protected $to;
protected $from;
public $mail;
public function __construct($subject, $to, $content="", $from="Admin <admin@gmail.com>") {
$this->subject = $subject;
$this->to = $to;
$this->content = $content;
$this->from = $from;
}
public function mail() {
$this->headers = 'From: ' . $this->from . " \r\n" .
'Reply-To: ' . $this->to . " \r\n" .
'X-Mailer: PHP/' . phpversion();
echo "header: " . $this->headers;
$ret = mail($this->to, $this->subject, $this->content, $this->headers);
echo "status($ret): email sent to " . $this->to;
}
public function gmail_setup($username="", $password="") {
$this->headers = "MIME-Version: 1.0\n"
. "Content-type:text/html;charset=iso-8859-1\n"
. "From: Admin <" . $this->from . ">\n";
$mail = new PHPMailer(); // create a new object
// TODO: if username and password is empty, read from config file
$mail->Username = $username;
$mail->Password = $password;
$mail->IsHTML(true);
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
}
public function mail_google($attachment=null, $username="", $password="") {
$mail = $this->mail;
$this->gmail_setup($username, $password);
$mail->SetFrom($this->from);
$mail->Subject = $this->subject;
$mail->Body = $this->content;
$mail->AddAddress($this->to);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
if(!is_null($attachment)) {
unlink($attachment);
}
echo "Message has been sent";
}
}
public function dump() {
echo "to: " . $this->to;
echo "subject: " . $this->subject;
echo "content: " . $this->content;
echo "header: " . $this->headers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment