Skip to content

Instantly share code, notes, and snippets.

@AtkinsSJ
Created March 25, 2012 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtkinsSJ/2198095 to your computer and use it in GitHub Desktop.
Save AtkinsSJ/2198095 to your computer and use it in GitHub Desktop.
A basic emailing class I wrote a while back, for sending html/plain emails
<?php
/***********************************************************************
* Emailer class
* -------------
* Copyright Samuel Atkins 2011
* -------------
* Simplifies sending of HTML and plain-text emails.
*
* Usage:
* send( $to : Email address to send to
* $fromName : Display name for sender
* $fromEmail : Email address for sender
* $subject : Subject of the email
* $bodyPlain : Plain text message
* $bodyHtml : HTML message );
* Returns whether sending of the email was successful.
**********************************************************************/
class Emailer
{
private $headers = array();
/**
* Sends an email. See above for more specific documentation.
*
* Returns whether email was sent successfully.
*/
public function send($to, $fromName, $fromEmail, $subject, $bodyPlain, $bodyHtml)
{
$this->_setHeader('From', $fromName.' <'.$fromEmail.'>');
$this->_setHeader('Reply-To', $from);
$boundary = md5(date('r', time()));
$this->_setHeader('Content-Type', 'multipart/alternative; boundary="PHP-alt-'.$boundary.'"');
// Fix newlines?
// $body_plain = str_replace
$headersString = $this->_getHeadersString();
$message = $this->_buildMessage($bodyPlain, $bodyHtml, $boundary);
// Clear the headers, to prevent them potentially accumulating
$this->headers = array();
return @mail($to, $subject, $message, $headersString);
}
/**
* Sets the email header '$name' to the $value.
*/
private function _setHeader($name, $value)
{
$this->headers[$name] = $value;
}
/**
* Combines all the set headers into a header string suitable for email.
*
* Returns that string.
*/
private function _getHeadersString()
{
$str = '';
foreach ($this->headers as $key => $val)
{
$str .= $key . ': ' . $val . "\r\n";
}
return $str;
}
/**
* Constructs the message component for mail(),
* from the $plain and $html messages, and the $boundary.
*
* Returns the complete message.
*/
private function _buildMessage($plain, $html, $boundary)
{
$message = 'This is a multi-part message in MIME format.'."\r\n";
// Plain text
$message .= '--PHP-alt-'.$boundary."\r\n";
$message .= 'Content-Type: text/plain; charset="iso-8859-1"'."\r\n";
$message .= 'Content-Transfer-Encoding: 7bit'."\r\n";
$message .= "\r\n";
$message .= $plain."\r\n\r\n";
// HTML
$message .= '--PHP-alt-'.$boundary."\r\n";
$message .= 'Content-Type: text/html; charset="iso-8859-1"'."\r\n";
$message .= 'Content-Transfer-Encoding: 7bit'."\r\n";
$message .= "\r\n";
$message .= $html."\r\n\r\n";
// End
$message .= '--PHP-alt-'.$boundary."--\r\n";
return $message;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment