2016-04-04 14 views
7

मुझे रेलगाड़ी अनुप्रयोग पर रुबी में सेंड्रिड वेब एपीआई को एकीकृत करने के तरीके पर चरण-दर-चरण ट्यूटोरियल नहीं मिल रहा है। मैं इसके लिए बहुत नया हूं इसलिए शायद मुझे कुछ याद आ रही है।रेल पर रूबी के लिए सेंड्रिड एपीआई

मैं smtp वितरण विधि के बजाय सेंड्रिड वेब एपीआई का उपयोग करना चाहता हूं (एसएमटीपी विधि पर वेब एपीआई के लाभों के बारे में मेलगुन वार्ता: https://documentation.mailgun.com/quickstart-sending.html, और मैं सोच रहा था कि सेंड्रिड के पास या तो समान लाभ होंगे या मैं संभावित रूप से बाद में मेलगुन पर स्विच करूँगा)।

sendgrid मणि (https://github.com/sendgrid/sendgrid-ruby) स्थापित करने के बाद, प्रलेखन मुझसे कहता है "अपने SendGrid एपीआई कुंजी के साथ एक नए ग्राहक बनाएँ" करने के लिए, और मैं इसे 2 तरीके से कर सकते हैं कि:

require 'sendgrid-ruby' 

# As a hash 
client = SendGrid::Client.new(api_key: 'YOUR_SENDGRID_APIKEY') 

# Or as a block 
client = SendGrid::Client.new do |c| 
    c.api_key = 'YOUR_SENDGRID_APIKEY' 
end 

विशेष रूप से कहां में मेरी क्या मैं इस कोड को रखना चाहता हूं? क्या मुझे इसे अपने मेलर, मेरे एप्लिकेशन मेलर या कॉन्फ़िगर/वातावरण/production.rb फ़ाइल में रखना चाहिए? https://launchschool.com/blog/handling-emails-in-rails

इस ट्यूटोरियल यह लाइन client = SendGrid::Client.new(api_key: 'YOUR_SENDGRID_APIKEY') वास्तव में मेलर विधि ही में जाना चाहिए की तरह दिखता है के अनुसार:

मैं इस ट्यूटोरियल है कि कैसे Mailgun एपीआई स्थापित करने के लिए के माध्यम से चलता है पर एक नज़र ले लिया। (संभवतः sendgrid जानकारी के साथ mailgun विशिष्ट जानकारी की जगह) launchschool.com उदाहरण के लिए नीचे देखें:

class ExampleMailer < ActionMailer::Base 

     def sample_email(user) 
    @user = user 
    mg_client = Mailgun::Client.new ENV['api_key'] 
    message_params = {:from => ENV['gmail_username'], 
         :to  => @user.email, 
         :subject => 'Sample Mail using Mailgun API', 
         :text => 'This mail is sent using Mailgun API via mailgun-ruby'} 
    mg_client.send_message ENV['domain'], message_params 
    end 
end 

साथ ही, मैं कैसे मिलता है मेरी मेलर विधि एक मेलर देखने के बजाय साधारण पाठ के रूप में में उल्लिखित भेजने के लिए लॉन्चस्कूल उदाहरण? उदाहरण के लिए, 'यह मेल भेजकर भेजा गया है ...' भेजने के बजाय मैं एक मेलर व्यू भेजना चाहता हूं (account_activation.html.erb जैसे कुछ)।

अंत में, मैं अपने आवेदन में डेविस का उपयोग कर रहा हूं, और मैं ईमेल भेजने के लिए वेब एपीआई का उपयोग करना चाहता हूं (यानी पासवर्ड रीसेट इत्यादि)। क्या इसका मतलब है कि मुझे डेविस के लिए कस्टम मेलर बनाना होगा? अगर ऐसा है, तो मैं वह कैसे करू?

डेविस (https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer) के अनुसार, मुझे "एक वर्ग बनाना जो डेविस :: मेलर" को विस्तारित करना चाहिए। क्या इसका मतलब है कि मैं बस अपने मेलर फ़ोल्डर में दस्तावेज़ों में रखी गई जानकारी के साथ एक फाइल बना सकता हूं? क्या मुझे डेविस के लिए एक अलग मेलर चाहिए या क्या मेरे पास डेविस मेलर से मौजूदा मेलर विरासत हो सकता है? अंत में, मैं ईमेल भेजने के लिए प्रेषक वेब एपीआई का उपयोग करने के लिए तैयार कैसे करूं (सरल smtp विधि के बजाय)?

लंबे प्रश्न के लिए खेद है, लेकिन उम्मीद है कि दूसरों को यह उपयोगी लगेगा।

धन्यवाद!

+2

ने यह कोशिश नहीं की है लेकिन आशाजनक प्रतीत होता है: https://github.com/eddiezane/sendgrid-actionmailer –

उत्तर

-3

यहां आपके RoR के ऐप में SendGrid को एकीकृत करने में सहायता के लिए एक चरण-दर-चरण मार्गदर्शिका है। आशा करता हूँ की ये काम करेगा!

  • एक SendGrid खाता बनाने के पहली बार में: http://sendgrid.com/
  • एक नई रेल फ़ोल्डर बनाएँ (sendgrid_confirmation)

    • रेल नया sendgrid_confirmation
  • नेविगेट करने के लिए 'sendgrid_confirmation' फ़ोल्डर

    • सीडी sendgrid_confirmation
  • अपने पाठ संपादक में खोलें 'sendgrid_confirmation' (उदात्त)

  • एक उपयोगकर्ता मॉडल बनाएं (उपयोगकर्ता एक परीक्षण मॉडल है, तो आप अपनी परियोजना के उपयोग के अनुसार किसी भी अन्य मॉडल बना सकते हैं)

    • रेल पाड़ उपयोगकर्ता नाम ईमेल लॉगिन
    • रेक db उत्पन्न: विस्थापित
  • अपने Gemfile

    • मणि में sendgrid-रेल मणि शामिल करें 'sendgrid-रेल', '~> 2.0'
  • भागो बंडल अपने टर्मिनल में स्थापित

    • बंडल इंस्टॉल
  • SendGrid API क्रेडेंशियल्स को परिभाषित करने के लिए secrets.yml का उपयोग करें: (config/secrets.yml) उत्पादन: sendgrid_username: your-sendgrid-username sendgrid_password: आपका-प्रेषण-पासवर्ड (ईमेल विकास और परीक्षण वातावरण में नहीं भेजे जाते हैं। तो आप इसे केवल उत्पादन के लिए परिभाषित कर सकते हैं।)

  • मेलर क्लास जेनरेट करें। ईमेल दृश्यों के लिए मेलर कक्षाएं हमारे नियंत्रकों के रूप में कार्य करती हैं।

    • रेल उत्पन्न मेलर UserNotifier
  • एप्लिकेशन खोलें/मेलर्स/user_notifier.rb और निम्नलिखित मेलर कार्रवाई उपयोगकर्ताओं को साइन-अप मेल भेजता है जोड़ने

class UserNotifier < ActionMailer::Base 
    default :from => '[email protected]' 
    # send a signup email to the user, pass in the user object that contains the user's email address 
    def send_signup_email(user) 
    @user = user 
    mail(:to => @user.email, 
     :subject => 'Thanks for signing up for our amazing app') 
    end 
end 
  • फ़ाइल ऐप/व्यू/User_notifier/send_signup_email.html बनाएं।ERB इस प्रकार है:

<!DOCTYPE html> 
    <html> 
     <head> 
       <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
     </head> 
     <body> 
       <h1>Thanks for signing up, <%= @user.name %>!</h1> 
       <p>Thanks for joining and have a great day! Now sign in and do awesome things!</p> 
     </body> 
    </html> 
  • उपयोगकर्ता नियंत्रक पर जाएं (एप्लिकेशन/नियंत्रक/users_controller.rb (यह एक विचार है कि हमारे कार्रवाई से मेल खाती है और हमारे ईमेल के लिए HTML आउटपुट पैदा करेगा)) और जब उपयोगकर्ता को सहेजा जाता है तो UserNotifier.send_signup_email पर एक कॉल जोड़ें।

def create 
    @user = User.new(user_params) 
    respond_to do |format| 
    if @user.save 
     UserNotifier.send_signup_email(@user).deliver 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render :show, status: :created, location: @user } 
    else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
    end 
end 
  • अद्यतन अपने config/environment.rb SendGrid के सर्वर को अपने ActionMailer सेटिंग्स इंगित करने के लिए। आपका environment.rb फ़ाइल ऐसा दिखाई देगा:

# Load the Rails application. 
require File.expand_path('../application', __FILE__) 

# Initialize the Rails application. 
Rails.application.initialize! 

ActionMailer::Base.smtp_settings = { 
    :user_name => ‘your_sendgrid_username’, 
    :password => 'your_sendgrid_password’, 
    :domain => ‘your_domain.com', 
    :address => 'smtp.sendgrid.net', 
    :port => 587, 
    :authentication => :plain, 
    :enable_starttls_auto => true 
} 
  • प्वाइंट अपने config/routes.rb लोड पर सूचकांक पृष्ठ लोड करने फ़ाइल। अपने routes.rb फ़ाइल में निम्नलिखित जोड़ें:

get ‘/’ => ‘users#index’ 

और वह यह है! अब जब आप एक नया उपयोगकर्ता बनाते हैं, तो आपको [email protected] से एक ईमेल प्राप्त करना चाहिए (उपयोगकर्ता द्वारा प्रदान किया गया ईमेल)। आप इसे बदल सकते हैं: app/mailers/user_notifier.rb से ई-मेल। डिफ़ॉल्ट बदलें: पते से और इसे करना चाहिए। आप उसी फ़ाइल के अंदर send_signup_mail विधि में ईमेल पैरामीटर जोड़ सकते हैं और वह विवरण जोड़ सकते हैं जिसे आप जोड़ना चाहते हैं। आप जो भी सामग्री चाहते हैं उसे प्रदर्शित करने के लिए आप app/views/user_notifier/send_signup_email.html.erb से संदेश निकाय को भी बदल सकते हैं।

+3

हालांकि यह उत्तर सही ढंग से बताता है (प्रेषक दस्तावेज़ों की तरह) SMTP का उपयोग करके अपने RoR ऐप से ई-मेल कैसे भेजें, यह करता है पूछे गए सवाल का जवाब न दें। सवाल एसएमटीपी के बजाय प्रेषक एपीआई का उपयोग करने के निर्देशों के लिए स्पष्ट रूप से पूछता है। –

+4

निराशा! यह कोई जवाब नहीं है जो अप्रचलित तरीके से कट और पेस्ट कर रहा है जो आसानी से कई अन्य स्थानों को पा सकता है। –

-1

संपादित करें:

इस विधि, दुर्भाग्य से, केवल दूरस्थ छवियों के साथ काम करेंगे।


ठीक है, तो यहां मैंने यह कैसे किया है। ऐसा करने का एक बेहतर तरीका हो सकता है, लेकिन यह मेरे लिए काम करता है।

तो एक ईमेल भेजने के साथ आप इसे मूल रूप से UserMailer.reset_email(user).deliver जैसे कुछ करके भेज देंगे। तो एक चर को देने और इसे बचाने के लिए निकालें:

object = UserMailer.reset_email(user)

गहराई से इस ish में नेस्ट ईमेल के मुख्य भाग है। जिस स्थान पर यह झूठ बोलता है वह संदर्भ के साथ बदल सकता है, इसलिए मेरी सलाह है कि ऑब्जेक्ट को फ्रंटेंड पर वापस कर दें ताकि आप इसमें खोद सकें और इसे ढूंढ सकें। मेरे लिए, शरीर यहां रहता है:

object = UserMailer.reset_email(user) 
body = object.body.parts.last.body.raw_source 

ठीक है तो अब आपको कच्चा स्रोत मिला है।अब इसे भेजने के लिए, यहाँ एक विधि मैं बनाया है:

def self.sendEmail(from,to,subject,message) 
    from = Email.new(email: from) 
    to = Email.new(email: to) 
    content = Content.new(type: 'text/html', value: message) 
    mail = Mail.new(from, subject, to, content) 

    sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) 
    response = sg.client.mail._('send').post(request_body: mail.to_json) 
    puts response.status_code 
    puts response.body 
    puts response.headers 
    end 

