mail/akmailer

dev-master 2024-05-01 13:02 UTC

This package is auto-updated.

Last update: 2024-08-31 00:27:47 UTC


README

这个库是常见PHP邮件发送器的包装器。

您可以使用swiftmailerphpmailerZend Mail来发送电子邮件。

功能

  • 支持SMTP传输。
  • 使用HTML或纯文本模板。您可以在单独的文件中创建模板,库将使用它们作为电子邮件的正文。

示例用法

通过扩展akMailer类创建您的邮件发送器类。

    # MyMailer.php
    class MyMailer extends akMailer {

      public function example_email($options) {
        $this->from     = array("noreply@example.com" => "My example");
        $this->cc       = array("recipient_1@domain.tld" => "A name");
        $this->bcc      = array("recipient_2@domain.tld" => "Another Name");
        $this->send_to  = array( $options['recipient'] => $options['name'] );
        $this->subject  = 'My Subject';
        $attach = realpath(dirname(__FILE__) . "/tux.png");
        $embedded = $this->setEmbeddedAttachment($attach);
        # The keys of this array would be variables in your template file.
        $this->body     = array(
          'name' => $options['name'],
          'title' => $options['title'],
          'embedded' => $embedded
        );
      }
    }

使用您的新类

    # example.php
    require_once '../lib/init.php';
    require_once 'MyMailer.php';

    akMailer::$host     = 'mail.domain.tld'; #Set your email host
    akMailer::$port     = 25;
    akMailer::$username = 'username';        # Set your username
    akMailer::$password = 'password';        # Set your password
    akMailer::$encryption = 'tls';           # Set the type of encryption
    akMailer::$transport_type = 'smtp';
    akMailer::$mailer   = 'swift_mailer';
    akMailer::$log      = true;              # allow logging events
    akMailer::$test     = false;             # Set this to true and no email will be send. It would be only logged.
    akMailer::$templates_path = realpath( dirname(__FILE__) ). "/templates/";
    try {
      $mailer = new MyMailer();

      $options =array(
        'recipient' => 'your_test_email@domain.tld',
        'name' => 'John Doe',
        'title' => 'Mr'
      );

      /*
       * Call the method from your Mailer object adding the prefix 'send_'.
       * so here the method in MyMailer object is example_email(), and is called
       * like send_example_email()
       */
      $mailer->send_example_email($options);
      if ( $mailer->isSuccess() ) {
        echo 'Successfully send email!';
      }
    } catch (akMailerException $e) {
      echo $e->getMessage();
    }

定义您的模板

    # example_email.text.html.php
    <h2>Dear <?php echo $title ?> <?php echo $name ?></h2>
    <p><img src="<?php echo $embedded ?>" alt="title"></p>
    <p><em>Inline attachment</em>
    <p>This is a demo example from akMailer</p>
    <p>Thank you.</p>