9 Replies - 2882 Views - Last Post: 07 July 2011 - 11:22 AM Rate Topic: -----

#1 globus25  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 19
  • Joined: 07-July 11

Sending email - gmail(smtp) - error

Posted 07 July 2011 - 05:13 AM

I just started programming in ruby and wanted to write gmail mail sender...
so here is the code:
require 'net/smtp'

message = <<MESSAGE_END
From: XXX <xxxx@gmail.com>
To: XXXXX <xxxx@gmail.com>
Subject: Test

This is a test e-mail message.
MESSAGE_END
begin
Net::SMTP.new 'smtp.gmail.com', 587
Net::SMTP.enable_starttls
Net::SMTP.start('gmail.com','xxxx@gmail.com', 'xxx') do |smtp|
  smtp.send_message message, 'xxxx@gmail.com', 
                             'xxxxxx@gmail.com'
end
rescue Exception => e  
  print "Exception occured: " + e.to_s  
end  



The error:
Exception occured: undefined method `enable_starttls' for Net::SMTP:Class


I found solution over the web it is about installing some additional gem but I cannot make it work.
Is there any other way to make it work?

I`m using Netbeans 7.0 as "compiler".

Is This A Good Question/Topic? 0
  • +

Replies To: Sending email - gmail(smtp) - error

#2 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 05:37 AM

Dude, get one of the beginners books and start reading. PHP style of coding (copy/paste) won't work with ruby. It seems that you have no idea what you're doing.
Was This Post Helpful? 0
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 10:02 AM

Why not at least suggest something useful like the ruby documentation for net::smtp??

http://www.ruby-doc....rdoc/index.html

ruby-doc.org is great for looking up issues like this, and you'll even find some sample code there occasionally (*ehem) that you can paste into a notepad and tweek before testing it out in irb.
Was This Post Helpful? 1
  • +
  • -

#4 globus25  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 19
  • Joined: 07-July 11

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 10:08 AM

View PostNotarySojac, on 07 July 2011 - 10:02 AM, said:

Why not at least suggest something useful like the ruby documentation for net::smtp??

http://www.ruby-doc....rdoc/index.html

ruby-doc.org is great for looking up issues like this, and you'll even find some sample code there occasionally (*ehem) that you can paste into a notepad and tweek before testing it out in irb.


I already checked that site and that error shouldn`t be occuring(that function is included in ruby).

ps. I never said that I made that script from first line it is a copy/paste... I just wanted to know how it works because there isn`t good debbuger where I can try each line so I took whole code and changed some stuff
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 10:47 AM

The method you're calling is an instance method, not a class method. You should be calling it on the instance you're creating when you call Net::SMTP.new (which means you should also be saving that instance in a variable, so you can do that).

Also:

Which ruby version are you using? The method in question did not exist until ruby 1.8.7/1.9, so if you're using 1.8.6, that's another problem.

This post has been edited by sepp2k: 07 July 2011 - 10:50 AM

Was This Post Helpful? 2
  • +
  • -

#6 globus25  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 19
  • Joined: 07-July 11

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 10:53 AM

I`m using ruby 1.9.2
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 11:06 AM

View Postglobus25, on 07 July 2011 - 07:53 PM, said:

I`m using ruby 1.9.2


So the only problem is that you're calling instance methods on the class.

Another thing that's weird (i.e. wrong) about your code is that you're first creating an SMTP object using new and then another using start. You probably just want to use start, get rid of the call to new and move the call to enable_starttls into the start-block.
Was This Post Helpful? 1
  • +
  • -

#8 globus25  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 19
  • Joined: 07-July 11

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 11:15 AM

But still it should work..

btw. I read about my error here but can`t fix it in displayed way
http://agilewebdevel...mtp_tls_support
Was This Post Helpful? 0
  • +
  • -

#9 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 11:16 AM

Following sepp2k's suggestions would look like this. (and creating the msg a bit differently as that the TO and FROM lines are redundant from the example).

require 'net/smtp'

YourDomain = 'smtp.gmail.com'
YourAccountName = 'REMOVED@gmail.com'
YourPassword = 'REMOVED'

FromAddress = 'REMOVED@gmail.com'
ToAddress = 'REMOVED@gmail.com'


@msg = "Subject: Hi There!\nThis is the body.  Tested on ruby 1.8.7"

smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start(YourDomain, YourAccountName, YourPassword, :login) do
  smtp.send_message(@msg, FromAddress, ToAddress)
end



It won't work with out making the changes regarding the class/ object confusion.

This post has been edited by NotarySojac: 07 July 2011 - 11:17 AM

Was This Post Helpful? 1
  • +
  • -

#10 globus25  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 19
  • Joined: 07-July 11

Re: Sending email - gmail(smtp) - error

Posted 07 July 2011 - 11:22 AM

View PostNotarySojac, on 07 July 2011 - 11:16 AM, said:

Following sepp2k's suggestions would look like this. (and creating the msg a bit differently as that the TO and FROM lines are redundant from the example).

require 'net/smtp'

YourDomain = 'smtp.gmail.com'
YourAccountName = 'REMOVED@gmail.com'
YourPassword = 'REMOVED'

FromAddress = 'REMOVED@gmail.com'
ToAddress = 'REMOVED@gmail.com'


@msg = "Subject: Hi There!\nThis is the body.  Tested on ruby 1.8.7"

smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start(YourDomain, YourAccountName, YourPassword, :login) do
  smtp.send_message(@msg, FromAddress, ToAddress)
end



It won't work with out making the changes regarding the class/ object confusion.



Thanks that worked now I know where I made mistakes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1