Email does not send with perl MIME::Lite using smtp - perl

I am trying to send an email using MIME::Lite but the email will not send and I am not getting any errors.
Code:
my $subject = $Config->{email}->{subject};
my $from_email = $Config->{email}->{from_email};
my $message = $Config->{email}->{message};
my $smtp_server = $Config->{email}->{smtp_server};
my $msg = MIME::Lite->new
(
Subject => $subject,
From => $from_email,
To => $email,
Type => 'text/html',
Data => $message
);
$msg->send('smtp' ,$smtp_server );

Not much to go on there ...
Maybe try debugging?
$msg->send( 'smtp', $smtp_server, Debug=>1 );
And check all the values in $Config->{email} are as you would expect.

Related

Email text has incorrect default encoding in client program

I am trying to send an email containing Russian text and subject in utf-8 encoding. Email is being received, when I open it in the web interface of gmail, everything is correct. But when I open the email in "The bat" client, the encoding is incorrect by default (I can set it manually to utf-8 though):
Subject: "Hello. Текст"
Body: "test email. Русский текст"
Instead of:
Subject: "Hello. Текст"
Body: "test email. Русский текст"
Code:
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use MIME::Base64 qw( encode_base64 );
use open ':std', ':encoding(UTF-8)';
sub send_email
{
my $email_from = shift;
my $email_to = shift;
my $subject = shift;
my $message = shift;
my $smtpserver = 'smtp.gmail.com';
my $smtpport = 465;
my $smtpuser = 'user#gmail.com';
my $password = 'secret';
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
sasl_username => $smtpuser,
sasl_password => $password,
debug => 1,
ssl => 1,
});
my $email = Email::Simple->create(
header => [
To => $email_to,
From => $email_from,
Subject => $subject,
],
body => $message,
);
$email->header_set( 'Content-Type' => 'text/html' );
$email->header_set( 'charset' => 'UTF-8' );
sendmail($email, { transport => $transport });
}
my $body = Encode::encode('utf-8', 'test email. Русский текст');
my $subject = Encode::encode('utf-8', 'Hello. Текст');
send_email('user#gmail.com', 'user#gmail.com', $subject, $body);
How to tell the email clients that the encoding is utf-8?
Give Email::Sender/Net::SMTP string of bytes
Email::Sender (Net::SMTP) expects bytes (see answer mentioned by user4035):
my $msg = $email->as_string();
utf8::encode($msg) if utf8::is_utf8($msg);
sendmail($msg, ...);
Email body encoding
Set all three MIME headers for "raw" utf-8 email body:
(You may use text/html instead of typical text/plain)
$email->header_set( 'MIME-Version' => '1.0' );
$email->header_set( 'Content-Type' => 'text/plain; charset=utf-8' );
$email->header_set( 'Content-Transfer-Encoding' => '8bit');
Your SMTP server should accept it and conduct conversions from "raw" (8-bit) utf-8 to another email encoding if necessary. Most modern email servers do it.
See Steffen Ullrich comment about notable exceptions among email providers [1&1 (GMX)].
Email headers encoding:
$email->header_raw_set( 'Subject' => Encode::encode('MIME-Header',$subject));
Debug procedure
Create minimal Email::Simple message, print it on utf-8 terminal
(print $email->as_string();) and post the result.

How to set multiple CC email recipients one message using webform in drupal 7

My website using webform in drupal 7 application.
I have two queries
I can added a list for To email recipients one message. Email will receiving all the recipients but mails sending individual. I cant see in the to list group.
How to set multiple CC email recipients one message ? Here am using 'CC' => 'xx#yyyy.com, xx1#yyyy1.com, xx2#yyyy2.com' in theme_mail_headers under "$headers.
function MODULENAME_mail($key, &$message, $params) {
if($key === 'MODULEcase') {
$header = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'Cc' => implode(',', $params['Cc']),
);
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
break;
}
}
Then prepare data and pass to drupal_mail function
$mail_content = "Hello There!";
$params = array('body' => $mail_content);
$params['cc'] = array(
'cc_user1#example.com',
'cc_user2#example.com',
'cc_user3#example.com',
);
$to = 'touser#example.com';
$from = 'no-reply#example.com';
$mail = drupal_mail('MODULENAME', 'MODULEcase', $to, language_default(), $params, $from);
This is it. Enjoy :-)

