Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Hantar E-mel Dengan Selamat dengan PHP: Panduan Menggunakan SMTP untuk E-mel Bebas Spam

Hantar E-mel Dengan Selamat dengan PHP: Panduan Menggunakan SMTP untuk E-mel Bebas Spam

Barbara Streisand
Barbara Streisandasal
2024-09-28 06:08:01487semak imbas

Deliver Emails Safely with PHP: A Guide to Using SMTP for Spam-Free Emails

ここでは、PHP SMTP を使用してスパム フォルダーに分類されずに電子メールを送信する方法の段階的な例を示します。

PHPMailer ライブラリを使用します。これにより、SMTP 経由での電子メールの送信が簡素化され、到達性の向上に役立ちます。これらの手順に従って、電子メールがスパム フォルダーに入るのを避けるために SMTP を適切に構成する方法を学びます。

ステップ 1: PHPMailer をインストールする

まず、PHPMailer ライブラリをインストールする必要があります。これは Composer を使用して行うことができます。

composer require phpmailer/phpmailer

Composer をお持ちでない場合は、GitHub から PHPMailer を手動でダウンロードして、プロジェクトに含めることができます。

ステップ 2: PHP メール スクリプトを作成する

SMTP で PHPMailer を使用して電子メールを送信するためのスクリプトを記述する新しいファイル send_email.php を作成します。

<?php
// Load Composer's autoloader if using Composer
require 'vendor/autoload.php';

// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();                                          // Use SMTP
    $mail->Host = 'smtp.example.com';                         // Set the SMTP server (use your SMTP provider)
    $mail->SMTPAuth = true;                                   // Enable SMTP authentication
    $mail->Username = 'your_email@example.com';               // SMTP username
    $mail->Password = 'your_password';                        // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;       // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                        // TCP port to connect to (587 is common for TLS)

    //Recipients
    $mail->setFrom('your_email@example.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');  // Add recipient
    $mail->addReplyTo('reply_to@example.com', 'Reply Address');    // Add a reply-to address

    // Content
    $mail->isHTML(true);                                        // Set email format to HTML
    $mail->Subject = 'Test Email Subject';
    $mail->Body    = 'This is a <b>test email</b> sent using PHPMailer and SMTP.';
    $mail->AltBody = 'This is a plain-text version of the email for non-HTML email clients.';

    // Send the email
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

部品ごとの内訳:

  1. PHPMailer の初期化:

    • Composer 経由で必要なファイルをインクルードした後、PHPMailer クラスのインスタンスを作成します。
    • try-catch ブロックは、電子メールの送信中に発生する可能性のある例外を処理するために使用されます。
  2. SMTP サーバー構成:

    • $mail->isSMTP() は、PHPMailer に電子メールの送信に SMTP を使用するように指示します。
    • $mail->Host = 'smtp.example.com': 'smtp.example.com' を SMTP プロバイダーのホストに置き換えます (例: Gmail の SMTP は 'smtp.gmail.com')。
    • $mail->SMTPAuth = true: これにより、通常 SMTP サーバーで必要となる認証が有効になります。
    • $mail->ユーザー名と $mail->パスワード: ここに SMTP 資格情報を入力します。
    • $mail->SMTPSecure: 暗号化方式を設定します。セキュリティのために TLS (STARTTLS) を推奨します。
    • $mail->Port: 一般的な SMTP ポートは、TLS の場合は 587、SSL の場合は 465 です。 SMTP サーバーに必要なポートを使用します。
  3. 送信者と受信者の設定:

    • $mail->setFrom('your_email@example.com', 'Your Name'): 送信者の電子メールと名前を指定します。
    • $mail->addAddress('recipient@example.com', 'Recipient Name'): 受信者の電子メールと名前を追加します。
    • $mail->addReplyTo(): 返信先メール アドレスを設定します (オプション)。
  4. メールコンテンツの設定:

    • $mail->isHTML(true): 電子メールのコンテンツが HTML であることを指定します。
    • $mail->Subject: 電子メールの件名を設定します。
    • $mail->Body: これはメールの HTML コンテンツです。
    • $mail->AltBody: これはメール本文のプレーンテキスト バージョンで、HTML をサポートしていないメール クライアントに役立ちます。
  5. メールの送信:

    • $mail->send() は電子メールの送信を試みます。成功した場合は、成功メッセージが出力されます。それ以外の場合、エラーが捕捉されて表示されます。

ステップ 3: SMTP 構成とベストプラクティス

メールがスパム フォルダーに送られるのを避けるには、次のベスト プラクティスに従うことが重要です。

  1. 信頼できる SMTP プロバイダーを使用します:

    Gmail、SendGrid、Mailgun などの信頼できる SMTP プロバイダーを使用すると、スパムとしてフラグが立てられる可能性が低くなるため、配信率が向上します。

  2. ドメインを認証する:

    SPF (送信者ポリシー フレームワーク)、DKIM (DomainKeys Identified Mail)、および DMARC (ドメインベースのメッセージ認証、レポートおよび適合性) レコードをセットアップします。ドメインを使用してメールの正当性を確認します。

  3. スパムコンテンツを避ける:

    電子メールの内容がクリーンであり、スパムとしてフラグが設定されていないことを確認してください。すべて大文字、スパム的な単語 (「無料」、「勝者」など) の過度の使用、リンクの多すぎは避けてください。

  4. プレーンテキストの代替を使用する:

    メールのプレーンテキスト バージョン ($mail->AltBody) を必ず含めてください。一部の電子メール クライアントは、HTML のみの電子メールに不審なフラグを立てます。

  5. 送信者として無料の電子メール サービスを避ける:

    スパムとしてフラグ付けされるのを避けるために、Gmail、Yahoo などの無料サービスではなく、独自のドメインの専門的な電子メール アドレスを使用してください。

  6. メールごとの受信者数を制限する:

    一括メールを送信する場合は、スパムのフラ​​グが立てられるのを避けるために、1 つのメッセージで多数の受信者に送信するのではなく、適切な一括メール サービスを使用してください。

ステップ 4: スクリプトを実行する

send_email.php ファイルをサーバーにアップロードし、ブラウザまたはコマンドラインを通じて実行します。

php send_email.php

設定が正しい場合は、次のメッセージが表示されます:

Message has been sent

If there’s an error, it will display:

Message could not be sent. Mailer Error: {Error Message}

Conclusion

By using PHPMailer and a proper SMTP setup, you can ensure your emails are sent reliably and with a lower chance of landing in the spam folder. Here's a quick summary:

  1. Install PHPMailer to simplify email sending with SMTP.
  2. Configure SMTP correctly with authentication and encryption.
  3. Use proper domain verification (SPF, DKIM, DMARC) to increase deliverability.
  4. Write clean, non-spammy email content and always include a plain-text version.
  5. Run the script and monitor the success message or error log.

This approach ensures better deliverability and reduces the chances of your emails being marked as spam.

Feel free to follow me:

  • LinkedIn
  • GitHub

Atas ialah kandungan terperinci Hantar E-mel Dengan Selamat dengan PHP: Panduan Menggunakan SMTP untuk E-mel Bebas Spam. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:. Kalendar Saya IArtikel seterusnya:. Kalendar Saya I