ActionMailer无法识别的身份验证类型

时间:2017-04-20 08:46:04

标签: ruby actionmailer

我在php中使用此代码并且可以正常工作:

$host = "smtp.office365.com";
$username = "a@a.a";
$password = "a";
$port = 587;
$smtpSecure = 'tls';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = $host;
$mail->Port       = $port;
$mail->SMTPSecure = $smtpSecure;
$mail->SMTPAuth   = true;
$mail->Username = $username;   
$mail->Password = $password;
$mail->SetFrom('a@a.com', 'B2b');
$mail->SMTPDebug  = 4;
$mail->Debugoutput = 'html';
$mail->IsHTML(true);
$mail->MsgHTML("test");
$mail->addAddress('b@b.com', 'Vaktor');
$mail->Send();

它返回:

Connection: opening to smtp.office365.com:587, timeout=300, options=array ()
Connection: opened
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "220 VI1PR07CA0176.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 20 Apr 2017 08:14:05 +0000"
SERVER -> CLIENT: 220 VI1PR07CA0176.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 20 Apr 2017 08:14:05 +0000
CLIENT -> SERVER: EHLO localhost
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]"
SMTP -> get_lines(): $str is "250-SIZE 157286400"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400"
SMTP -> get_lines(): $str is "250-PIPELINING"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING"
SMTP -> get_lines(): $str is "250-DSN"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN"
SMTP -> get_lines(): $str is "250-ENHANCEDSTATUSCODES"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES"
SMTP -> get_lines(): $str is "250-STARTTLS"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS"
SMTP -> get_lines(): $str is "250-8BITMIME"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME"
SMTP -> get_lines(): $str is "250-BINARYMIME"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME"
SMTP -> get_lines(): $str is "250 CHUNKING"
SERVER -> CLIENT: 250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250 CHUNKING
CLIENT -> SERVER: STARTTLS
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "220 2.0.0 SMTP server ready"
SERVER -> CLIENT: 220 2.0.0 SMTP server ready
CLIENT -> SERVER: EHLO localhost
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]"
SMTP -> get_lines(): $str is "250-SIZE 157286400"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400"
SMTP -> get_lines(): $str is "250-PIPELINING"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING"
SMTP -> get_lines(): $str is "250-DSN"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN"
SMTP -> get_lines(): $str is "250-ENHANCEDSTATUSCODES"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES"
SMTP -> get_lines(): $str is "250-AUTH LOGIN"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN"
SMTP -> get_lines(): $str is "250-8BITMIME"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN250-8BITMIME"
SMTP -> get_lines(): $str is "250-BINARYMIME"
SMTP -> get_lines(): $data is "250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN250-8BITMIME250-BINARYMIME"
SMTP -> get_lines(): $str is "250 CHUNKING"
SERVER -> CLIENT: 250-VI1PR07CA0176.outlook.office365.com Hello [83.52.45.225]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN250-8BITMIME250-BINARYMIME250 CHUNKING
Auth method requested: UNKNOWN
Auth methods available on the server: LOGIN
Auth method selected: LOGIN
CLIENT -> SERVER: AUTH LOGIN
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "334 VXNlcm5hbWU6"
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: YmlAbGl6em8ubmV0
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "334 UGFzc3dvcmQ6"
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: MTIzQmkzMjE=
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "235 2.7.0 Authentication successful target host DB5PR07MB0776.eurprd07.prod.outlook.com"
SERVER -> CLIENT: 235 2.7.0 Authentication successful target host DB5PR07MB0776.eurprd07.prod.outlook.com
CLIENT -> SERVER: MAIL FROM:<a@a.a>
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "250 2.1.0 Sender OK"
SERVER -> CLIENT: 250 2.1.0 Sender OK
CLIENT -> SERVER: RCPT TO:<b@b.b>
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "250 2.1.5 Recipient OK"
SERVER -> CLIENT: 250 2.1.5 Recipient OK
CLIENT -> SERVER: DATA
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "354 Start mail input; end with <CRLF>.<CRLF>"
SERVER -> CLIENT: 354 Start mail input; end with <CRLF>.<CRLF>
CLIENT -> SERVER: Date: Thu, 20 Apr 2017 08:14:03 +0000
CLIENT -> SERVER: To: Victor <b@b.b>
CLIENT -> SERVER: From: B2b <a@a.a>
CLIENT -> SERVER: Subject: 
CLIENT -> SERVER: Message-ID: <08f3700c117905f7e610eba801ee6741@localhost>
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: multipart/alternative;
CLIENT -> SERVER: boundary="b1_08f3700c117905f7e610eba801ee6741"
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
CLIENT -> SERVER: 
CLIENT -> SERVER: This is a multi-part message in MIME format.
CLIENT -> SERVER: 
CLIENT -> SERVER: --b1_08f3700c117905f7e610eba801ee6741
CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
CLIENT -> SERVER: 
CLIENT -> SERVER: prueba
CLIENT -> SERVER: 
CLIENT -> SERVER: 
CLIENT -> SERVER: --b1_08f3700c117905f7e610eba801ee6741
CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
CLIENT -> SERVER: 
CLIENT -> SERVER: prueba
CLIENT -> SERVER: 
CLIENT -> SERVER: 
CLIENT -> SERVER: 
CLIENT -> SERVER: --b1_08f3700c117905f7e610eba801ee6741--
CLIENT -> SERVER: 
CLIENT -> SERVER: .
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "250 2.6.0 <08f3700c117905f7e610eba801ee6741@localhost> [InternalId=82575041234541, Hostname=DB5PR07MB0776.eurprd07.prod.outlook.com] 2837 bytes in 0.502, 5.511 KB/sec Queued mail for delivery"
SERVER -> CLIENT: 250 2.6.0 <08f3700c117905f7e610eba801ee6741@localhost> [InternalId=82575041234541, Hostname=DB5PR07MB0776.eurprd07.prod.outlook.com] 2837 bytes in 0.502, 5.511 KB/sec Queued mail for delivery
CLIENT -> SERVER: QUIT
SMTP -> get_lines(): $data is ""
SMTP -> get_lines(): $str is "221 2.0.0 Service closing transmission channel"
SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
Connection: closed

