1717701598

Ruby email bot with PONY


Creating a Email bot to annoy people (or any other likley unproductive usecase) <br><br> This is all done in ruby using the Pony gem. I found this gem a while ago and was able to set everything up so that It can send emails at a certian time, or on a certian trigger of your choosing. <br><br> This post is broke up into 2 instruction parts. The first will explain the configuration of pony which is very simple along with its supporting code. <br><br> The second part of the post is how to get auth aswell as some ideas for something that you can add to your project to flesh out your project if you want to add them later. All in all its not a complicated project. Its something thats nice to know in-case you want to integrate it into something of yours in the future. <br><br> First off we need the ruby gem: [pony](https://github.com/benprew/pony). ```gem install pony``` Simple as that. I also have the date gem if you want to add that functionality. Next up we will need to create some files to store info at. One will store the content of our email. You can change this content at any time. Im going to call it email_content.txt. I will also make another email_count.txt so that i can append "this is the 5th newsletter" or whatever you want to the end of your email. You dont need to do this if that isnt a feature you want to include. <br><br> ``` require 'pony' require 'date' # Method to read email content from a text file def read_email_content(file_path) File.read(file_path) end # Method to keep track of the number of emails sent, we need this because if you set up a scheduler (i use cron jobs for most stuff like this) to have it say go off at midnight/noon, after the email is sent the ruby application will close out and any data in count will be erased from memory. We need somewhere to keep the current increment at during the inactive time. def increment_email_count(file_path) if File.exist?(file_path) count = File.read(file_path).to_i count += 1 else count = 1 end File.write(file_path, count) count end ``` <br><br> The next thing that we need to do is get the configuration set up right. This is where you enter the people you want emails sent too aswell as your auth to send the email. You can get more details about these parameters on the pony github linked above. <br><br> ``` # Email configuration Pony.options = { via: :smtp, via_options: { address: 'smtp.gmail.com', #replace with your providers address port: '587', # leave this alone unless you know what your doing enable_starttls_auto: true, user_name: '<your_email@gmail.com>', password: '<your_password>', # this is not your email password. With google it will provide you a KEY to use for "less secure apps", put that here. authentication: :plain, # :plain, :login, :cram_md5, no auth by default domain: "localhost.localdomain" # the HELO domain provided by the client to the server } } ``` <br>There are notes all throughout this so make sure to read them to make sure you know what info to fill in. I will go into more debth about password & auth later. <br><br> Finally we get to the email sending part. We grab the content and count aswell as call on that increment count function that we wrote at beginning. <br> ``` # Path to the email content file and email count file email_content_file = 'email_content.txt' email_count_file = 'email_count.txt' # Read email content email_content = read_email_content(email_content_file) # Increment and get the email count email_count = increment_email_count(email_count_file) # Prepare the email body with the email count email_body = "#{email_content}\n\nEmails sent so far: #{email_count}" ``` <br> Pretty simple. Grabs the info of your choosing to prepare for the last bit. The last bit is just telling pony to send the email that we have generated now. <br> ``` # Send the email Pony.mail( to: '<recipient_email@example.com>', from: '<your_email@gmail.com>', # same as above subject: 'This is an email', body: email_body ) puts "Email sent successfully with the content:\n#{email_body}" # this will be the output in your terminal when this is ran, it wont be included in the email body. ``` Pony handles the rest talking to your email provider and doing all the smtp stuff. Note: you can also just use smtp as its part of the standard ruby library so you dont need gems or anything but pony makes it prettier and nice to auth. <br><br> If you tried running this you will notice an error that says ``` `finish': 535-5.7.8 Username and Password not accepted. For more information, go to (Net::SMTPAuthenticationError) ``` Im using gmail to explain this next part. These instructions change depending on your provider. Also a warning. If you use this bot alot google might just mark all of your emails as spam. You can use this program in some sort of loop so that it floods people with tons of emails which might be fun to do to your friends but please dont use your main google account. Dont be stupid, wear a cond- make an ALT account. <br> First off you will need 2 step verification on for this acc. Just use a burner phone if you dont care and this is an alt account. You should really have 2 step on already but anyways. Somewhere in the [security settings](https://myaccount.google.com/) there will be an option to generate an app code. I dont bother looking and just used the search bar at top. "App passwords" works for me. <br><br> You will need to pick an app name. Email-bot or whatever. This is useful if you have multiple of these going you can check and see activity and sort by which bot. It will output a code. If it gives you the option select plain. If you select a different option you will need to change ``` authentication: :plain ``` line in your config section. Once you have your passkey enter that into your password var in the config inside single quotes. You should now be able to send emails. --- Extra stuff:<br> You can make the send to a array of other peoples addresses. You should store this array in another file so that it dont get forgotten once the emailer code stops like the rest of the long-term data. You can have another bot have access to this email array file if you want to automate new subscribers. Keep in mind this will send 1 email with all those people. It may be better to make a loop go through them all and send individual emails rather then add them all to 1 big email. You will know what I mean if you've ever been in a large thread with people that dont know difference between reply and reply-all buttons. Keep in mind gmail has limits on amount of emails that can be sent per day. <br><br> Lets say you dont want to write the email at all. You can make it do that for you. Let say for example i want to grab the most hot post on chat-to.dev. You could have curl grab [the hot post](https://chat-to.dev/hot) or [the newest post](https://chat-to.dev/new) and filter through the response and grab all the title elements and output it all into the email_content.txt file. <br><br> Another cool thing you can create is a trigger, rather then just setting it to go off at a certain time. Say if you got a home server and something goes wrong. It can send you an email based on that trigger and pass the status as the email content. (please use real server status & automation software). <br><br> If you have some other cool ideas to feed into the email_content leave a comment. Anyways let me know if you need help. I will be found in the ruby chat.

(1) Comments
tomcat
tomcat
1717706109

## amazing!!


Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]