Προγραμματισμός

* Γνώση Υπολογιστών >> Προγραμματισμός >  >> Προγραμματισμός σε PHP / MySQL

Πώς να Στείλετε Email με PHP στο IIS 6

Εισαγωγή

Σημαντικό: Το IIS 6 (Windows Server 2003) είναι End-of-Life από Ιούλιο 2015. Αυτό το άρθρο είναι για legacy συστήματα. Για σύγχρονα συστήματα (IIS 10+, Windows Server 2016/2019/2022), χρησιμοποιήστε SMTP service ή εξωτερικούς providers (SendGrid, Mailgun, AWS SES).

Ρύθμιση PHP (php.ini)

; php.ini
[mail function]
; Για IIS SMTP service στον ίδιο server
SMTP = localhost
smtp_port = 25

; Αν χρησιμοποιείτε εξωτερικό SMTP (π.χ. Gmail, SendGrid)
; SMTP = smtp.sendgrid.net
; smtp_port = 587
; sendmail_from = noreply@yourdomain.com

; Για authentication (απαιτεί PHP 5.6+ με sendmail_path ή PEAR Mail)
;.auth_username = your_username
;.auth_password = your_password

Ρύθμιση IIS 6 SMTP Service

1. Εγκατάσταση SMTP Service

  1. Control Panel → Add/Remove Programs → Add/Remove Windows Components
  2. Επιλέξτε IIS → Details → SMTP Service
  3. Complete installation

2. Ρύθμιση Relay Restrictions

  1. IIS Manager → Default SMTP Virtual Server → Properties
  2. Tab Access → Relay button
  3. Only the list below → Add:
    • 127.0.0.1 (localhost)
    • IP του web server αν διαφέρει
  4. Tab Access → Authentication → Anonymous access (για local)
  5. Tab Delivery → Outbound Security → Anonymous (για local)
  6. Tab Delivery → Advanced → Smart host (αν χρειάζεται): smtp.yourisp.com

3. Firewall / Antivirus

  • Ανοίξτε port 25 (ή 587/465) outbound
  • Αντιβιοτικά λογισμικό μπορεί να μπλοκάρει SMTP - προσθέστε exception

PHP Code για Αποστολή Email

Βασική mail() Funktion

<?php
$to = 'recipient@example.com';
$subject = 'Test Email from PHP on IIS';
$message = "Hello,\n\nThis is a test email sent from PHP on IIS 6.\n\nRegards,\nSystem";
$headers = "From: sender@yourdomain.com\r\n";
$headers .= "Reply-To: reply@yourdomain.com\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully";
} else {
    echo "Email sending failed";
}
?>

HTML Email με Attachments (Χωρίς εξωτερικές βιβλιοθήκες - πολύπλοκο)

Συστηνόμεται να χρησιμοποιήσετε PHPMailer ή Symfony Mailer:

<?php
// Composer: require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->Port = 25;
    $mail->SMTPAuth = false; // true αν χρειάζεται auth
    
    // Recipients
    $mail->setFrom('sender@yourdomain.com', 'System Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');
    $mail->addReplyTo('reply@yourdomain.com', 'Reply Name');
    
    // Content
    $mail->isHTML(true);
    $mail->Subject = 'HTML Email from PHP on IIS';
    $mail->Body    = '

Hello!

This is HTML email.

'; $mail->AltBody = 'Hello! This is plain text version.'; // Attachments $mail->addAttachment('/path/to/file.pdf', 'document.pdf'); $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } ?>

Troubleshooting

Σφάλμα Αιτία Λύση
mail() επιστρέφει false SMTP service not running / relay denied Ελέγξτε IIS SMTP service, relay settings, logs
Email καθίσταται σε spam Missing SPF/DKIM/DMARC, poor reputation Ρυθμίστε DNS records, χρησιμοποιήστε reputable SMTP
Timeout Firewall blocking port 25 Ανοίξτε firewall, ελέγξτε antivirus
550 Relay not permitted IP not in relay list Προσθέστε 127.0.0.1 και server IP στο relay

Logs και Debugging

  • IIS SMTP Logs: C:\WINDOWS\system32\LogFiles\SMTPSVC1\
  • PHP mail log: Ρυθμίστε mail.log = "C:\logs\php_mail.log" στο php.ini
  • Test με telnet: telnet localhost 25 → HELO localhost → MAIL FROM:...

Σύγχρονη Εναλλακτική: SMTP API Providers

Αποφύγετε τον τοπικό SMTP server. Χρησιμοποιήστε:

  • SendGrid - API / SMTP, generous free tier
  • Mailgun - API-based, excellent deliverability
  • AWS SES - Low cost, high volume
  • Postmark - Transactional email specialist

Όλοι παρέχουν PHP SDKs και διαχειρίζονται deliverability, bounces, reputation.

Σχετικά άρθρα

Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.