2015-03-08 93 views
2

我試圖將我的網站切換到Mandrill,但是我遇到了PHP API的一些問題。發送帶有Mandrill的電子郵件模板給我錯誤

有兩個問題:

  • 首先,發送電子郵件的兩倍。底部的代碼是我擁有的所有代碼(PHP打開和關閉標記除外),我無法弄清楚爲什麼每次都會發送兩次電子郵件。
  • 其次,我從cURL得到一個錯誤,說URL沒有設置。電子郵件正在發送,顯然有一個URL集。錯誤在下面。

這裏是我的代碼:

require_once './libraries/Mandrill.php'; 

try { 
    $mandrill = new Mandrill('myapikey'); 
    $template_name = 'my-template-slug'; 
    $template_content = ''; 
    $message = array(
     'to' => array(
      array(
       'email' => '[email protected]', 
       'name' => 'RecipientsName', 
       'type' => 'to' 
      ) 
     ), 
     'auto_text' => true, 
     'merge_vars' => array(
      array(
       'rcpt' => '[email protected]', 
       'vars' => array(
        array(
         'name' => 'USERNAME', 
         'content' => 'user1234' 
        ), 
        array(
         'name' => 'CONFIRM_CODE', 
         'content' => '19874lahg62378hwsi' 
        ) 
       ) 
      ) 
     ) 
    ); 
    $result = $mandrill->messages->sendTemplate($template_name, $template_content, $message); 
} catch(Mandrill_Error $e) { 
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage(); 
    throw $e; 
} 

這裏是錯誤:

A mandrill error occurred: Mandrill_HttpError - API call to messages/send-template failed: No URL set! Fatal error: Uncaught exception 'Mandrill_HttpError' with message 'API call to messages/send-template failed: No URL set!' in /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill.php:126 Stack trace: #0 /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill/Messages.php(160): Mandrill->call('messages/send-t...', Array) #1 /Users/Gavin/Desktop/Web/mandrill-test/index.php(70): Mandrill_Messages->sendTemplate('my-template-slug', Array, Array) #2 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/php/setup.php(131): require('/Users/Gavin/De...') #3 {main} thrown in /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill.php on line 126

+0

拋出的異常,並在同一時間發送電子郵件兩次發送郵件?或者是在調用多次後發生?我會嘗試調試/打印,看看你的方法是不是叫了兩次。另外,在這裏:http://stackoverflow.com/questions/22647687/sending-email-with-mandrill-using-php?rq=1他們建議使用send而不是sendTemplate – marianosimone 2015-03-17 14:47:45

+0

上面的代碼是唯一使用的代碼。是的,它拋出異常併發送電子郵件兩次。 – Gavin 2015-03-17 15:30:12

+0

我還應該補充說,因爲這個問題正在發生,所以我轉而使用Mandrill的SMTP服務器而不是API(我的Web服務器上託管了自己的模板)並且只發送一次。 – Gavin 2015-03-17 15:39:07

回答

2

山魈劫持等環節該鏈接是通過他們的服務器改道注入了自己的網址。用戶在瀏覽右側頁面之前在其瀏覽器中看到了mandrill URL,結果如下

您帳戶中的Sending Defaults頁面上有一個用於點擊跟蹤的選項(應該是一個下拉菜單,靠近頂部,就在跟蹤打開的複選框下面)。除非您爲點擊跟蹤提供了不同的每封郵件設置,否則您選擇的內容會應用於所有郵件的默認選項。使用SMTP,您可以使用自定義SMTP標頭在每封郵件的基礎上設置點擊跟蹤。有關使用SMTP標頭進行定製的更多信息,請參見Mandrill KB here

0
<?php 
require_once 'Mandrill.php'; 

$mandrill = new Mandrill('MY API KEY IS USUALLY HERE'); 
$message = array(
    'subject' => 'Test message', 
    'from_email' => '[email protected]', 
    'from_name' => 'Sender person', 
    'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>', 
    'to' => array(array('email' => '[email protected]', 'name' => 'Recipient 1')), 
    'merge_vars' => array(array(
     'rcpt' => '[email protected]', 
     'vars' => 
     array(
      array(
       'name' => 'FIRSTNAME', 
       'content' => 'Recipient 1 first name'), 
      array(
       'name' => 'LASTNAME', 
       'content' => 'Last name') 
    )))); 

//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message)); 
echo ("hello"); 

?> 

$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null); 
+0

嘗試使用新的當前源文件刪除位置。 https://packagist.org/packages/mandrill/mandrill – 2015-03-24 12:40:13