如何在Raspbian Jessie服务器上用Python构建SMTP服务器发送指定域名邮件?
Hey there! Let's break this down clearly since you're new to SMTP—you're probably mixing up smtpd and smtplib right now, which is super common when starting out.
First, let's clarify the two modules:
smtplibis what you need for sending emails as a client: it connects to an existing SMTP server (either your domain's own or a third-party service) to deliver messages to real recipients.smtpdis for setting up a local test-only SMTP server: it can receive emails locally but won't send them to external addresses unless you configure it as a full Mail Transfer Agent (MTA)—that's way more complex than what you need right now.
1. Sending Real Emails from myname@mydomain.name
To send actual emails with your custom domain, you first need a working SMTP server configured for mydomain.name. This could be:
- A third-party SMTP service provided by your domain registrar or email host
- A local MTA (like Postfix) installed and set up on your Raspbian Jessie server
Here's a Python script using smtplib that works for both scenarios:
import smtplib from email.mime.text import MIMEText # Email details sender_email = "myname@mydomain.name" receiver_email = "recipient@example.com" subject = "Test Email from My Raspbian Server" body = "Hello! This is a test email sent using Python with my custom domain." # Build the email message msg = MIMEText(body) msg["From"] = sender_email msg["To"] = receiver_email msg["Subject"] = subject try: # Option 1: Use local Postfix (no auth needed if configured for local users) with smtplib.SMTP("localhost", 25) as server: server.send_message(msg) # Option 2: Use a third-party SMTP server (requires authentication) # with smtplib.SMTP_SSL("smtp.mydomain.name", 465) as server: # server.login("myname@mydomain.name", "your_email_password") # server.send_message(msg) print("Email sent successfully!") except Exception as e: print(f"Error sending email: {str(e)}")
Fixes for Common Errors You Might Face:
- Connection refused: If using local Postfix, start it first with
sudo systemctl start postfixon Raspbian. For remote servers, confirm port 25 (or 465/587 for SSL/TLS) isn't blocked by a firewall. - Authentication failed: Double-check your username and password. For custom domain services, use the credentials provided by your email host (avoid plain text passwords in production—use environment variables instead).
- Relay access denied: This means your SMTP server isn't allowed to send emails on behalf of
myname@mydomain.name. For local Postfix, configure it to allow relaying from your server's IP or set up proper domain mapping.
2. Testing with smtpd (Local-Only)
If you just want to test your code without sending real emails, use smtpd to run a local test server:
- Open a terminal and start the test server:
python -m smtpd -n -c DebuggingServer localhost:1025
- Use this script to send a test email to the local server:
import smtplib from email.mime.text import MIMEText sender_email = "myname@mydomain.name" receiver_email = "test@localhost" subject = "Local Test Email" body = "This email will only appear in the smtpd terminal output." msg = MIMEText(body) msg["From"] = sender_email msg["To"] = receiver_email msg["Subject"] = subject try: with smtplib.SMTP("localhost", 1025) as server: server.send_message(msg) print("Test email sent! Check the smtpd terminal for the full message content.") except Exception as e: print(f"Error: {str(e)}")
This won't deliver emails to external addresses—it just prints the email content in the terminal running smtpd, perfect for testing without spamming anyone.
Raspbian Jessie Specific Notes:
- Jessie comes with Python 2.7 and 3.4 pre-installed. If using Python 3, run your script with
python3instead ofpython. - If you need to install missing packages (though
smtplibis usually included), runsudo apt-get update && sudo apt-get install python3-smtplib.
内容的提问来源于stack exchange,提问作者Jay Robson
相关产品推荐
相关产品推荐