यह के रूप में कॉल इस तरह के (मेरे मामले में यह उपयोगकर्ता मॉडल में है):

User.sendEmail('[email protected]',user.email,'Reset Password', body)

से सामग्री प्रकार बदलने के लिए सुनिश्चित करें एचटीएमएल के लिए सादा, या आप सिर्फ कच्चे कोड मिलेगा। उम्मीद है की यह मदद करेगा।

1

मैं इस

class SendgridWebMailer < ActionMailer::Base 
    include Sendgrid 

    def initialize 
    @client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client 
    end 

    def send_some_email(record, token) 
    mail = Mail.new 
    // do your mail setup here 
    mail = Mail.new 
    mail.from = Email.new(email: YOUR_EMAIL_HERE) 
    mail.subject = YOUR_SUBJECT 

    // I personally use sendgrid templates, but if you would like to use html - 
    content = Content.new(
     type: 'text/html', 
     value: ApplicationController.render(
     template: PATH_TO_TEMPLATE, 
     layout: nil, 
     assigns: IF_NEEDED || {} 
    ) 
    mail.contents = content 

    personalization = Personalization.new 
    personalization.to = Email.new(email: EMAIL, name: NAME) 
    personalization.subject = SUBJECT 

    mail.personalizations = personalization 
    @client.mail._('send').post(request_body: mail.to_json) 
    end 
end 

करने के लिए एक मेलर वर्ग पैदा करेगा config/devise.rb

# Configure the class responsible to send e-mails. 
    config.mailer = 'MyDeviseMailer' 
में

SendgridWebMailer.send_some_email(record, token).deliver_later 

का उपयोग कर इसे कॉल वसीयत

लिए
class MyDeviseMailer < Devise::Mailer 
    helper :application # gives access to all helpers defined within `application_helper`. 
    include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` 

    def reset_password_instructions(record, token, opts={}) 

     SendgridWebMailer.send_some_email(record, token).deliver_later 
    end 
end 

+1

'SendgridWebMailer' को 'ActionMailer :: Base' से प्राप्त करने की आवश्यकता क्यों है? – kittyminky

संबंधित मुद्दे