#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::INET;

# Configuration
my $from_address = '[email protected]';  # Sender email address
my $rdns_domain = 'rat.morena.rip';      # Sender's domain for HELO
my $editor = '/usr/bin/ovi';             # Set editor to 'ovi'

# Get the current date and time in local CET/CEST format
my ($sec, $min, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime();
$year += 1900;  # Correct the year
$month += 1;    # Correct the month (January is 0 in localtime)

# Format the date in RFC 5322 format
my $date = sprintf("%s, %02d %s %4d %02d:%02d:%02d %s",
 qw(Sun Mon Tue Wed Thu Fri Sat)[$wday],
 $day,
 qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$month],
 $year,
 $hour, $min, $sec,
 ($isdst ? "+0200" : "+0100") # CEST (+0200) if DST, otherwise CET (+0100)
);

# Get recipient's email and domain
print "Enter recipient (To): ";
my $to_address = <STDIN>;
chomp $to_address;

# Extract the domain part from the email address
my ($user, $domain) = split('@', $to_address);

# Execute nslookup command to get MX records
my $nslookup_output = `nslookup -type=MX $domain`;
if ($? != 0) {
   die "Failed to query MX records for domain $domain: $!\n";
}

# Parse the nslookup output to find the MX records, excluding the priority number
my @mx_records = $nslookup_output =~ /mail exchanger\s+=\s+\d+\s+(\S+)/g;

# Declare $mail_server before using it
my $mail_server;

if (@mx_records) {
   # Get the first mail server (the one with the lowest priority)
   $mail_server = $mx_records[0];  # Assuming nslookup returns the correct MX server
   print "Using mail server: $mail_server\n";
} else {
   die "No MX records found for domain $domain.\n";
}

# Get subject from user input
print "Enter subject: ";
my $subject = <STDIN>;
chomp $subject;

# Temporary file to hold the email body
my $tempfile = "/tmp/email_body.txt";

# Open the text editor to compose the message body
print "Opening text editor ($editor) to compose the message body...\n";
system("$editor", $tempfile);

# Read the contents of the temporary file (email body)
open my $fh, '<', $tempfile or die "Cannot open temporary file $tempfile: $!\n";
my $message_body = do { local $/; <$fh> };
close $fh;

# Remove the temporary file after reading the email body
unlink $tempfile;

# Create a socket to the recipient's mail server (MX record)
my $sock = IO::Socket::INET->new(
   PeerHost => $mail_server,
   PeerPort => 25,
   Proto    => 'tcp',
) or die "Failed to connect to $mail_server:25 - $!\n";

# Read server greeting
my $response = <$sock>;
print $response;  # Print server greeting (e.g., 220 message)

# Send HELO command with the sender's domain (rat.morena.rip)
print $sock "HELO $rdns_domain\r\n";
$response = <$sock>;
print $response;  # Print server response

# Send MAIL FROM command
print $sock "MAIL FROM:<$from_address>\r\n";
$response = <$sock>;
print $response;

# Send RCPT TO command
print $sock "RCPT TO:<$to_address>\r\n";
$response = <$sock>;
print $response;

# Send DATA command
print $sock "DATA\r\n";
$response = <$sock>;
print $response;

# Write the email headers
print $sock "From: $from_address\r\n";
print $sock "To: $to_address\r\n";
print $sock "Subject: $subject\r\n";
print $sock "Date: $date\r\n";
print $sock "MIME-Version: 1.0\r\n";
print $sock "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
print $sock "\r\n";  # Blank line between headers and body

# Write the email body
print $sock "$message_body\r\n";

# End the DATA section with a single period (.)
print $sock ".\r\n";
$response = <$sock>;
print $response;

# Send QUIT command to end the session
print $sock "QUIT\r\n";
$response = <$sock>;
print $response;

# Close the socket
close($sock);

print "Email sent successfully!\n";