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

基于Rails实现带转发功能的用户表单邮件发送方案问询

Solution for Form Submission Email & Reply Forwarding

Let’s break this down into two core tasks: generating the formatted email from your form data, and setting up reply forwarding to the user’s personal email.

1. Generate & Send the Target Email from Form Data

Assuming you’re using a Node.js backend (a common choice for form handling), here’s how you can use Nodemailer to construct and send the email exactly as you need it:

First, install Nodemailer:

npm install nodemailer

Then, write the code to handle form submission (example using Express.js):

const nodemailer = require('nodemailer');
const express = require('express');
const app = express();
app.use(express.json());

// Configure your email transporter (use your service like SendGrid, Gmail, or Mailgun)
const transporter = nodemailer.createTransport({
  service: 'SendGrid',
  auth: {
    user: 'your_sendgrid_username',
    pass: 'your_sendgrid_api_key'
  }
});

// Handler for form submission
app.post('/submit-form', async (req, res) => {
  const { user_name, info, to } = req.body;
  // Retrieve user's personal email from your database (linked to user_name/user_id)
  const userPersonalEmail = `${user_name}@gmail.com`; 

  // Construct the email content in your required format
  const emailContent = `from ${user_name} <${user_name}@appdomain.com> to: ${to} content: "${info}"`;
  
  const mailOptions = {
    from: `${user_name} <${user_name}@appdomain.com>`,
    to: to,
    subject: 'New Form Submission',
    text: emailContent,
    // Optional: Direct replies to personal email (skip if using domain forwarding)
    replyTo: userPersonalEmail
  };

  try {
    await transporter.sendMail(mailOptions);
    res.status(200).send('Email sent successfully');
  } catch (error) {
    console.error('Email send failed:', error);
    res.status(500).send('Failed to process your submission');
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

Key notes:

  • Replace the transporter config with your actual email service credentials.
  • You’ll need to store the user’s personal email (like johnsmith@gmail.com) in your database, linked to their user_id/user_name for easy retrieval.

2. Set Up Reply Forwarding to User's Personal Email

You have two reliable options here:

Option A: Use the Reply-To Header (Simplest)

Add the replyTo field in mailOptions (as shown in the code above) pointing to the user’s personal email. When recipients click "reply", their email client will automatically address the message to the personal email instead of johnsmith@appdomain.com. This avoids needing domain-level setup.

Option B: Domain-Level Forwarding (Formal Approach)

If you want all emails sent to johnsmith@appdomain.com (not just replies to form emails) to forward to their personal email, set up a rule with your domain provider or email service:

  • Domain registrars (GoDaddy/Namecheap): Create an email alias for johnsmith@appdomain.com and set its forward destination to johnsmith@gmail.com.
  • Email services (SendGrid/Mailgun): Use their forwarding features. For example, in SendGrid, create a rule that matches emails to *@appdomain.com and forwards them to the corresponding personal email (using your username-to-email mapping).
  • Self-hosted servers: Configure your MTA (like Postfix) with virtual aliases. Add a line in /etc/postfix/virtual like johnsmith@appdomain.com johnsmith@gmail.com, then run postmap /etc/postfix/virtual and reload Postfix.

Whichever option you choose, ensure you’re storing user personal emails securely and associating them correctly with their account details.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:36:29