Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending mail via SMTP in Perl

Tags:

email

smtp

perl

I am trying to send mail via SMTP in Perl.

I have written a script for this.

#!perl
use warnings;
use strict;
use Net::SMTP;

my $smtpserver = 'server';
my $smtpport = 25;
my $smtpuser   = 'username';
my $smtppassword = 'password';

my $smtp = Net::SMTP->new($smtpserver, Port=>$smtpport, Timeout => 10, Debug => 1);
die "Could not connect to server!\n" unless $smtp;

$smtp->auth($smtpuser, $smtppassword);
$smtp->to('[email protected]');
$smtp->data();
$smtp->datasend("To: mymail\@gmail.com\n");
$smtp->quit;

When I run this script, my output is like following:

Net::SMTP>>> Net::SMTP(2.31)
Net::SMTP>>>   Net::Cmd(2.29)
Net::SMTP>>>     Exporter(5.65)
Net::SMTP>>>   IO::Socket::INET(1.31)
Net::SMTP>>>     IO::Socket(1.32)
Net::SMTP>>>       IO::Handle(1.31)
Net::SMTP=GLOB(0x273faf0)<<< 220 server GMX Mailservices E
Net::SMTP=GLOB(0x273faf0)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x273faf0)<<< 250-server GMX Mailservices
Net::SMTP=GLOB(0x273faf0)<<< 250-8BITMIME
Net::SMTP=GLOB(0x273faf0)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP=GLOB(0x273faf0)<<< 250-SIZE
Net::SMTP=GLOB(0x273faf0)<<< 250-AUTH=LOGIN PLAIN
Net::SMTP=GLOB(0x273faf0)<<< 250-AUTH LOGIN PLAIN
Net::SMTP=GLOB(0x273faf0)<<< 250 STARTTLS
Net::SMTP=GLOB(0x273faf0)>>> RCPT TO:<[email protected]>
Net::SMTP=GLOB(0x273faf0)<<< 503 5.5.1 MAIL first {mp-eu001}
Net::SMTP=GLOB(0x273faf0)>>> DATA
Net::SMTP=GLOB(0x273faf0)<<< 503 5.5.1 MAIL first {mp-eu001}
Net::SMTP=GLOB(0x273faf0)>>> To: [email protected]
Net::SMTP=GLOB(0x273faf0)>>> .
Net::SMTP=GLOB(0x273faf0)<<< 502 5.5.2 Unimplemented {mp-eu001}
Net::SMTP=GLOB(0x273faf0)>>> QUIT
Net::SMTP=GLOB(0x273faf0)<<< 502 5.5.2 Unimplemented {mp-eu001}

I don't have enough information about Perl and SMTP, so I couldn't understand this error.

How can I solve this?

like image 587
Selin Avatar asked Apr 04 '12 09:04

Selin


People also ask

How to send mail using Perl?

#!/usr/bin/perl use MIME::Lite; $to = '[email protected]'; $cc = '[email protected]'; $from = '[email protected]'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; $msg = MIME::Lite->new( From => $from, To => $to, Cc => $cc, Subject => $subject, Type => 'multipart/mixed' ); # Add your text ...

How to send HTML mail using Perl?

Just like the SendMail Utility, MIME::Lite Module also allows the users to send HTML formatted mails using the perl-script. $message = '<h1>H1 is the main heading</h1><p><img src="image_source_with_extension" /></p>'; The above code will format your mail by following HTML syntax.

How do I send an email using SendMail?

Simple example Once logged in, you can run the following command to send email: [server]$ /usr/sbin/sendmail [email protected] Subject: Test Send Mail Hello World control d (this key combination of control key and d will finish the email.)

What is SmtpClient C#?

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.


1 Answers

You have to start a SMTP session (after authorization, if necessary) with a MAIL command giving the sender's email address. That's why the responses say "MAIL first" (5xx is an error response). So:

$smtp->auth($smtpuser, $smtppassword);
$smtp->mail('[email protected]');
$smtp->to('[email protected]');

But if you're not a SMTP expert, why not use a higher-level module like Email::Sender instead of the low-level Net::SMTP?

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use Email::Simple::Creator ();

my $smtpserver = 'server';
my $smtpport = 25;
my $smtpuser   = 'username';
my $smtppassword = 'password';

my $transport = Email::Sender::Transport::SMTP->new({
  host => $smtpserver,
  port => $smtpport,
  sasl_username => $smtpuser,
  sasl_password => $smtppassword,
});

my $email = Email::Simple->create(
  header => [
    To      => '[email protected]',
    From    => '[email protected]',
    Subject => 'Hi!',
  ],
  body => "This is my message\n",
);

sendmail($email, { transport => $transport });
like image 152
cjm Avatar answered Sep 29 '22 12:09

cjm