繁体   English   中英

通过 Mandrill 发送电子邮件

[英]send email by Mandrill

我需要给 mandrill 发一封电子邮件。 这个Mandrill API在我的项目中实现,并通过客户端提供的apikey发送测试邮件。 我发送邮件有两种选择,一种是通过我的 Mandrill 帐户,另一种是用户可以通过 MailChimp 登录。 然后插入API KEY mandrill您的帐户。

我在 conf 文件中有一个默认变量,如下所示:

public $default = array(
    'transport' => 'Smtp',
    'from' => array('noreply@example.com' => 'Example'),
    'host' => 'smtp.mandrillapp.com',
    'port' => 587,
    'timeout' => 30,
    'username' => 'example@example.com',
    'password' => '12345678',
    'client' => null,
    'log' => false
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

要通过我的帐户 mandrill 发送邮件,我这样做:

$email = new FrameworkEmail();
$email->config('default');
$email->emailFormat('html');

我给我的帐户 Mandrill 数据。 但是,如果用户选择使用 MailChimp 并添加您的 API KEY,那么 Mandrill 邮件应该从您的帐户交易电子邮件中发送,而不是我的。 知道这是否可能吗?

我不相信这可以通过 MailChimp 进行身份验证然后通过 Mandrill 发送。 Mandrill API 使用一组与 MailChimp API 不同的密钥,因此如果用户通过 MailChimp 登录,您将无法访问用户的 Mandrill API 密钥。

编辑:如果您有用户的 Mandrill API 密钥,您应该能够将其直接提供给Mandrill SDK的 Mandrill 发送函数:

<?php 
$mandrill = new Mandrill('USER_PROVIDED_API_KEY_GOES_HERE');
$message = array(
    'html' => '<p>html content here</p>',
    // other details here
)
$async = False;
$ip_pool = null;
$send_at = null;

$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
?>

可以在此处找到有关 SDK 中消息功能的更多详细信息。

编辑 #2:使用 CakeEmail 可以实现相同的方法 - 您只需要在收到用户的 API 密钥时而不是之前实例化$email类。

CakeEmail 建议在标准设置中,您在初始配置中执行此操作:

class EmailConfig {
    public $mandrill = array(
        'transport' => 'Mandrill.Mandrill',
        'from' => 'from@example.com',
        'fromName' => 'FromName',
        'timeout' => 30,
        'api_key' => 'YOUR_API_KEY',
        'emailFormat' => 'both',
    );
}

但是我们不能使用默认值设置它,然后使用他们的 API 密钥为每个用户更改它。 我们需要在收到用户的电子邮件时实例化它,如下所示:

App::uses('CakeEmail', 'Network/Email');

// PHP code that takes in user email
$user_api_key = // this would come from a form, or from a user's record in the database

$email = new CakeEmail(array(
    'transport' => 'Mandrill.Mandrill',
    'from' => 'from@example.com',
    'fromName' => 'FromName',
    'timeout' => 30,
    'api_key' => $user_api_key,
    'emailFormat' => 'both',
));

// add CakeEmail details like $email->template, $email->subject(), etc.

$email->send();

所以不管使用什么框架,原理都是一样的。 不是在全局设置中实例化 Mandrill 配置细节(就像大多数电子邮件框架推荐的那样),您需要在用户想要发送电子邮件时使用用户特定的细节来实例化它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM