perl script which received email using sasl authentication - perl

I have use a script for smtp purpose on Centos OS as given below:
#!/usr/bin/perl
use strict;
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
use Data::Dumper;
our $host = $ARGV[0] || "MY_IP" ;
our $server = new Net::SMTP::Server($host) ||
croak("Unable to open SMTP server socket");
print "Waiting for incoming SMTP connections on ".($host eq "0.0.0.0" ? "all IP addresses":$host)."\n";
$| = 1;
while(my $conn = $server->accept()) {
print "Incoming mail ... from " . $conn->peerhost() ;
my $client = new Net::SMTP::Server::Client($conn) ||
croak("Unable to process incoming request");
if (!$client->process) {
print "\nfailed to receive complete e-mail message\n"; next; }
print " received\n";
print "From: $client->{FROM}\n";
my $to = $client->{TO};
my $toList = #{$to} ? join(",",#{$to}) : $to;
print "To: $toList\n";
print "\n" ;
print $client->{MSG};
}
Now I have send email using application which using below information.
SMTP Hostname: MY_IP
SMTP Username: root
SMTP Password: ****
SMTP Port: 25
Send a test email to: mahboobkheri#gmail.com
And when I have clicked on send email then found that email is received on smtp and it show below output:
Waiting for incoming SMTP connections on MY_IP
Incoming mail ... from MY_IP received
From: <abc#xyz.com>
To: <mahboobkheri#gmail.com>
To: mahboobkheri#gmail.com
Subject: Test Email System
Message-ID: <9f0c1a999390053b8e64665749a047a8#resolv.fastreturn.biz>
Return-Path: abc#xyz.com
Date: Tue, 02 Dec 2014 12:20:48 +0100
From: <abc#xyz.com>
Reply-To: abc#xyz.com
MIME-Version: 1.0
Content-Type: text/plain; format=flowed; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Hi,This is a test of the emailing system. If you received this ok, then everything is working as it should.
But if I have use no existing username and password (Fake detail) then also application send email. So I have the need to configure sasl authentication. I have try and also search on Google but can't find useful. I have requested to please help me that how write script which accept email using sasl authentication.

Related

Problem with sending text attachment in Perl

