SMTP सर्वर के माध्यम से ईमेल भेजना – सरल और उन्नत समाधान

SMTP-आधारित ईमेल भेजने को कई तरीकों से लागू किया जा सकता है, चाहे वह मूलभूत हो या उन्नत दृष्टिकोण। नीचे, हम सरल और जटिल दोनों विकल्प प्रस्तुत करते हैं, साथ ही एक PHP कोड का उदाहरण।

    सरल समाधान: होस्टिंग और डोमेन किराए पर लें

    SMTP-आधारित ईमेल भेजने का सबसे सरल तरीका है होस्टिंग और डोमेन किराए पर लेना।
    अधिकांश होस्टिंग सेवाएं PHP निष्पादन का समर्थन करती हैं और पहले से कॉन्फ़िगर किए गए SMTP के साथ आती हैं, जिससे आप PHP के mail() फ़ंक्शन का उपयोग करके सीधे ईमेल भेज सकते हैं, जैसा कि नीचे दिखाया गया है:

    <?php

    function sendEmail($emailAddresses, $subjects, $sentences) {
    foreach ($emailAddresses as $email) {
    // Select a random subject
    $subject = $subjects[array_rand($subjects)];

    // Generate the email body
    $message = '';
    foreach ($sentences as $sentenceOptions) {
    $message .= $sentenceOptions[array_rand($sentenceOptions)] . " ";
    }

    // Send the email
    $headers = "From: no-reply@domain.com\r\n";
    if (mail($email, $subject, $message, $headers)) {
    echo "Email successfully sent to: $email\n";
    } else {
    echo "Failed to send email to: $email\n";
    }
    }
    }

    // Define email addresses
    $emailAddresses = [
    "example1@domain.com",
    "example2@domain.com",
    "example3@domain.com",
    ];

    // Define possible subjects
    $subjects = [
    "Important Notification",
    "New Offer Available",
    "Discover Our Latest Services",
    ];

    // Define possible sentences (with variations)
    $sentences = [
    ["Welcome!", "Hello!", "Dear customer,"],
    ["We are excited to share our latest promotion.", "Our newest updates might interest you.", "Don't miss out on our latest opportunities!"],
    ["Please visit our website for more information.", "Details can be found on our website.", "Find additional information on our homepage."],
    ];

    // Send emails
    sendEmail($emailAddresses, $subjects, $sentences);

    ?>

    महत्वपूर्ण टिप: स्पैम से बचने के लिए रैंडमाइज़्ड कंटेंट
    हमारे स्पैम फिल्टर से बचने पर आधारित लेख में, हमने पहले ही इस बात पर जोर दिया था कि ईमेल सामग्री को रैंडमाइज़ करना उपयोगी हो सकता है। ऊपर दिया गया PHP कोड भी ऐसा ही करता है, विषय और वाक्यों को रैंडम तरीके से चुनकर।

      उन्नत समाधान: लोकल वेब सर्वर और डेटाबेस का उपयोग करें
      यदि आपको एक अधिक जटिल समाधान की आवश्यकता है, तो लोकल वेब सर्वर (जैसे Apache या Nginx) को चलाना भी संभव है। निम्नलिखित कोड MySQL डेटाबेस से प्राप्तकर्ताओं को खींचता है और रैंडमाइज़्ड सामग्री के साथ ईमेल भेजता है।

      <?php
      //exit;

      set_time_limit(999999999);
      ini_set("max_execution_time", 30000); //sec

      ini_set("display_errors", 1);
      ini_set("display_startup_errors", 1);
      error_reporting(E_ALL);
      header("Content-Type: text/html; charset=utf-8");
      header("Content-Type: text/html; charset=UTF-8");

      mb_internal_encoding("UTF-8");
      mb_http_output("UTF-8");
      mb_http_input("UTF-8");
      mb_regex_encoding("UTF-8");

      //---------sanitize incoming parameters start

      if (!empty($_POST)) {
      foreach ($_POST as $post => $value) {
      $_POST[$post] = addslashes($_POST[$post]);
      }
      }
      if (!empty($_PUT)) {
      foreach ($_PUT as $put => $value) {
      $_PUT[$put] = addslashes($_PUT[$put]);
      }
      }
      if (!empty($_GET)) {
      foreach ($_GET as $get => $value) {
      $_GET[$get] = addslashes($_GET[$get]);
      }
      }
      if (!empty($_REQUEST)) {
      foreach ($_REQUEST as $key => $value) {
      $_REQUEST[$key] = addslashes($_REQUEST[$key]);
      }
      }
      //---------sanitize incoming parameters end

      require_once dirname(__FILE__) . "/phpmailer-6.1.1/Exception.php";
      require_once dirname(__FILE__) . "/phpmailer-6.1.1/OAuth.php";
      require_once dirname(__FILE__) . "/phpmailer-6.1.1/PHPMailer.php";
      require_once dirname(__FILE__) . "/phpmailer-6.1.1/POP3.php";
      require_once dirname(__FILE__) . "/phpmailer-6.1.1/SMTP.php";

      /*CONFIG*/
      /*CONFIG*/
      /*CONFIG*/
      /*CONFIG*/

      echo memory_get_usage() . "<br>";

      $host = "localhost";
      $user = "root";
      $pswd = "";
      $database = "multikuldo";

      $connect = mysqli_connect($host, $user, $pswd, $database);

      mysqli_query($connect, "set names utf8");
      mysqli_select_db($connect, $database);

      $sleeptime = 3; // 3 is optimal
      $runtime = 0;
      $testcount = 50; // Send test email every 50th email
      $testemail = "yourtest@gmail.com";
      $table_name = "emails";

      function getRandElementFromArray($array)
      {
      return $array[array_rand($array, 1)];
      }

      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/

      $fromemail[0][] = "sender@gmail.com";

      $sender[0][] = "sender name1";
      $sender[0][] = "sender name2";
      $sender[0][] = "sender name3";

      $subject[0][] = "subject1";
      $subject[0][] = "subject2";
      $subject[0][] = "subject3";

      $body[0][] = "Welcome!";
      $body[0][] = "Dear Editor!";

      $body[1][] = "body1";
      $body[1][] = "body2";
      $body[1][] = "body3";
      $body[2][] = "body1";
      $body[2][] = "body2";
      $body[2][] = "body3";
      $body[3][] = "body1";
      $body[3][] = "body2";
      $body[3][] = "body3";

      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/
      /*SENTENCE VARIATIONS*/

      do {
      // Start the loop if not redirected via JavaScript
      sleep($sleeptime);

      // Create a PHPMailer instance
      $phpmailer = new PHPMailer\PHPMailer\PHPMailer();

      set_time_limit(9999999);

      $isz = file_get_contents("isz.txt"); // previous email id
      echo "<br>isz: " . $isz . "<br>";

      // Fetch the data to be sent
      $query =
      "
      SELECT *
      FROM " .
      $table_name .
      "
      WHERE
      sended = '0'
      LIMIT 1

      ";

      echo $query . "<hr>";
      $sql_result = mysqli_query($connect, $query);

      $result = [];
      while ($row = mysqli_fetch_array($sql_result)) {
      $result[] = $row;
      }
      //print_r($result);
      if (empty($result)) {
      die("the end");
      }
      //exit;

      file_put_contents("isz.txt", $result[0]["id"]); // one more than what was written
      $log = file_get_contents("log.txt");

      try {
      // Debugging settings
      $phpmailer->SMTPDebug = 1;

      // Set character encoding
      $phpmailer->CharSet = "UTF-8";

      // Set email format to HTML
      $phpmailer->isHTML(true);

      // Sender details
      $phpmailer->setFrom(
      $fromemail[0][rand(0, count($fromemail[0]) - 1)],
      $sender[0][rand(0, count($sender[0]) - 1)]
      );
      $phpmailer->AddReplyTo(
      $fromemail[0][rand(0, count($fromemail[0]) - 1)],
      $sender[0][rand(0, count($sender[0]) - 1)]
      );

      $phpmailer->Sender = $phpmailer->From;

      // Recipient details
      $cimzettnev = explode("@", $result[0]["email"]);
      $phpmailer->addAddress($result[0]["email"], $cimzettnev[1]);

      // SMTP connection settings for sending the email
      $phpmailer->isSMTP();
      $phpmailer->Host = "ssl://smtp.gmail.com";
      $phpmailer->Port = 465;
      $phpmailer->SMTPAuth = true;
      $phpmailer->AuthType = "LOGIN"; // 'CRAM-MD5', 'PLAIN', 'LOGIN
      //$phpmailer->SMTPSecure = false;
      $phpmailer->SMTPSecure = "ssl";
      //$phpmailer->SMTPAutoTLS = false;
      $phpmailer->Username = "your@gmail.com";
      $phpmailer->Password = "12345";

      // Message content
      $phpmailer->Subject = "" . $subject[0][rand(0, count($subject[0]) - 1)];

      // HTML body
      $phpmailer->Body =
      '<html> ... </html>'; // Truncated for brevity

      // Send the email
      $phpmailer->Send();

      // Mark the sent data
      $query =
      "
      UPDATE " .
      $table_name .
      "
      SET sended = 1
      WHERE id = '" .
      $result[0]["id"] .
      "'
      ";

      mysqli_query($connect, $query);

      } catch (phpmailerException $err) {
      // Log mailer error
      } catch (Exception $err) {
      // Log general error
      }
      } while (!empty($result));
      echo memory_get_usage() . "<br>";
      echo "ready";
      ?>

      संबंधित लिंक:

        सरल और उन्नत दोनों विधियाँ बड़े पैमाने पर ईमेल भेजने के लिए लचीले विकल्प प्रदान करती हैं। अपनी ज़रूरत के अनुसार समाधान चुनें और अपने अभियान की शुरुआत करें!