我正在尝试使用Ruby通过允许用户输入电子邮件地址来向用户发送电子邮件。用户将通过Tkinter制作的文本框输入电子邮件地址,然后程序将从中获取值并将其传递给发送电子邮件的函数。但是,将文本框中的值传递给它时,电子邮件功能不起作用。该功能似乎仅在我预先指定了应该发送给它的电子邮件时才起作用。
例如,如果我要使用send_email(final_location,'specifiedaddress @ gmail.com')调用该函数,则该函数将起作用。但是,如果我将其替换为包含字符串形式电子邮件的变量,例如send_email(final_location,user_email),则该程序将引发错误。似乎SMTP send_message函数仅在电子邮件用单撇号引起来时才起作用。
require 'net/smtp'
require 'tk'
result_win = TkToplevel.new
textbox = TkText.new(result_win)
textbox.place('height' => 20, 'width' => 200, 'x' => 350, 'y' => 250)
#this is the text widget of my program
send_email_button = TkButton.new(result_win)
send_email_button.text = "Send Email"
send_email_button.borderwidth = 5
send_email_button.state = "normal"
send_email_button.font = TkFont.new('times 20 bold')
send_email_button.foreground= "red"
send_email_button.activebackground = "blue"
send_email_button.relief = "groove"
send_email_button.command = (proc{user_email = textbox.get("1.0",'end');send_email(final_location,user_email)})
send_email_button.place('height' => 50, 'width' => 150, 'x' => 100, 'y' => 350)
def send_email(最终位置,用户电子邮件)
message = <<EOF
From: LUCNHINATOR! <FROM@gmail.com>
To: RECEIVER <TO@gmail.com>
Subject: Your Lunch Location!
The following are information regarding your lunch location
EOF
smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start('gmail.com', 'senderemail@gmail.com', '<password>', :login)
smtp.send_message message, 'senderemail@gmail.com', user_email
smtp.finish
#Using Block
smtp = Net::SMTP.new('smtp.gmail.com', 587 )
smtp.enable_starttls
smtp.start('gmail.com', 'senderemail@gmail.com', '<password>', :login) do |smtp|
smtp.send_message message,'senderemail@gmail.com', user_email
结束 结束
此问题是否有任何变通办法,以允许该功能接受用户输入的电子邮件?