Joseph Gjelaj
Joseph Gjelaj

Reputation: 23

Mandrill API E-mail queue with sinatra

Trying to get form to send e-mail to Mandrill api. My email keeps getting queued and will not send.

post '/my-handling-form-page' do

    m = Mandrill::API.new
    message = {
        :subject => "Hello from the Mandrill API",
        :from_name => "#{params[:name]} #{params[:email]}",
        :text => "Hi message how are you?",
        :to => [
            {
                :email => "[email protected]",
                :name => "Recipient1"
             }
        ],
        :html => "<html>#{params[:msg]}</html>",
        :from_email => "[email protected]"
    }
    sending = m.messages.send message
    puts sending
    erb :index
end

Error says: {"email"=>"[email protected]", "status"=>"queued", "_id"=>"216c30f42ee849e2a70528e3d4f9774f", "reject_reason"=>nil}

Help would be appreciated.

Upvotes: 2

Views: 1625

Answers (3)

Mike.R
Mike.R

Reputation: 2938

According to Mandrill api:

subaccount

the unique id of a subaccount for this message - must already exist or will fail with an error.

(At least for me the test start working after removing that field). Mandrill definitely should improve their response errors.

Upvotes: 0

GrvTyagi
GrvTyagi

Reputation: 4497

A Response Mail From Mandrill :

Thanks for reaching out. In this case, it appears that the API call being passed to Mandrill contains several invalid parameters - however, since you're also passing us an attachment array in that API call, you won't see the response indicating that it's an invalid API call.

Whenever Mandrill receives an attachment, we will always return a response of "Queued," as our systems will set that message aside to scan the attachment for viruses/malware before we process it in any other way. This means that, if something else is wrong with the API call, you won't be alerted, and it will "silently" fail.

It looks as though you've included several parameters from our default API docs, but those are intended to show users how those parameters would be included.

    **"subaccount"**: "customer-123",

and

    **"ip_pool"**: "Main Pool",

Will both cause this API call to fail, as you're specifying options that don't exist within your account. I would recommend that you go through your API code and remove anything that you aren't using. For reference, the minimum API call required to send an email would look like: { "message": { "html": "<html content>", "subject": "<subject>", "from_email": "<sender email address>", "from_name": "<sender name>", "to": [ { "email": "<recipient email address>", "name": "<recipient name>", "type": "to" } ], "headers": { "Reply-To": "<reply-to address>" } }, "async": false, "ip_pool": null, "send_at": null, "key": "<valid API key>" }

So after this valuable response this what that work in Django for me :)
def send_mail_msg(): import mandrill

try:
    mandrill_client = mandrill.Mandrill('xxxxxxxxxxxxxxx')
    message = {
        # 'attachments': [{'content': 'ZXhhbXBsZSBmaWxl',
        #                         'name': 'myfile.txt',
        #                         'type': 'text/plain'}],
               'auto_html': None,
               'auto_text': None,
               # 'bcc_address': '[email protected]',
               'from_email': '[email protected]',
               'from_name': 'Example Name',
               'global_merge_vars': [{'content': 'merge1 content', 'name': 'merge1'}],
               'google_analytics_campaign': '[email protected]',
               'google_analytics_domains': ['example.com'],
               # 'headers': {'Reply-To': '[email protected]'},
               'html': '<p>Example HTML content</p>',
               'images': [{'content': 'ZXhhbXBsZSBmaWxl',
                           'name': 'IMAGECID',
                           'type': 'image/png'}],
               'important': False,
               'inline_css': None,
               'merge': True,
               'merge_language': 'mailchimp',
               # 'merge_vars': [{'rcpt': '[email protected]',
               #                 'vars': [{'content': 'merge2 content', 'name': 'merge2'}]}],
               'metadata': {'website': 'www.example.com'},
               'preserve_recipients': None,
               'recipient_metadata': [{'rcpt': '[email protected]',
                                       'values': {'user_id': 123456}}],
               'return_path_domain': None,
               'signing_domain': None,
               # 'subaccount': 'customer-123',
               'subject': 'example subject',
               'tags': ['password-resets'],
               'text': 'Example text content',
               'to': [{'email': '[email protected]',
                       'name': 'Recipient Name',
                       'type': 'to'}],
               'track_clicks': None,
               'track_opens': None,
               'tracking_domain': None,
               'url_strip_qs': None,
               'view_content_link': None}
    result = mandrill_client.messages.send(message=message, async=False, ip_pool='Main Pool')
    # send_at=str(datetime.datetime.now().time()))

    '''
    [{'_id': 'abc123abc123abc123abc123abc123',
      'email': '[email protected]',
      'reject_reason': 'hard-bounce',
      'status': 'sent'}]
    '''
    return result

except mandrill.Error as e:  # Mandrill errors are thrown as exceptions
    print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
    # A mandrill error occurred: <class 'mandrill.UnknownSubaccountError'> - No subaccount exists with the id 'customer-123'
    raise`

Upvotes: 1

Joel Brewer
Joel Brewer

Reputation: 1652

From the Mandrill docs:

Why does a delivered message say "queued"?

Mandrill automatically tracks and records the SMTP response we receive from recipient mail servers for each email you send. Some successfully delivered emails will include a "queued" notation in the SMTP response such as 250 OK; queued as 12345. The email has still been delivered to the recipient as expected, but may require additional processing before it lands in the recipient's mailbox. For example, most times Mandrill can send email much faster than recipient servers are able to accept or process it. In many cases, things like the time of day and overall email traffic to that ISP or recipient server can affect how quickly they're able to receive and process your email.

Your code seems fine. Looks there could be an issue with the recipient's server.

Upvotes: 1

Related Questions