Ghetto Mailer: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „Ryans sehr alte Lösung für einfachen E-Mail Versand (genutzt bis 2013). Inzwischen nutzt Ryan ProcessWire Seiten und versendet über directly from this serv…“)
 
(kein Unterschied)

Aktuelle Version vom 19. Januar 2019, 12:23 Uhr

Ryans sehr alte Lösung für einfachen E-Mail Versand (genutzt bis 2013). Inzwischen nutzt Ryan ProcessWire Seiten und versendet über directly from this server using PW's WireMail interface with either Teppo's WireMailSwiftMailer or Horst's WireMailSMTP. Interessant bei untenstehender Lösung: durch das 3 Sekunden Intervall gab es wohl niemals Probleme. Bis zu einer gewissen Menge sicher ausreichend.

Quelle: https://processwire.com/talk/topic/3433-newsletter-system-for-pw/

I'm embarrassed to admit that I send them myself, using a script I wrote more than a decade ago. It's always worked fine, so I've just stuck with it. But I agree with the guys that say it's better to use a service. Someday I will take that advice myself too. But if you want to use what I'm using, here you go…

First you'll need a text file with your subscribers in it. 1 email address per line. It can be only a few, or it can be thousands.

subscribers.txt

bob@email.com
jim@bigcompany.com
mike@little-org.com

Now you'll need the email content. You'll want 1 HTML file and 1 text file. Both should have the same content:

template.html

<html>
<head>
<title>test email</title>
</head>
<body>
<h1>Test</h1>
<p>This is a test email</p>
</body>
</html>

template.txt

TEST
This is a test email

email.php

Place this PHP file in the same directory as the above files. Edit the DEFINEs at the top as necessary. I apologize in advance for this rather awful email solution, but hey it works, and that's why I haven't bothered to rewrite it in 10 years. :) To run, place the files on a web server and then load in your browser. It should start emailing immediately, once every 3 seconds. If you are sending thousands, then it might take several hours. But because it sends slow, it never seems to caught up in any filters.

    <?php
     
    /**
     * GhettoMailer v1.0 
     *
     * © 1994 by Ryan Cramer
     *
     */
     
    define("NAME_FROM",     '"Your Name Here"');
    define("EMAIL_FROM",    "you@domain.com");
    define("REPLY_TO",      "you@domain.com");
    define("ERRORS_TO",     "you@domain.com");
    define("SUBJECT",       "ProcessWire News & Updates - April/May 2013");
     
    define("SECONDS", 3); // seconds delay between each email sent
    define("TEXT_ONLY", false); // set to true if not sending HTML email
    define("TEST_MODE", false); // set to true if just testing a send
    define("TEST_MODE_EMAIL", "you@domain.com"); // email to send to when testing
    define("SUBSCRIBERS_FILE", "subscribers.txt"); // file containing subscribers, 1 email per line
    define("TEMPLATE", "template"); // file containing email to send: template.html and template.txt
    define("X_MAILER", "GhettoMailer 1.0"); 
     
    /**************************************************************************************/
     
    ini_set("auto_detect_line_endings", true);
     
    function mailTextHtml($to, $subject, $message_text, $message_html, $headers) {
      // exactly like regular mail function except sends both text and html versions
      
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      $headers = trim($headers); // in case there is a trailing newline
      
      $headers .=               
        "\nX-Mailer: " . X_MAILER . "\n" .
        "MIME-Version: 1.0\n" .
        "Content-Type: multipart/alternative;\n  boundary=\"$mime_boundary\"";
      
      $message = 
        "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"utf-8\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        "$message_text\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/html; charset=\"utf-8\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        "$message_html\n\n" .
        "--{$mime_boundary}--\n";
      
      $success = @mail($to, $subject, $message, $headers, "-f" . ERRORS_TO);
      return $success;
    }
     
    /**************************************************************************************/
     
    $start = 0;
    if(!empty($_GET['start'])) $start = (int) $_GET['start'];
     
    $subscribers = file(SUBSCRIBERS_FILE);
     
    if(isset($subscribers[$start])) {
      
      $line = trim($subscribers[$start]);
      $total = count($subscribers)-1;  
      $email = trim($line);
      $subject = SUBJECT;
      
      if(isset($_GET['pause'])) {
        $meta = '';
        $content = "[$start/$total] Paused. <a href=\"email.php?start=$start\">Resume</a><br />";
     
      } else {
     
        $meta = '<META HTTP-EQUIV=Refresh CONTENT="' . SECONDS . '; URL=./email.php?start=' . ($start+1) . '">';
     
        $content =   
          "[$start/$total] Emailing <u>$email</u><br />" .
          '<a href="email.php?pause=1&start=' . ($start+1) . '">Pause</a><br />';
     
        if(TEST_MODE) $email = TEST_MODE_EMAIL;
     
        $headers =   
          "From: " . NAME_FROM . " <" . EMAIL_FROM . ">\n" .
          "Reply-To: " . REPLY_TO . "\n" .
          "Errors-To: " . ERRORS_TO;
     
        $content .= "Subject: <b>$subject</b><br />";
     
        $bodyText = file_get_contents(TEMPLATE . ".txt");
     
        if(TEXT_ONLY) {
          mail($email, $subject, $bodyText, $headers, "-f" . ERRORS_TO);
     
        } else {
          $bodyHtml = file_get_contents(TEMPLATE . '.html'); 
          mailTextHtml($email, $subject, $bodyText, $bodyHtml, $headers); 
        }
     
        $handle = fopen("email.log", "a");
        if($handle) {
          fwrite($handle, "[$start/$total]: $email\n");
          fclose($handle);
        }
      }
     
    } else {
      $meta = '';
      $content = "Subscriber emailing finished. $start emails sent.";
    }
     
    ?>
    <html>
    <head>
    <?=$meta; ?>
    </head>
    <body>
    <?=$content; ?>
    </body>