Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use SmtpApiHeader;
- use MIME::Entity;
- use Net::SMTP;
- my $hdr = SmtpApiHeader->new;
- # The list of addresses this message will be sent to
- my @toList = ('isaac@example', 'tim@example', 'jose@example');
- # The names of the recipients
- my @nameList = ('Isaac', 'Tim', 'Jose');
- # Another subsitution variable
- my @timeList = ('4pm', '1pm', '2pm');
- # Set all of the above variables
- $hdr->addTo(@toList);
- $hdr->addSubVal('-name-', @nameList);
- $hdr->addSubVal('-time-', @timeList);
- # Specify that this is an initial contact message
- $hdr->setCategory("initial");
- # Enable a text footer and set it
- $hdr->addFilterSetting('footer', 'enable', 1);
- $hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");
- my $from = 'you@yourdomain.com';
- # For multiple recipient e-mails, the 'to' address is irrelivant
- my $to = 'doesntmatter@nowhere.com';
- my $plain = <<EOM;
- Hello -name-,
- Thank you for your interest in our products. We have set up an appointment
- to call you at -time- EST to discuss your needs in more detail.
- Regards,
- Fred
- EOM
- my $html = <<EOM;
- <html>
- <head></head>
- <body>
- <p>Hello -name-,<br />
- Thank you for your interest in our products. We have set up an appointment<br />
- to call you at -time- EST to discuss your needs in more detail.<br />
- Regards,<br />
- Fred<br />
- </p>
- </body>
- </html>
- EOM
- # Create the MIME message that will be sent. Check out MIME::Entity on CPAN for more details
- my $mime = MIME::Entity->build(Type => 'multipart/alternative' ,
- Encoding => '-SUGGEST',
- From => $from,
- To => $to,
- Subject => 'Contact Response for <name> at <time>');
- # Add the header
- $mime->head->add("X-SMTPAPI", $hdr->asJSON);
- # Add body
- $mime->attach(Type => 'text/plain',
- Encoding =>'-SUGGEST',
- Data => $plain);
- $mime->attach(Type => 'text/html',
- Encoding =>'-SUGGEST',
- Data => $html);
- # Login credentials
- my $username = 'yourlogin@sendgrid.net';
- my $password = "yourpassword";
- # Open a connection to the SendGrid mail server
- my $smtp = Net::SMTP->new('smtp.sendgrid.net',
- Port=> 25,
- Timeout => 20,
- Hello => "yourdomain.com");
- # Authenticate
- $smtp->auth($username, $password);
- # Send the rest of the SMTP stuff to the server
- $smtp->mail($from);
- $smtp->to($to);
- $smtp->data($mime->stringify);
- $smtp->quit();
Add Comment
Please, Sign In to add comment