首页 文章

由Mandrill发送电子邮件

提问于
浏览
2

我需要发送一封关于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邮件应该从您的帐户交易电子邮件发送,而不是我的 . 知道这是否可行?

1 回答

  • 1

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

    EDIT: 如果您有用户的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中Message功能的更多详细信息,请参见here .

    编辑#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配置详细信息(如大多数电子邮件框架建议的那样),您需要在用户想要使用特定于用户的详细信息发送电子邮件时对其进行实例化 .

相关问题