Lua SMTP邮件发送无报错但未收到邮件问题排查求助
Looking at your code, there are a few key reasons why you’re not getting any errors or emails—let’s break them down and fix this step by step:
1. You’re Not Actually Sending the Email
The biggest issue is that you’ve set up all the email parameters, but you never call the smtp.send() function to trigger the send operation. Without this call, the code just defines variables and exits quietly, which explains why you see no errors and no email.
2. Invalid Email Address Formatting
You’re using HTML entities (< and >) around your email addresses instead of actual angle brackets (< and >). The SMTP library expects raw addresses wrapped in angle brackets, not escaped HTML characters.
3. Missing SMTP Server Configuration
Your code doesn’t specify critical SMTP server details like host, port, and authentication credentials. Most email providers require authentication and use specific ports (e.g., 587 for TLS, 465 for SSL). Without these, the library can’t connect to a valid server to deliver your email.
Corrected Example Code
Here’s a complete, working version of your code with all fixes applied. Update the SMTP server details to match your email provider’s requirements:
-- Load required libraries local smtp = require("socket.smtp") local ltn12 = require("ltn12") local mime = require("mime") -- Configure email addresses (use actual angle brackets) local from = "<sender@dataday.com>" local rcpt = { "<aqs@dataday.com>" } -- Define the email content local mesgt = { headers = { to = "aqs@dataday.com", -- Plain address here, no angle brackets needed subject = "Sending attachment over e-mail test", ["content-type"] = "text/plain; charset=utf-8" -- Ensure proper content interpretation }, body = { -- Replace with your actual message content [1] = { body = "Hello, this is a test email sent from Lua!" } } } -- SMTP server configuration (update these for your provider) local smtp_settings = { server = "smtp.dataday.com", -- Your SMTP hostname port = 587, -- TLS port; use 465 for SSL if required user = "sender@dataday.com", -- Your email username (usually full address) password = "your_app_or_account_password", -- Use app-specific password if needed starttls = true -- Enable TLS encryption (most providers require this) } -- Send the email and handle errors local success, err = smtp.send({ from = from, rcpt = rcpt, source = smtp.message(mesgt), server = smtp_settings.server, port = smtp_settings.port, user = smtp_settings.user, password = smtp_settings.password, starttls = smtp_settings.starttls }) -- Check results if success then print("Email sent successfully!") else print("Failed to send email: " .. err) end
Key Fixes Explained:
- Added the
smtp.send()call with error handling—now you’ll get clear feedback if something goes wrong - Fixed email address formatting by replacing HTML entities with actual angle brackets
- Added SMTP server configuration (host, port, auth) to connect to your email provider
- Added a
content-typeheader to ensure the email is displayed correctly - Replaced the placeholder body with meaningful test content
Additional Tips:
- If your provider uses app-specific passwords (like Gmail or Outlook), use that instead of your regular account password
- Check your spam folder—test emails often end up there even when sent successfully
- Verify your SMTP server allows connections from your current IP address (some providers block unrecognized locations)
内容的提问来源于stack exchange,提问作者user3310052

