Using Mail I am able to send messages (HTML) perfectly using this script email_handler.rb on Ruby, please how can i add an attachment (eg pdf) to my message, i have tried it for 2 weeks now, please i need help guys please. Here goes the email handler scipt below.
"""
Author: daemon
Description: Module to send bulk emails using any email address, with functional programming
"""
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
and this is the main script (main.rb) that handles initiates the sending sequence once you enter it in the cmd prompt..
"""
Author: daemon
Description: Send bulk emails using any email address
"""
# 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 = "MAIL@MAIL.COM"
$custom_sender_email = "no-reply.#randstring@MAIL.COM"
$email_subject = "TEST"
$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
this scripts works fine and sends out html messages perfectly as of now.
PLEASE HOW CAN I EDIT THIS SCRIPTS TO SEND AN ATTACHMENT? e.g pdf I HAVE TRIED EVERYTHING I KNOW, very important to me, please help me guys, Wish you goodluck in all you do! P/S: I'm new to Ruby, please be kind
Aucun commentaire:
Enregistrer un commentaire