How will i know a url adress is IP address or DNS address in ruby.
Example to clarify question:
IP Address: http://74.125.236.72/
DNS Address: http://google.co.in
Just check it manually?
string =~ /\/\/[0-9.]+\/?$/
Something like below, I am thinking to solve this problem using IPAddr:-
def dns_or_ip_addrs_check(address)
addr = address[/http(?:s)?:\/\/([a-z0-9.]+)\/?/i,1]
begin
require 'ipaddr'
IPAddr.new addr
'ip address'
rescue IPAddr::InvalidAddressError
'dns address'
end
end
dns_or_ip_addrs_check('http://74.125.236.72/') # => "ip address"
dns_or_ip_addrs_check('http://google.co.in') # => "dns address"
Related
I am using netaddr gem to validate network IP range. But I unable to find the way validated given subnet mask is valid?
def valid_ip_range(ip, gateway, subnet_mask)
ip_range = NetAddr::CIDR.create("#{gateway} #{subnet_mask}")
valid_ip_range = NetAddr.range(ip_range.first, ip_range.last)
valid_ip_range.include?(ip_ip)
end
For Valid subnet mask
ip_range = NetAddr::CIDR.create('192.168.1.1 255.255.255.0')
=> #<NetAddr::CIDRv4:0x0000000b334720 #address_len=32, #all_f=4294967295, #hostmask=255, #ip=3232235777, #netmask=4294967040, #network=3232235776, #tag={}, #version=4, #wildcard_mask=4294967040>
For Invalid subnet mask, getting following error
ip_range = NetAddr::CIDR.create('192.168.1.1 255.128.128.0')
ip_range = NetAddr::CIDR.create('192.168.1.1 1.1.1.1')
=> NetAddr::ValidationError: 1.1.1.1 contains '1' bits within the host portion of the netmask.
from /ruby-2.1.1#customerservice-mar/gems/netaddr-1.5.1/lib/validation_shortcuts.rb:182:in `block in validate_netmask_str'
ruby ipaddr
require 'ipaddr'
net1 = IPAddr.new("192.168.2.0/24")
net2 = IPAddr.new("192.168.2.100")
net3 = IPAddr.new("192.168.3.0")
p net1.include?(net2) #=> true
p net1.include?(net3) #=> false
I am trying to send an email using mail gem. But Unfortunately it is not working.
This is my controller.
def create
fn = params["firstname"]
ln = params["lastname"]
email = params["email"]
file = params["file"]
summery = params["summery"]
email_body = "Hello\n This is Your favorite website.\nA I want to personaly say hi."
mail = Mail.new do
from 'someone#gmail.com'
to email
subject "Saying Hi"
body email_body
end
mail.add_file(filename: file.original_filename, content: File.read(file.tempfile.path)) unless file.nil?
mail.deliver!
render json: {message: "A bug has been created", success: true}, status: 201
end
This code is producing this error
Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:
However when I am installing the mailcatcher and configure my controller to send the mail to mailcatcher, I can see the email in my mailcatcher UI.
Mail.defaults do
delivery_method :smtp, address: "localhost", port: 1025
end
Also I have add this two lines to my config/environment/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
From my searches I saw that some people are mentioning that dont send email on development mode, however on this case I really want to test the full capability.
Update
As #Uzbekjon and #SimoneCarletti suggested I change my code to use the ActionMailer. I created the a file in app/mailer/ and I am calling that from my controller.
def create
fn = params["firstname"]
ln = params["lastname"]
email = params["email"]
file = params["file"]
summery = params["summery"]
email_body = "Hello\n This is Your favorite website.\nA I want to personaly say hi."
WelcomeMailer.welcome(fn, ln, email, file, email_body).deliver_now
render json: {message: "An Email has been send", success: true}, status: 201
end
and This is my mailer
class WelcomeMailer < ActionMailer::Base
default from: "someone#yahoo.com"
def welcome(first_name, last_name, email, file, email_body)
attachments["#{file.original_filename}"] = File.read("#{file.tempfile.path}")
mail(
to: email,
subject: 'Welcome to My Awesome Site',
body: email_body
)
end
end
However I am still getting the same error.
Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:
Answer
So I found the solution. Yes you need to use the ActionMailer. After that you need to go to the config/environments/development.rb , and modify and add these lines:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "YOUR EMAIL",
:password => "YOUR Password",
:authentication => "plain",
:enable_starttls_auto => true
}
Also If Gmail complained about this:
Net::SMTPAuthenticationError - 534-5.7.9 Application-specific password required
Go to this link and let less secure application access Gmail.
Other configurations are available for other services like Yahoo. Just Google it.
Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:
Looks like mail gem is trying to connect to your local smtp server on port 25. Most probably you don't have the service running and receiving connections on port 25.
To solve, install and run sendmail or postfix on your machine.
PS. Use ActionMailer.
You don't have a mail server running on port 25. You can install postfix and start the server using
sudo postfix start
And then modify the settings in config/environments/development.rb
config.action_mailer.delivery_method = :sendmail
Hope this helps.
My user has email with this format: "-user-#domain.com". Mailgun validation succeeded but Rails couldn't send email to the address. I'm using SMTP with Mandrill.
This is the error message:
/home/johnny/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/net/smtp.rb:948:in `check_response': 401 4.1.3 Bad recipient address syntax (Net::SMTPServerBusy)
Do you have any idea?
Thanks in advance.
Updated:
This sample code (with valid SMTP configuration) would raise the error:
#!/usr/bin/env ruby
require 'mail'
options = {
address: "smtp.mandrillapp.com",
port: 587,
domain: "mydomain.com",
authentication: "login",
user_name: "myemail#mydomain.com",
password: "mypassword",
enable_starttls_auto: false
}
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
from 'valid.email#domain.com'
to "-test-#domain.com"
subject 'Testing sendmail'
body 'Testing sendmail'
end
Even if the starting dash in the email address is valid, most mail servers do not accept such emails due to restrictions for command line arguments.
A quick fix you can try is wrapping the email address with angle brackets:
Mail.deliver do
from 'valid.email#domain.com'
to "<-test-#domain.com>"
subject 'Testing sendmail'
body 'Testing sendmail'
end
i am writing a ruby script to send email using 'mail' gem.
and my smtp settings on my local machine:
mailer_options:
address: smtp.gmail.com
port: 465
domain: gmail.com
user_name: example#gmail.com
password: example_password
authentication: :login
enable_starttls_auto: true
ssl: true
i am trying to send the like this :-----
Mail.deliver do
to 'receiver#gmail.com'
from 'sender#gmail.com'
subject 'Test Mail'
text_part do
body 'Hello World!!!!!'
end
end
the mail is send successfully but when i open the email i see sender email id as
example#gmail.com instead of sender#gmail.com, why it is so i am not able to figure out.
thanks for any comment and answers.
This is often done by your SMTP server and is beyond your control. You could try using a different SMTP provider like Sendgrid if Google isn't working out for you.
Correct answers above, it's not your code, it's the Gmail SMTP servers that do this. I work for SendGrid and if you wanted to change this over to using SendGrid (or any other provider for that matter) then you can do it really easily. Our free plan can send 400 emails a day and is fine for local development.
Your code would change as follows:
mailer_options:
address: smtp.sendgrid.net
port: 587
domain: yourdomain.com
username: your_username
password: your_password
authentication: plain
enable_starttls_auto: true
You don't need to have SSL set at this stage. From here you can use your original Mail.deliver method.
You'll find you can now send from the sender#yourdomain.com address, or whichever address you specify in the from attribute.
There's further Ruby & SendGrid details in the SendGrid documentation.
google does not allow sender email masking. This is done by GMAIL's server. Not by your rails code!!. It always uses email address of gmail account you are using as "from_email".
Your best alternative might be "Mandrill" (12000 emails free / month). They allow email routing in the way you want.
Please set sender default name instead of 'sender#gmail.com' at below :
class UserMailer < ActionMailer::Base
default from: 'sender#gmail.com'
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
mail(to: #user.email, subject: 'Welcome to My Awesome Site')
end
end
Good afternoon,
I'm facing a problem about the way to send email through my application hosted on hostingrails.
In my config/initializers I added a file "setup_mailer" containing this
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
# Production
if Rails.env.production?
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t -f support#xxxxx.xxx'
}
end
and my mailer is as below
class Notification < ActionMailer::Base
def subscription_confirmation(user)
setup_email(user)
mail(:to => #recipients, :subject => "blabla")
end
protected
def setup_email(user)
#recipients = user.email
#from = "support#xxxxx.xx"
headers "Reply-to" => "support#xxxxx.xx"
#sent_on = Time.now
#content_type = "text/html"
end
end
It seems to work very fine on my local machine. But in production, emails are not sent properly and I receive this message in my support inbox.
A message that you sent using the -t command line option contained no
addresses that were not also on the command line, and were therefore
suppressed. This left no recipient addresses, and so no delivery could
be attempted.
If you have any idea, the support seems cannot help me, hope some of you'll have ideas or config files from hostingrails to share.
Thank you,
albandiguer
I had exactly the same problem - resolved it by removing the -t from the sendmail_settings.
I haven't looked much further to investigate other implications, but at least it works. But from the man page:
-t Extract recipients from message headers. These are added to any
recipients specified on the command line.
With Postfix versions prior to 2.1, this option requires that no
recipient addresses are specified on the command line.
So maybe just a difference in Postfix versions?
Try calling to_s on user.email or specifying user email as "#{user.email}"