【问题标题】:Setting mail template in ses client在 ses 客户端中设置邮件模板
【发布时间】:2017-03-21 18:39:04
【问题描述】:

我正在使用 cakephp 3.0 开发一个 Web 应用程序,并且我有一个电子邮件发送功能,之前我使用的是 php 邮件功能。现在我正在使用 AWS ses 客户端。因此,在那个 ses 客户端中,我如何呈现模板。在 cake php 电子邮件功能中,这是可能的。但我不知道如何在 aws ses 客户端中执行此操作。 我的代码是

 public function testMail() {

if ($this->request->is('post')) {

  $client = SesClient::factory(Configure::read('AWScredentials'));


  $formData = $this->request->data;
  $body = $formData['body'];
  $ToAddresses = $formData['ToAddresses'];
  $request = array();
  $request['Source'] = 's@gmail.com';
  $request['Destination']['ToAddresses'] = array($ToAddresses);
  $request['Destination']['CcAddresses'] = 'ss@gmail.com';
  $request['Message']['Subject']['Data'] = 'Test mail from vcollect ses';
  $request['Message']['Subject']['Charset'] = 'ISO-2022-JP';
  $request['Message']['Body']['Text']['Data'] = $body;
  $request['Message']['Body']['Text']['Charset'] = 'ISO-2022-JP';

  try {
    $result = $client->sendEmail($request);
    $messageId = $result->get('MessageId');
    $this->log("Email sent! Message ID: $messageId", "info");
  } catch (Exception $e) {
    $this->log("The email was not sent. Error message:" . $e->getMessage(), "error");
  }
}   }

【问题讨论】:

  • 如果你不分享一些关于你的代码出了什么问题的细节,那么几乎没有人可以帮助你。至少是一条错误消息。
  • 我没有收到任何错误,我想呈现一个电子邮件模板并将其包含在 ses 正文中

标签: email amazon-web-services templates cakephp-3.0 amazon-ses


【解决方案1】:

你必须在 app.php 中创建一个调试发件人,像这样将它添加到你的 EmailTransport 数组中,它可以让你在不发送的情况下发送一封假邮件

 'EmailTransport' => [
        'default' => [
            'className' => 'Mail',
            // The following keys are used in SMTP transports
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => 'user',
            'password' => 'secret',
            'client' => null,
            'tls' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
        'debug' => [
            'className' => 'Debug',
        ],
    ],

使用您的模板生成电子邮件后,您可以使用此功能作为示例

public function setTemplate($template,$layout = null,$viewVars = array(),$debug = 0)
    {
        if(empty($layout)) $layout = 'default';

        $Email = new Email();
        $Email->template($template, $layout)
        ->transport('debug') //<---- Allows you to simulate the message without sending it
        ->to(array('foo@bar.org'=>'toto'))
        ->emailFormat('html');
        ->viewVars($viewVars);


        $Email->send();


        /* Once email is generate, personally i duplicate the object for access to the protected proprety ( it's not very clean but i don't have a greatest solution)
        The idea is to get _htmlMessage in the object */


        $refObj  = new ReflectionObject($Email);
        $refProp1 = $refObj->getProperty('_htmlMessage');
        $refProp1->setAccessible(TRUE);

        if($debug == 1)
        {
            die(debug($refProp1->getValue($Email)));
        }

        return $refProp1->getValue($Email);   // <---- your html code 
    }

【讨论】:

  • 我不想使用 cakephp 邮件功能我想使用 amazon ses 客户端 amazon ses api 发送邮件
  • 调试发件人是这样做的,它不会发送带有蛋糕或其他任何东西的电子邮件。它允许您使用蛋糕模板和布局生成电子邮件正文。 (这就是你想要的?)所以一旦生成了 html,你就可以得到它并用亚马逊发送它
  • @Gransfall 调试传输用于在开发和/或测试过程中进行调试,从技术上讲,您正在滥用它。另请注意,您可以通过Email::message() 方法访问消息内容,不需要这些反射黑客。此外,通过 Email 类进行的渲染应用了可能的字符集转换和换行,这可能需要注意,具体取决于用于处理数据的 API 的要求。
  • @ndm 感谢您提供所有这些信息,我会尽快对此进行测试
猜你喜欢
  • 2017-01-04
  • 2020-01-14
  • 2014-11-22
  • 1970-01-01
  • 2018-07-04
  • 2011-07-15
  • 2010-12-26
  • 2014-03-26
相关资源
最近更新 更多