You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法让Mustache文件读取sendmail.php及Ruby项目邮件功能适配问题

Hey there! Let's break down the two key issues you're hitting after removing the account module from your Ruby/Mustache project, and walk through fixes for each.

1. Fixing Mail Configuration Loading Failures

The original project likely tied mail settings to the account module (pulling API keys or SMTP credentials from user accounts). Since you've removed that, we need to decouple mail config from user accounts and load it directly:

  • Step 1: Create a standalone mail config file
    Make a new file at config/mail_settings.yml with your SMTP details (adjust for your environment):

    development:
      smtp_host: 'smtp.gmail.com'
      smtp_port: 587
      smtp_user: 'your-email@example.com'
      smtp_pass: 'your-app-specific-password'
      from_address: 'noreply@your-project.com'
    production:
      smtp_host: 'smtp.your-provider.com'
      smtp_port: 465
      smtp_user: 'prod-email@your-project.com'
      smtp_pass: 'prod-app-password'
      from_address: 'noreply@your-project.com'
    
  • Step 2: Write a Ruby loader for the config
    Create lib/mail_config.rb to load and initialize your mailer (I'll use Ruby's built-in mail gem here—swap with your library if needed):

    require 'yaml'
    require 'mail'
    
    module MailConfig
      def self.load(env = ENV['RACK_ENV'] || 'development')
        config_path = File.expand_path('../../config/mail_settings.yml', __dir__)
        YAML.load_file(config_path)[env]
      end
    
      def self.setup
        settings = load
        Mail.defaults do
          delivery_method :smtp, {
            address: settings['smtp_host'],
            port: settings['smtp_port'],
            user_name: settings['smtp_user'],
            password: settings['smtp_pass'],
            authentication: 'plain',
            enable_starttls_auto: true
          }
        end
      end
    end
    
  • Step 3: Initialize config on app startup
    Add this line to your app's entry point (like config.ru or your main Ruby file) to load config when the app starts:

    require_relative 'lib/mail_config'
    MailConfig.setup
    
2. Making Mustache Templates Access Data/Mail Functionality

Mustache is a logic-less template engine—you can't call external files or libraries directly in the template. All logic needs to be handled in your Ruby code, then passed to the template context:

  • Step 1: Build a Mustache context class
    Create a class for your homepage template that handles data and mail logic:

    require 'mustache'
    require_relative 'lib/mail_config'
    
    class HomeTemplate < Mustache
      self.path = 'views' # Path to your Mustache files
    
      def initialize
        @mail_settings = MailConfig.load
        @send_result = nil
      end
    
      # Expose config data to the template if needed
      def smtp_host
        @mail_settings['smtp_host']
      end
    
      # Handle email sending logic (called from your controller)
      def send_test_email(recipient_email)
        begin
          mail = Mail.new do
            from @mail_settings['from_address']
            to recipient_email
            subject 'Test Email from Your Project'
            body 'This is a test email sent without the account module!'
          end
          mail.deliver!
          @send_result = "Email sent successfully to #{recipient_email}!"
        rescue => e
          @send_result = "Failed to send email: #{e.message}"
        end
      end
    
      # Expose the result to the template
      def send_result
        @send_result
      end
    end
    
  • Step 2: Connect the template to your controller
    In your route handler (e.g., Sinatra route), handle the form submission and render the template:

    get '/' do
      @template = HomeTemplate.new
      erb @template.render
    end
    
    post '/' do
      @template = HomeTemplate.new
      @template.send_test_email(params[:recipient])
      erb @template.render
    end
    
  • Step 3: Update your Mustache template
    In views/home.mustache, use the context variables/methods you defined:

    <h1>Welcome to the Homepage</h1>
    
    <!-- Show SMTP config (optional) -->
    <p>Current SMTP Host: {{smtp_host}}</p>
    
    <!-- Email Send Form -->
    <form method="POST">
      <label for="recipient">Recipient Email:</label>
      <input type="email" id="recipient" name="recipient" required>
      <button type="submit">Send Test Email</button>
    </form>
    
    <!-- Show Send Result -->
    {{#send_result}}
    <div class="result">{{send_result}}</div>
    {{/send_result}}
    
Quick Troubleshooting Tips
  • Check config file paths: Use File.exists?('config/mail_settings.yml') in your Ruby code to confirm the file is being found.
  • Debug YAML syntax: A missing colon or indentation will break config loading—validate your YAML with a tool if needed.
  • Note on PHPMailer: If you're trying to use PHPMailer (a PHP library) in a Ruby project, you'll need to run it via a PHP script and call it from Ruby (e.g., with system('php send-email.php')) or switch to a Ruby mail library like mail or postmark.

内容的提问来源于stack exchange,提问作者Declan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:49:16