Mandrill

Status
Not open for further replies.

codywdx1

New Member
Messages
15
Reaction score
2
Points
3
I was hoping to use Mandrill for my website, but can seem to figure out how to set it up. Do we have access to Swift mailer or something similar?

Thanks in advance!
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Hi,

Although you can't connect to remote SMTP servers from your free hosting account, there shouldn't be any problems when connecting to Mandrill's REST API to send emails. What issue are you having? :)

Thank you,
 

codywdx1

New Member
Messages
15
Reaction score
2
Points
3
I am not using the REST API actually. I am using the PHP API, here is the code I was using:

Code:
<?php
    include_once "lib/swift_required.php";
 
    $subject = 'Hello from Mandrill, PHP!';
    $from = array($_POST['email'] => $_POST['name']);
    $to = array(
     'dostalcody@gmail.com'  => 'Cody Dostal'
    );
 
    $text = $_POST['message'];
 
    $transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
    $transport->setUsername('[username_redacted]');
    $transport->setPassword('[secret_key_redacted]');
    $swift = Swift_Mailer::newInstance($transport);
 
    $message = new Swift_Message($subject);
    $message->setFrom($from);
    $message->setBody($text, 'text/plain');
    $message->setTo($to);
 
    if ($recipients = $swift->send($message, $failures))
    {
     echo 'Message successfully sent!';
    } else {
     echo "There was an error:\n";
     print_r($failures);
    }
?>

I'm not too great with PHP though, do you have any examples on how to use the Mandrill REST API in PHP? I don't think (I guess I haven't checked) x10hosting supports Python! haha

I also just tried the following code:
Code:
<?php
try {
    $mandrill = new Mandrill('[secret_key_redacted');
    $message = array(
        'text' => $_POST['message'],
        'subject' => 'example subject',
        'from_email' => 'dostalcody@gmail.com',
        'from_name' => 'Cody Dostal',
        'to' => array(
            array(
                'email' => $_POST['email'],
                'name' => $_POST['name'],
                'type' => 'to'
            )
        ),
        'headers' => array('Reply-To' => 'dostalcody@gmail.com.com'),
    );
    $async = false;
    $result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
    print_r($result);
} catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}
?>

All that happens is it shows a blank page, with the url to the contact.php page shown.
 
Last edited:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Do you have the Mandrill class file(s) installed, and are you including them on your page? You'll need to have everything in the src directory (included in the ZIP) on the server, then use require_once(path/to/src/Mandrill.php); on your page to include the code. Otherwise, the Mandrill classes will turn into PHP errors, and since error reporting to the user is turned off by default, you'll just get a blank page.

See the Mandrill API documentation page for more.
 

codywdx1

New Member
Messages
15
Reaction score
2
Points
3
Awesome! Thank you essellar! I didn't think about the fact I would need the Mandrill class files *Embarrassed*

It is working perfectly now, so thank you :)
 
Status
Not open for further replies.
Top