Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/recipe-fiddle.git
<?php
namespace com\whitemagicsoftware;

require "login.php";
require "html2text.php";
require "mailgun/vendor/autoload.php";
require "swift/swift_required.php";

use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;
use Swift_TransportException;

use Mailgun\Mailgun;

/**
 * Responsible for sending email notifications.
 */
class Mail extends Obj {
  public function __construct() {
  }

  /**
   * Sends the HTML content to the given recipient; this strips the
   * HTML into a plaintext document and to send both plain & HTML data.
   * The subject line should be data-driven so that it can use the
   * DEFAULT_EMAIL_SUBJECT text.
   *
   * @param $recipient - Destination address to receive the content.
   * @param $subject - Email subject line.
   * @param $html - Formatted content to send.
   *
   * @return false - The email could not be sent.
   */
  public function send( $recipient, $subject, $html ) {
    global $DEFAULT_APP_TITLE;
    global $DEFAULT_EMAIL_FROM;
    global $DEFAULT_EMAIL_NAME;
    global $SERVER_EMAIL_HOST;
    global $SERVER_EMAIL_PORT;

    global $emailuser;
    global $emailpass;

    $result = false;
    $plain = convert_html_to_text( $html );

    try {
      $transport = Swift_SmtpTransport::newInstance(
                   $SERVER_EMAIL_HOST, $SERVER_EMAIL_PORT, 'ssl' )
          ->setUsername( $emailuser )
          ->setPassword( $emailpass );
      $mailer = Swift_Mailer::newInstance( $transport );

      $app = str_replace( ' ', '', $DEFAULT_APP_TITLE );

      $message = Swift_Message::newInstance()
        ->setSubject( $subject )
        ->setFrom( array( $DEFAULT_EMAIL_FROM => $DEFAULT_APP_TITLE ) )
        ->setReplyTo( array( $DEFAULT_EMAIL_FROM ) )
        ->setTo( $recipient )
        ->setPriority( 1 )
        ->setContentType( "text/plain; charset=UTF-8" )
        ->setBody( $plain, "text/plain" )
        ->addPart( $html, "text/html" )
        ->setId( time() . ".$app@" . $_SERVER['SERVER_NAME'] );

      // Include the language used to compose the message.
      $headers = $message->getHeaders();
      $headers->addTextHeader( "X-Mailer", "PHP v" . phpversion() );

      // No error status is returned; only false.
      // @see http://swiftmailer.org/docs/sending.html
      $result = $mailer->send( $message );
    }
    catch( Swift_TransportException $e ) {
      $this->log( $e->getMessage() );
      $result = false;
    }

    return $result;
  }

  /**
   * Uses the Mailgun API to validate an email address. This should
   * be called prior to sending an email using send.
   *
   * @see http://documentation.mailgun.com/api-email-validation.html#example
   */
  public function validate( $email ) {
    $mailgun = new Mailgun( "pubkey-3y4cc2gs3bkquj401sd6xjr5351rzjo3" );

    $result = $mailgun->get( "address/validate",
      array( "address" => $email ) );

    return (int)$result->http_response_body->is_valid == 1;
  }
}