尝试使用ruby发送批量电子邮件时出错

时间:2018-08-06 17:26:45

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rubygems

我正在尝试使用ruby脚本发送一些电子邮件,并且在发送到大约几封电子邮件后出现错误,然后给出错误,如下所示。注意:我正在使用office365作为smtp提供程序。 这是包含以下脚本的初始文件夹 folder containing the scripts

这是下面的电子邮件处理程序脚本

require './savmail/lib/mail.rb'

require './utils.rb'

RECIPIENT_HOLDER = "%0%"

module EmailHandler
    def self.format_email(email: '', recipient: '')
        # fill in all the placeholders
        return email.gsub(RECIPIENT_HOLDER, recipient)
    end


    def self.compose_email(recipient: nil, sender: nil, sender_name: nil, subject: '', html: nil, text: '')
        # Save recipient so other threads don't touch
        Utils.save_recipient(email: recipient)
        text_content = text
        html_content = format_email(email: html, recipient: recipient)

        mail = Mail.new do
            to      recipient
            from    sender_name + " <" + sender + ">"
            subject subject
        end

        text_part = Mail::Part.new do
            body text_content
        end

        html_part = Mail::Part.new do
            content_type 'text/html; charset=UTF-8'
            body html_content
        end

        mail.text_part = text_part
        mail.html_part = html_part

        return { 'msg'=> mail, 'recipient'=> recipient, 'sender'=> sender }
    end

    def self.email_login(sender_tuple: [], mail: '')
        smtp_login = sender_tuple[0]
        smtp_password = sender_tuple[1]
        smtp_host = sender_tuple[2]
        smtp_port = sender_tuple[3]
        limit = sender_tuple[4]

        begin
            # server = smtplib.SMTP(smtp_host, smtp_port)
            # server.starttls()
            # server.login(smtp_login, smtp_password)
            options = { 
                :address              => smtp_host,
                :port                 => smtp_port,
                # :domain               => 'your.host.name',
                :user_name            => smtp_login,
                :password             => smtp_password,
                :authentication       => 'login',
                # :enable_starttls_auto => true  
            }
            mail.delivery_method :smtp, options

            return mail
        rescue Exception => e
            puts 'Failed to Login to ' + smtp_login
            return false
        end
    end

    def self.send_email(payload: nil, sender_address: nil, recipient: nil, server: nil)
        begin
            payload.deliver
            puts 'Sent from '+sender_address+' to '+recipient
            return payload
        rescue Exception => e
            Utils.save_recipient(email: recipient, failed: true)
            puts 'Email Failed to Send from '+sender_address+' to '+ recipient
            return false
        end
    end
end

然后这是主要的发件人脚本

# from email_handler import compose_email, send_email, email_login
require './email_handler.rb'
require './utils.rb'
include EmailHandler

# Change Subject for all emails here
$custom_sender_name = "Africonso Tradings"
$custom_sender_email = "admin@africonsotradings.com"
$email_subject = "test mail."
$threads_total = 10

# These files have to exist
$army_file_path = './army.txt'
$email_file_path = './msg.html'
$senders_file_path = './senders.txt'


def mail_savage(recipients, senders, payload, custom_sender_email, custom_sender_name, email_subject, active_limit: nil, active_server: nil)
    ''' Login as one sender and send emails until the limit is reached or the email stops working,
    then do the same for the rest
    '''
    # Get first recipient from array
    sender = senders[0]

    # sender_email, smtp_password, smtp_host, smtp_port, limit = sender
    sender_email = sender[0]
    smtp_password = sender[1]
    smtp_host = sender[2]
    smtp_port = sender[3]
    limit = sender[4]
    mail_limit = active_limit or limit.to_i

    # if !recipients
    #     return true
    # end

    # Make sure we haven't emailed this user before
    for recipient in recipients
        if Utils.is_recipient_saved(email: recipient)
            # Move to next email if sent already
            next
        end

        # compose email
        # mail, recipient_email, csender_email = compose_email(recipient=recipient, sender=sender_email, sender_name=custom_sender_name, subject=email_subject, html=payload)
        mail_data = EmailHandler.compose_email(recipient: recipient, sender: sender_email, sender_name: custom_sender_name, subject: email_subject, html: payload)
        mail = mail_data['msg']
        recipient_email = mail_data['recipient']
        csender_email = mail_data['sender']


        # Login as sender if fail dump sender
        server = false
        while server == false do
            server = EmailHandler.email_login(sender_tuple: sender, mail: mail)
        end

        # Send email
        # print('Sending from %s to %s' % (sender_email, recipient))
        if !EmailHandler.send_email(payload: server, sender_address: sender_email, recipient: recipient_email, server: server)
            # email failed to send
            # Call it again without this sender and with the current limit
            server = EmailHandler.email_login(sender_tuple: sender, mail: server)
        end
    end

    # if server
    #     server.finish
    # end
    return true
end


def main()
    ''' Send email from msg.html to all addresses in army.txt
    with the addresses in senders.txt
    '''

    senders_all = Utils.get_sender_addresses(path: $senders_file_path)
    recipients_all = Utils.get_army_addresses(path: $army_file_path)
    html_content = Utils.get_email_content(path: $email_file_path)

    threads = []

    for th in 0..$threads_total-1
        threc = recipients_all[th..recipients_all.length-1]

        if threc != nil && threc.length > 0

            threads << Thread.new {
                if th == 0
                    sleep 2
                end
                mail_savage(recipients_all, senders_all, html_content, $custom_sender_email, $custom_sender_name, $email_subject, active_limit: nil, active_server: nil)
            }
        end
        # thread.join
        # thread.start()
    end

    threads.each do |t|
        t.join
    end
end

main

因此,当我尝试发送邮件时,首先它会多次发送一封邮件,然后其余电子邮件将失败,如下所示 cmd main

然后发生这种情况 error msg

现在我的问题是,为什么在成功发送了很少的邮件后,电子邮件却失败了?我认为脚本中某处有错误。 有帮助吗?

现在我添加了看跌期权e,并且我得到下面的错误代码。请注意,它发送约20封邮件,其余邮件失败。 error i get now

我还能如何使代码接受除365以外的其他smtp?

1 个答案:

答案 0 :(得分:0)

错误日志中的相关内容似乎是550 5.1.8 Access denied, bad outbound sender。如您所提到的,您将Office 365用作smtp提供程序,Microsoft的支持网站上有一篇帖子暗示这是由于您的sending address being suspected of sending spam造成的。

由于您尝试快速连续发送许多电子邮件,因此这似乎是您遇到的问题。

如果所有电子邮件都使用相同的电子邮件,则可以考虑发送一封所有地址均为密件抄送的邮件。否则,您可能需要与Office 365管理员联系,以查看是否有可以调整的设置。您也可以考虑使用事务电子邮件提供商(例如,SendGrid,Mailchimp或什至似乎是Microsoft的事务电子邮件系统Dynamics Marketing)来处理这些类型的事情,并且这些事务通常具有批处理邮件操作,不会占用您的服务器。