Ruby中的代码:

配置

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    :address    =>              'smtp.office365.com',
    :port   =>                 587,
    :user_name  =>            'a@a.a',
    :password   =>             'a',
    :authentication =>       :login,
    :enable_starttls_auto   => true
}

观察

class UserObserver < ActiveRecord::Observer
  def after_create(user)
    UserNotifier.deliver_signup_notification(user)
  end

的ActionMailer

def signup_notification(user)
    setup_email(user)
    @subject    += _('New B2B Account')
    the_site = get_proper_url(user)
    @body[:url]  = "http://#{the_site}/activate/#{user.activation_code}"
    @body[:current_user] = user
end

def setup_email(user)
  the_site = get_proper_url(user)
  @recipients  = "#{user.email}"
  @from        = "#{ADMINEMAIL}"
  @subject     = "[#{the_site}] "
  @bcc        = "#{ADMINEMAIL}"
  @sent_on     = Time.now
  @body[:user] = user
end

Ruby回复了这个:

Net::SMTPAuthenticationError in UsersController#create

504 5.7.4 Unrecognized authentication type [HE1PR02CA0117.eurprd02.prod.outlook.com]
RAILS_ROOT: C:/alhambrab2b/config/..

Application Trace | Framework Trace | Full Trace
c:/ruby/lib/ruby/1.8/net/smtp.rb:577:in `auth_plain'
c:/ruby/lib/ruby/1.8/net/smtp.rb:571:in `__send__'
c:/ruby/lib/ruby/1.8/net/smtp.rb:571:in `authenticate'
c:/ruby/lib/ruby/1.8/net/smtp.rb:411:in `do_start'
c:/ruby/lib/ruby/1.8/net/smtp.rb:378:in `start'
c:/ruby/lib/ruby/1.8/net/smtp.rb:316:in `start'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.3.6/lib/action_mailer/base.rb:565:in `perform_delivery_smtp'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.3.6/lib/action_mailer/base.rb:451:in `send'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.3.6/lib/action_mailer/base.rb:451:in `deliver!'
c:/ruby/lib/ruby/gems/1.8/gems/actionmailer-1.3.6/lib/action_mailer/base.rb:333:in `method_missing'
C:/alhambrab2b/app/models/user_observer.rb:3:in `after_create'
C:/alhambrab2b/app/controllers/users_controller.rb:95:in `create'
C:/alhambrab2b/app/controllers/users_controller.rb:94:in `create'

我尝试将config.action_mailer.smtp_settings.authentication更改为:plain和:cram_md5并始终返回相同的内容。

1 个答案:

答案 0 :(得分:0)

尝试将openssl_verify_mode设置为"none",例如:

conf.smtp_settings = {
  address: "<server>", 
  domain: "<domain>", 
  user_name: "<user>", 
  password: "<your_password>", 
  authentication: :login, 
  openssl_verify_mode: "none"
}