2017-10-16 7 views
0
use warnings; 
use MIME::Lite; 
use Net::SMTP; 

### Adjust sender, recipient and your SMTP mailhost 
my $from_address = '[email protected]'; 
my $to_address = '[email protected]'; 
my $mail_host = 'smtp.office365.com'; 

### Adjust subject and body message 
my $subject = 'A message with 2 parts ...'; 
my $message_body = "Here's the attachment file(s) you wanted"; 

### Adjust the filenames 
my $my_file_txt = 'C:\Users\Doni\Documents'; 
my $your_file_txt = 'test1.txt'; 

### Create the multipart container 
$msg = MIME::Lite->new (
    From => $from_address, 
    To => $to_address, 
    Subject => $subject, 
    Type =>'multipart/mixed' 
) or die "Error creating multipart container: $!\n"; 

### Add the text message part 
$msg->attach (
    Type => 'TEXT', 
    Data => $message_body 
) or die "Error adding the text message part: $!\n"; 

### Add the TEXT file 
$msg->attach (
    Type => 'text', 
    Encoding => 'base64', 
    Path => $my_file_txt, 
    Filename => $your_file_txt, 
    Disposition => 'attachment' 
) or die "Error adding file_text: $!\n"; 

##Send 
MIME::Lite->send('smtp',$mail_host,SSL=>0,AuthUser=>'[email protected]',AuthPass=>'******',Debug=>1,Port=>587,Timeout=>60); 
$msg->send; 

la sortie de la commande est:auth SMTP() sur smtp.office365.com

##Here's the output 

SMTP auth() command not supported on smtp.office365.com 

Comment résoudre ce problème? Je suis coincé avec ça.

Répondre

0

surtout, j'ajouté

use Net::SMTP::TLS; 

après les codes MIME :: Lite, j'ai utilisé ci-dessous Mailer:

my $mailer = new Net::SMTP::TLS(
    "smtp.office365.com", 
    Hello =>  'smtp.office365.com', 
    Port =>  587, 
    User =>  '[email protected]', 
    Password=>  '******'); 
$mailer->mail('[email protected]'); 
$mailer->to('[email protected]'); 
$mailer->data; 
$mailer->datasend($msg->as_string); 
$mailer->dataend; 
$mailer->quit; 

Cela a fonctionné comme un charme!

+0

Vous pourriez plutôt utiliser aussi le Net :: SMTP (module de base) et juste faire un '$ mailer-> starttls() 'après avoir établi la connexion. Un module de moins à installer. En outre, vous pouvez simplement faire '$ mailer-> data ($ msg-> as_string)' et ignorer 'datesend' et' dataend'. –

+0

deux bravo pour vous bro @SteffenUllrich –

1
MIME::Lite->send('smtp',$mail_host,SSL=>0,AuthUser=>...,Port=>587 ... 
            ^^^^^^ ^^^^^^ 

Vous essayez d'utiliser l'authentification sans SSL. Ce n'est pas supporté par smtp.office365.com pour des raisons de sécurité comme on peut le voir dans la poignée de main. La réponse à EHLO après les spectacles de connexion initiale non AUTH disponible:

<<< 220 LNXP265CA0010.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 16 Oct 2017 14:36:53 +0000 
>>> EHLO ... 
<<< 250-LNXP265CA0010.outlook.office365.com Hello ... 
<<< 250-SIZE 157286400 
<<< 250-PIPELINING 
<<< 250-DSN 
<<< 250-ENHANCEDSTATUSCODES 
<<< 250-STARTTLS 
<<< 250-8BITMIME 
<<< 250-BINARYMIME 
<<< 250-CHUNKING 
<<< 250 SMTPUTF8 

uniquement lors de la mise à TLS AUTH obtient disponible:

>>> STARTTLS 
<<< 220 2.0.0 SMTP server ready 
>>> EHLO localhost.localdomain 
<<< 250-LNXP265CA0010.outlook.office365.com Hello ... 
<<< 250-SIZE 157286400 
<<< 250-PIPELINING 
<<< 250-DSN 
<<< 250-ENHANCEDSTATUSCODES 
<<< 250-AUTH LOGIN XOAUTH2   <<<<<<<<<<<<< HERE 
<<< 250-8BITMIME 
<<< 250-BINARYMIME 
<<< 250-CHUNKING 
<<< 250 SMTPUTF8 

Malheureusement, il semble que MIME :: Lite ne prend pas en charge l'utilisation de starttls. Voir les réponses sur MIME::Lite - Cannot send mail [SMTP auth() command not supported on smtp.gmail.com] pour des alternatives.

+0

je l'ai déjà suivi et modifié mon code pour ajouter SSL => 1 et utiliser NET :: SMTP (3.10), mais ne fonctionne toujours pas. Quel est le meilleur moyen d'envoyer un email à office365 alors? –

+0

@DoniAndriCahyono: 'SSL => 1' n'utilise pas explicitement TLS (à savoir starttls) mais implicite TLS. Pour me citer: * « Malheureusement, il semble que MIME :: Lite ne supporte pas l'utilisation de starttls Voir les anwers sur MIME :: Lite - ne peut pas envoyer du courrier .... pour des alternatives *. ». –

+0

'MIME :: Lite' ne passe pas l'option' 'SSL' de Net :: SMTP' - voir https://stackoverflow.com/questions/45588971/mimelite-3-030-netsmtp-with-smtps-port-465 – AnFi

0

Pour moi la solution ci-dessus, en utilisant Net::SMTP::TLS, n'a pas fonctionné pour l'envoi de courriels en utilisant MIME::Lite & Office365. J'ai reçu le message d'erreur suivant:

invalid SSL_version specified at /usr/share/perl5/IO/Socket/SSL.pm line 575. 

Cependant, la solution donnée à l'aide hereNet::SMTPS, a fonctionné comme un charme.

est ici l'extrait de code qui a fonctionné pour moi:

my $email = MIME::Lite->new(From => 'John Doe <[email protected]>', 
          To  => $to, 
          Subject => $email_subject, 
          Type =>'multipart/related'); 

$email->attach(Type => 'text/html', 
       Data => $email_body); 

my $smtps = Net::SMTPS->new("smtp.office365.com", Port => 587, doSSL => 'starttls', SSL_version=>'TLSv1'); 

my $username = '[email protected]'; 
my $password = 'myc0mpl1c4t3dpa55w0rd'; 

$smtps->auth ($username, $password) or DIE("Nooooo..Could not authenticate with mail server!!\n"); 

$smtps->mail($username); 
$smtps->to($to); 
$smtps->data(); 
$smtps->datasend($email->as_string()); 
$smtps->dataend(); 
$smtps->quit; 

Hope this helps quelqu'un.