I have a procedure to send mails as this:
sub SendMail {
my $subject = shift;
my #message = #_;
my $sender;
my $MIME_BOUNDARY = '====Multipart.Boundary.689464861147414354====';
my $now = strftime("%Y-%m-%d %H:%M:%S", localtime);
my #addresses = split(",", $ENV{ADMIN_MAIL});
my $sender = $ENV{USER} || $ENV{USERNAME};
$sender .= "\#" . hostname();
my $smtp = Net::SMTP->new($ENV{MAILHOST} || 'mailhost', Debug => 1);
unless ( $smtp ) {
die "Error while sending notification mail. Not connected to SMTP server.";
}
$smtp->mail( $addresses[0] );
$smtp->recipient( #addresses );
$smtp->data;
$smtp->datasend("From: $sender\n");
$smtp->datasend("To: " . join(",", #addresses) . "\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("Date: " . strftime("%a, %d %b %Y %H:%M:%S %z", localtime) . "\n");
if ( #log_messages ) {
$smtp->datasend("Mime-Version: 1.0\n");
$smtp->datasend("Content-Type: multipart/mixed; boundary=\"$MIME_BOUNDARY\"\n");
$smtp->datasend("This is a multipart message in MIME format.\n");
$smtp->datasend("--$MIME_BOUNDARY\n");
}
$smtp->datasend("Content-type: text/plain; charset=UTF-8\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\n");
foreach ( #message ) { $smtp->datasend("$_\n") }
$smtp->datasend("\n\n");
$smtp->datasend("Message from " . hostname() . " (PID=$$) sent by 'LogDumper.pl' at $now");
$smtp->datasend("\n");
if ( #log_messages ) {
$smtp->datasend("\n");
$smtp->datasend("--$MIME_BOUNDARY\n");
$smtp->datasend("Content-Type: text/plain; name=\"logs.txt\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"logs.txt\"\n");
$smtp->datasend("\n");
foreach ( #log_messages ) { $smtp->datasend("$_\n") }
$smtp->datasend("\n");
$smtp->datasend("--$MIME_BOUNDARY--\n");
}
$smtp->dataend;
$smtp->quit;
}
The procedure works fine with plain text mails, i.e. empty #log_messages. However, if I try to attach a text file
my #log_messages;
push #log_messages, "Line 1";
push #log_messages, "Line 2";
SendMail("The Subject", "The Message");
then the mail is not sent.
Debug output is this:
Net::SMTP=GLOB(0x7a78a40)<<< 354 Start mail input; end with <CRLF>.<CRLF>
Net::SMTP=GLOB(0x7a78a40)>>> From: Domscheit#xxxxx
Net::SMTP=GLOB(0x7a78a40)>>> To: wernfried.domscheit#xxxxx.xxx
Net::SMTP=GLOB(0x7a78a40)>>> Subject: The Suject
Net::SMTP=GLOB(0x7a78a40)>>> Date: Mo, 01 Okt 2018 10:15:57 W. Europe Daylight Time
Net::SMTP=GLOB(0x7a78a40)>>> Mime-Version: 1.0
Net::SMTP=GLOB(0x7a78a40)>>> Content-Type: multipart/mixed; boundary="====Multipart.Boundary.689464861147414354===="
Net::SMTP=GLOB(0x7a78a40)>>> This is a multipart message in MIME format.
Net::SMTP=GLOB(0x7a78a40)>>> --====Multipart.Boundary.689464861147414354====
Net::SMTP=GLOB(0x7a78a40)>>> Content-type: text/plain; charset=UTF-8
Net::SMTP=GLOB(0x7a78a40)>>> Content-Disposition: quoted-printable
Net::SMTP=GLOB(0x7a78a40)>>> The Message
Net::SMTP=GLOB(0x7a78a40)>>> Message from xxxxx (PID=8072) sent by 'LogDumper.pl' at 2018-10-01 10:15:54
Net::SMTP=GLOB(0x7a78a40)>>> --====Multipart.Boundary.689464861147414354====
Net::SMTP=GLOB(0x7a78a40)>>> Content-Type: text/plain; name="logs.txt"
Net::SMTP=GLOB(0x7a78a40)>>> Content-Disposition: attachment; filename="logs.txt"
Net::SMTP=GLOB(0x7a78a40)>>> Line 1
Net::SMTP=GLOB(0x7a78a40)>>> Line 2
Net::SMTP=GLOB(0x7a78a40)>>> --====Multipart.Boundary.689464861147414354====--
Net::SMTP=GLOB(0x7a78a40)>>> .
Net::SMTP: Unexpected EOF on command channel at C:\Developing\Source\LogDumper.pl line 1271.
Apparently there is a missing or needless \n. I tried to put it almost everywhere but I don't get it working.
Update:
Actually it is working when I execute the script on Linux but not on my Windows development PC.
I think the problem is solved.
It works on production server at Linux
It works on development Windows PC when I am working in the office
It does not work on Windows when I am working at home and connected via VPN - nasty security department...
Well, it is not really solved but for me it is working fine. I did not test (yet) whether it fails over VPN also for one of the higher-level packages.

connect to localhost failed (Connection refused) no (more) retries

I want to send an email using perl ,but when i execute the command as follows:
#./sendmail.sh "par1" "par2" "par3"
i got the error msg "connect to localhost failed (Connection refused) no (more) retries"
sendmail.sh:
/usr/bin/perl /code/sendmail.pl "$1" "$2" "$3";
sendmail.pl:
#!/usr/bin/perl -w
use Mail::Sendmail;
my $event1 = shift(#ARGV);
my $event2 = shift(#ARGV);
my $time = shift(#ARGV);
#my $info = shift(#ARGV);
my $datetime = `/bin/date "+20%y-%m-%d %H:%M:%S"`;
chomp $datetime;
$msg = "This is Monitor System speak:\n
The system discovers the events at $datetime.
Something may be abnormal, please check it. The detail is below:\n";
$msg = $msg."$event1 and $event2 at $time\n";
$msg = $msg."\n";
$msg = $msg."Any problem, check it from http://map_test.php\n\n\n";
$mail_subject = "Abnormal";
sendmail(
From => 'localhost',
To => 'test#mail.com',
Subject => $mail_subject,
Message => $msg,
);
Any help appreciated.
smtp stands for simple mail transfer protocol.
When you need to send an email your mail client needs to talk to an smtp server which will accept the message. Normally your internet service provider will provide an smtp host. If you look at your mail client it will need to have an smtp server configured to be able to send mail.
Ok so when you install the Mail::Sendmail module, it doesn't know what your smtp server will be. It is up to you to tell it. It provides a default of localhost which would often be true if your server is running a sendmail daemon.
The configuration of Mail::Sendmail is stored in a variable called
%Mail::Sendmail::mailcfg
You can change the value of the sendmail server using this snippet of code:
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
You need to add this line of code to your script to set the smtp server.
It adds this server to an array which also includes localhost.
So if neither of the hosts work it will still print an error message about localhost which is slightly confusing.
If you use Data::Dumper to print the contents of the mailcfg variable it will look something like this:
#!/usr/bin/perl
use Mail::Sendmail;
use Data::Dumper;
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
print Dumper(\%Mail::Sendmail::mailcfg);
Should return:
$VAR1 = {
'retries' => 1,
'smtp' => [
'my.smtp.server',
'localhost'
],
'delay' => 1,
'port' => 25,
'from' => '',
'debug' => 0,
'tz' => '',
'mime' => 1
};

PHPMailer HELP - Getting SMTP error but mail is send

I'm ripping my head off in a moment...
Using PHPmailer to send a email from my site.
I have created a HTML-form on my website, and the values from there needs to go to my mail... You know - standard :)
But I keep getting this error :
Mailer Error: SMTP connect() failed.
When I have turned SMTPDEBUG on it goes like this:
SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.
The host and port is correct, got the details from my provider..
Is there something i'm missing, typed in wrong or misunderstood?
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// here we use the php mail function
// to send an email to:
// you#yourdomain.com
mail( "info#recive.com", "Feedback Form Results",$message, "From: $email" );
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.test.com'; // Specify main and backup server
$mail->Port = 25;
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#recive.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->From = 'info#recive.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->addAddress('info#recive.com'); // Name is optional
//$mail->addReplyTo('info#recive.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
From my understanding the 'ssl' setting for SMTPSecure is for direct ssl connect, while the 'tls' setting is for plain connect followed by upgrading to SSL with the STARTTLS command. With port 25 you need plain connect, e.g. 'tls'.

Zend_Mail error 500 > postfix/sendmail fatal: -n option not supported

I'm working on a Zend project in a very specific server configuration, our production environment is made of two dedicated servers, one for the company's email which host a postfix server and an other server for our web-application which is running on Apache2/Zend.
Those servers have two different IPs, but works on the same website domain.
Now when i try to send an email with an email from the mail server as a sender, i get an error 500 from Zend_Mail and the email.err log tells me :
postfix/sendmail[15782]: fatal: -n option not supported
But when i put a local adresse or a blank email as a sender it works, so i guess i get kicked out by the postfix of the webserver because it doesn't manage localy those emails.
So my question is: is there any way to use a domain email as sender from a distant server without merging the two servers?
Edit: i forgot to add: i can't use the distant server SMTP, i only can use a local sendmail.
I haven't find any solution or explanations so i ended up by writing a custom action helper based on the mail command of PHP.
I hope it can help someone:
class Zend_Controller_Action_SentEmail extends Zend_Controller_Action_Helper_Abstract{
public function sendEmail($from, array $to, $subject, $message){
//Header set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "From: ".$from."<email#domain.com>\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
//To
$stringTo = "";
foreach($to as $k => $v) {
$stringTo .= $k." <".$v.">, ";
}
$stringTo = trim($stringTo, ", ");
//Send the email
if(mail($stringTo, $subject, $message, $headers, "-f bounce#domain.com")){
return true;
}
else{
//Oh! Noes!
return false;
}
}
}

(perl Net::SMTP) authentication failure using gmail smtp server

I have written a perl script to send email using the gmail's smtp server: smtp.gmail.com -
use Net::SMTP;
my $smtp = Net::SMTP->new('smtp.gmail.com',
Port=> 587,
Timeout => 20,
Hello=>'user#gmail.com'
);
print $smtp->domain,"\n";
$sender = "user#gmail.com";
$password = "mypassword";
$smtp->auth ( $sender, $password ) or die "could not authenticate\n";
$receiver = "user#gmail.com";
$subject = "my custom subject";
$smtp->mail($sender);
$smtp->to($receiver);
$smtp->data();
$smtp->datasend("To: <$reciever> \n");
$smtp->datasend("From: <$sender> \n");
$smtp->datasend("Content-Type: text/html \n");
$smtp->datasend("Subject: $subject");
$smtp->datasend("\n");
$smtp->datasend('the body of the email');
$smtp->dataend();
$smtp->quit();
print "done\n\n";
For 'user', I have my gmail username and for 'mypassword' I have the password. But when I run this code it stops at the auth itself giving : could not authenticate.
I am able to connect to the smtp serever of google as I get : 'mx.google.com' as the result of :
print $smtp->domain,"\n";
What is it that I am doing wrong? Please help.
use Net::SMTP;
my $smtp = Net::SMTP->new ....
afaik you need to use an encrypted connection to send via gmail, use Net::SMTP::TLS or Net::SMTP::SSL (with port 465)
Hello=>'user#gmail.com'
'Hello' is not an email address, put your hostname there instead
$sender = "user#gmail.com";
...
$receiver = "user#gmail.com";
put these in single quotes
if you still get "could not authenticate." make sure you have the modules MIME::Base64 and Authen::SASL installed.
$smtp->datasend("To: <$reciever> \n");
should be $receiver, not $reciever
Gmail uses TLS/STARTTLS on port 587, SSL on port 465. Please follow Sending email through Google SMTP from Perl. Alternatively you could use Email::Send::SMTP::Gmail. I recommend this as it is way easier to use and has many more features.