无法让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.
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 atconfig/mail_settings.ymlwith 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
Createlib/mail_config.rbto load and initialize your mailer (I'll use Ruby's built-inmailgem 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 endStep 3: Initialize config on app startup
Add this line to your app's entry point (likeconfig.ruor your main Ruby file) to load config when the app starts:require_relative 'lib/mail_config' MailConfig.setup
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 endStep 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 endStep 3: Update your Mustache template
Inviews/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}}
- 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 likemailorpostmark.
内容的提问来源于stack exchange,提问作者Declan

