Skip to main content

Sending Emails using Mandrill API in python

Mandrill is a great API for sending transactional e-mails. Transactional e-mails are emails such as order confirmations, user signups, forgotten passwords and anything that the user may receive during normal use.
Mandrill  is  a simple REST API.
By creating an account at Mandrill.com, you can get your API keys, and then you are ready to make calls to Mandrill.
Requirements :
Python 2.6+, Python 3.0+
Getting the library :
The preferred method of installing the Mandrill Python API client is by using pip.
$ sudo pip install mandrill
Using the library:
Now that you have a copy of the library in your project, you're ready to start using it. All uses of the Mandrill API start by importing the library module and instantiating the Mandrill class.
import mandrill
mandrill_client = mandrill.Mandrill('YOUR_API_KEY')
They are different  api call categories.In that we are using messages calls
Messages calls:
Send a new transactional message through Mandrill:
import mandrill
try:
    mandrill_client = mandrill.Mandrill('YOUR_API_KEY')
    message = { 
        'merge': False,
        'from_email': 'from@gmail.com',
        'headers': {'Reply-To': 'message.reply@example.com'},
        'from_name': 'sender_name',
        'subject': "Testing out Mandrill",
        'html': '<p>Example HTML content</p>',
        'to': [{
        'email': 'to@gmail.com',
        'name': 'to_name',
        'type': 'to'
       }],
        'subaccount': 'customer-123',
        'async': True,
          
    }

    result = mandrill_client.messages.send(message = message)
    # result is a dict with metadata about the sent message, including
    # if it was successfully sent
    print(result)
    

except mandrill.Error, 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

The result is an array - of structs for each recipient containing the key "email" with the email address, and details of the message status for that recipient.

In Mandrill you can host a range of templates with variables. By calling this endpoint, you don’t have to provide any HTML, only the variables needed.This is basic code for sending emails through mandrill. Please go through the link Mandrill API for more information about Mandrill API.So it's all for this article. I hope it helped you. If you have any queries please comment below and don't forget to share this article with your friends.

Comments

Popular posts from this blog

MYSQL CONNECTION USING PDO WITH PHP

Hello guys, In the previous tutorial we have seen how to connect to MySQL database with PHP using MySQLi. Today we will learn how to connect with PDO. As a PHP programmer we have learned how to access database by using either MySQL and MySQLi. As of PHP 5.1, there's a better way. PHP Data Objects(PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction :        PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. Database Support:       The extension can support any database that a PDO driver has been written for. The following drivers currently implement the PDO interface: PDO_CUBRID Cubrid PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase PDO_FIREBIRD Firebird PDO_I...

Convert HTML to PDF using mPDF library

PDF is format of file to share .Here is code for converting html to PDF using mpdf library (PHP library). //include library path include('mpdf/mpdf.php'); $mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); $mpdf->SetDisplayMode('fullpage'); $mpdf->list_indent_first_level = 0; $mpdf->WriteHTML('<html><head></head><body><h2>Welecome to HTML to PDF </h2></body></html>'); //save the file put which location you need folder/filename $mpdf->Output('test.pdf', 'F'); You can download the library from GitHub. Extract it and paste into your (project folder).

Send Email via SMTP server using PHPMailer

Email sending is the most useful feature for web application. mail() function in PHP is used to send mail from PHP script.While sending email using mail() ,the mail will be sent from web server. Sometimes it may cause issues on sending an email and fails to deliver. With SMTP you can overcome this issue,when you send an email through SMTP server email will be sent through mail server  rather than web server.       The easiest way to send email in PHP with SMTP is to use PHPMailer library. PHPMailer provides an ability to send email via SMTP server in PHP. In this tutorial, you’ll show how you can send HTML email with SMTP in PHP using PHPMailer. Send Email via SMTP server: PHPMailer library: The script uses PHPMailer to send the email via SMTP server, so PHPMailer library needs to be included first. Include the PHPMailerAutoload.php file and create a new PHPMailer instance. <?php require ( "phpmailer/PHPMailerAutoload.php" ); $mail = new ...