People in cc are not receiving the mail using MIME::Lite api

Please find below my code snippet that send a mail to person and his friend in cc.
In the cc list I also have a DL.
use MIME::Lite;
$to = 'ABC#DOMAIN1.com';
$from = 'MAILER#DOMAIN2.com';
$subject = 'How are you doing';
$message = 'This is test email sent by Perl Script';
my #cc=('XYZ#DOMAIN2.com','DL#DOMAIN2.com');
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc =>\#cc,
Subject => $subject,
Data => $message
);
$msg->send('smtp','smtpserver', Timeout => 60 );
print "Email Sent Successfully\n";
The problem is the person and DL is cc are not receiving the mails.
Is there any log in the api MIME::Lite where I can check what is the error(if any)Or what do you think the problem can be?
Perhaps you want to take a closer look at the documentation for MIME::Lite. Here's the first example from the synopsis.
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => 'me#myhost.com',
To => 'you#yourhost.com',
Cc => 'some#other.com, some#more.com',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
The Cc parameter here is sent as a text string containing comma-separated email addresses. You are passing in a reference to an array of email addresses.

MIME::Lite - Mail to multiple recipients [duplicate]

Please find below my code snippet that send a mail to person and his friend in cc.
In the cc list I also have a DL.
use MIME::Lite;
$to = 'ABC#DOMAIN1.com';
$from = 'MAILER#DOMAIN2.com';
$subject = 'How are you doing';
$message = 'This is test email sent by Perl Script';
my #cc=('XYZ#DOMAIN2.com','DL#DOMAIN2.com');
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc =>\#cc,
Subject => $subject,
Data => $message
);
$msg->send('smtp','smtpserver', Timeout => 60 );
print "Email Sent Successfully\n";
The problem is the person and DL is cc are not receiving the mails.
Is there any log in the api MIME::Lite where I can check what is the error(if any)Or what do you think the problem can be?
Perhaps you want to take a closer look at the documentation for MIME::Lite. Here's the first example from the synopsis.
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => 'me#myhost.com',
To => 'you#yourhost.com',
Cc => 'some#other.com, some#more.com',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
The Cc parameter here is sent as a text string containing comma-separated email addresses. You are passing in a reference to an array of email addresses.

Perl Net::SMTP Email Size issue while using html format

I am sending an email via SMTP in perl . The email contains some tables,links and lists.
I am using html format data.
$smtp->data();
$smtp->datasend("MIME-Version: 1.0\nContent-Type: text/html; charset=UTF-8 \n\n<H1>");
$smtp->datasend("$message");
...
$smtp->dataend();
$smtp->quit;
Sometimes the email size is too large around 1mb. Is there any way I can reduce the size of email without reducing the amount of data.I do not want the message as an attachment. I use outlook to open the mails.
You should use Mail::Sender for sending attachments through email
#!/usr/bin/perl
use Mail::Sender
$to = 'email1#example1.com,email2#example2.com';
$sender =new Mail::Sender {
smtp => 'smtp.mailserver.com',
from => 'script#somedomain.com,
});
$subject = 'This is a Test Email';
$sender->OpenMultipart({
to => "$to",
subject => "$subject",
});
$sender->Body;
$sender->SendLineEnc("Test line 1");
$sender->SendLineEnc("Test line 2");
$sender->Attach({
description => 'Test file',
ctype => 'application/x-zip-encoded',
encoding => 'Base64',
disposition => 'attachment;
filename="File.zip"; type="ZIP archive"',
file => "$file",
});
$sender->Close();
exit();
or using MIME::Lite
use MIME::Lite;
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "$!\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";
### Send the Message
$msg->send('smtp', $mail_host, Timeout=>60);