Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Dec 8, 2018

Python Sparkpost sample

How to send emails from Python using Sparkpost module


In this tutorial, we will see how to send emails using Sparkpost.

First you need to register with Sparkpost and get your API key, so that you can use this key in sending e-mails.

The advantage/beauty of using sparkpost module is

  • You can send thousands of emails even in the free-tier.
  • You can send attachments & you can use html.
  • You can schedule the emails for future date & time.
  • You can also delete the future emails if not required.
  • It has lot more flexible features than inbuilt smtplib.
  • You can check the delivery status of your emails in Sparkpost dashboard (once you login, you can able to see this)
  • Python API is very simple to use, they also support their APIs in multiple languages.


from sparkpost import SparkPost

emails_to_send = ['test@gmail.com']
sp = SparkPost('XXXXXXXXXXXXXXXXXXXXXXXX') #Key

response = sp.transmissions.send(
          recipients=emails_to_send,
          html='',
          from_email='noreply@test.com',
          subject='test'
)
print(response)


May 15, 2012

send email in perl

There are 3 ways to send mail from Perl
  • shelling out to /usr/sbin/sendmail
  • using Net::SMTP directly when the application did not need to send attachments
  • using MIME::Lite when you did need to include attachments

Out of these MIME::Lite is best as it handles attachments and performance perspective.
Let us explain sending email using MIME::Lite 


#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use MIME::Lite;

    my $msg = MIME::Lite->new(
        From     =>'admin@example.com',
        To       =>'user@example.com',
        Subject  =>'testing a mail with attachments',
        Type =>'multipart/mixed'
    ) die "Error creating multipart container: $!\n";
  
    ### Add the text message part
    $msg->attach (
      Type => 'TEXT',
      Data => "Here is the attachment file(s) you wanted"
    ) or die "Error adding the text message part: $!\n";

    ### Add the GIF file
    $msg->attach (
       Type => 'image/gif',
       Path => my_file.gif,
       Filename => your_file.gif,
       Disposition => 'attachment'
    ) or die "Error adding $file_gif: $!\n";

    ### Add the ZIP file
    $msg->attach (
       Type => 'application/zip',
       Path => my_file.zip,
       Filename => your_file.zip,
       Disposition => 'attachment'
    ) or die "Error adding $file_zip: $!\n";

       $msg->send;

1;