douzhushen_9776 2016-08-23 10:53
浏览 125

Mandrill API错误

First Time i used Mandrill API to send email,

But I got this error.

A mandrill error occurred: Mandrill_ValidationError - You must specify a key value Fatal error: Uncaught exception 'Mandrill_ValidationError' with message 'You must specify a key value' in mandrill-api-php/src/Mandrill.php:153 Stack trace: #0 mandrill-api-php/src/Mandrill.php(132): Mandrill->castError(Array) #1 mandrill-api-php/src/Mandrill/Messages.php(80): Mandrill->call('messages/send', Array) #2 mandrill.php(88): Mandrill_Messages->send(Array, false) #3 {main} thrown in Mandrill.php on line 153

This is my code

<?php

require_once 'mandrill-api-php/src/Mandrill.php';

$servername = "localhost";
$username = "root";
$password = "";
$table = '';

$conn = mysql_connect($servername, $username, $password);


if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}



$selected = mysql_select_db("DB Name")
    or die("Could not select database");

$query=mysql_query(Query);


$table .= "<table width='auto'  height='auto' border='1'  bordercolor='#003399'  style='color:#FF0000;table-layout:fixed' cellpadding=0 cellspacing=0>
<tr>
<th>User ID</th>
<th>Full Name</th>
<th>Username</th>
<th>Email ID</th>

</tr>";

while($row = mysql_fetch_array($query)) 
{ 
$table .= "<tr>";
$table .= "<td>" . $row['id'] . "</td>";
$table .= "<td>" . $row['fullname'] . "</td>";
$table .= "<td>" . $row['username'] . "</td>";
$table .= "<td>" . $row['useremail'] . "</td>";
$table .= "</tr>"; 
}
$table .= "</table><br>";


try {
$mandrill = new Mandrill(API Key);
$message = array(
    'html' => $table,
    'subject' => 'Notification Email',
    'from_email' => 'Example@example.com',
    'from_name' => 'Test',
    'to' => array(
        array(
            'email' => 'Example@example.com',
            'name' => 'Test',
            'type' => 'to'
        )
    ),
    'important' => false,
    'track_opens' => null,
    'track_clicks' => null,
    'auto_text' => null,
    'auto_html' => null,
    'inline_css' => null,
    'url_strip_qs' => null,
   // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
throw $e;
}
?>
'preserve_recipients' => null,
    'view_content_link' => null,
    'tracking_domain' => null,
    'signing_domain' => null,
    'return_path_domain' => null,

);
$async = false;
$result = $mandrill->messages->send($message, $async);
print_r($result);


} 
catch(Mandrill_Error $e) {

echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();

throw $e;
}
?>
  • 写回答

1条回答 默认 最新

  • drp935159 2018-06-19 12:13
    关注
    <pre>/*
    Before coding, you have to make configurations in Mandrill.
    
    Step 1: Enter Mandrill and register your domain.
    
    Step 2: Configure DNS: DKIM and SPF (This is a good tutorial: https://www.youtube.com/watch?v=uEjP_smeLjU) at https://mandrillapp.com/settings/sending-domains
    
    Step 3: Get a KEY API (https://mandrillapp.com/settings)
    
    Step 4: Code with PHP
    
    Step 5: Good luck! :)
    */
    
    
    /*LIBS*/
    include 'lib/mandrill-api-php/src/Mandrill.php';
    $mandrill = new Mandrill('YOUR API KEY HERE');
    
    /*ADMIN AND USER EMAIL*/
    $admin_email = 'your_email@your_domain.com';
    $client_email = 'the_email_of_the_client@mail.com';
    
    /*attach PDF*/
    $attachment = file_get_contents('the_route_to_your_pdf');
    $attachment_encoded = base64_encode($attachment);
    
    try{
        $user_message = array(
            'subject' => 'Your Subject here',
            'from_email' => $admin_email,
            'from_name' => 'your_domain_for_example',
            'html' =>  '<p>Aquí va la plantilla HMTL</p>',
            'to' => array(array('email' => $client_email, 'name' => 'Recipient 1')),
            'merge_vars' => array(array(
                'rcpt' => 'recipient1@domain.com',
                'vars' =>
                array(
                    array(
                        'name' => 'FIRSTNAME',
                        'content' => 'Recipient 1 first name'),
                    array(
                        'name' => 'LASTNAME',
                        'content' => 'Last name')
                ))),
            'attachments' => array(
                array(
                    'content' => $attachment_encoded,
                    'type' => "application/pdf",
                    'name' => 'the_name_of_the_attach.pdf',
                ))
        );
    
        $res_user_mandrill = $mandrill->messages->send($user_message, $async=false, $ip_pool=null, $send_at=null);
    
    } catch(Mandrill_Error $e) {
    
    }
    
    </pre>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