Re: email list 101

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 1/16/2011 7:09 AM, Kirk Bailey wrote:
So, in php, I want a program to handle sending out a mail list. All this
is going to do is be a filter to exclude non subscribers, and send a
copy to every person in the subscriber file. This is pretty simple in
python, but this is not my mother tounge we speak here, so let's talk in
php instead.

If the submission does not come from a member, the script simply aborts.
So the script should read the subscriber file, and if the source From:
does not appear there, DIE. If it is there, walk the array and send a
copy there, then end.

Now how to do this in php? Is there an off the shelf solution?



Is this something that you plan on executing with your SMTP server or through a web script?
Do you care how the subscribers file is managed?  If at all...
How is the subscriber file formatted?

With the above questions unanswered, I would do something along the lines of the following.

form.php:
<html>
<form action="process.php" method="post">
INPUT:fromname:value (display name)
INPUT:fromaddr:value (email address)
INPUT:subject:Something special
INPUT:message:the message goes here
</form>
</html>


process.php:
<?php

# Clean input
if (get_magic_quotes_gpc())
    $clean_post = array_map('stripslashes', $_POST);


# Check for required fields
$msg = 'Please press your back button and try again.';
if ( empty($clean_post['fromaddr']) )
	die('From address is required.  '.$msg);
if ( empty($clean_post['subject']) )
	die('Subject is required.  '.$msg);
if ( empty($clean_post['message']) )
	die('Message is required.  '.$msg);

# Error on bad input
if ( !empty($clean_post['fromname']) &&
     strpos($clean_post['fromname'], PHP_EOL) )
	die('Invalid from name.  ' . $msg);
if ( strpos($clean_post['fromaddr'], PHP_EOL) !== false )
	die('Invalid from address.  ' . $msg);
if ( strpos($clean_post['subject'], PHP_EOL) !== false )
	die('Invalid subject.  ' . $msg);

# assuming the format is one email address per line
$subscriber_file = '/path/to/file';

$subscribers = array();
if ( file_exists($subscriber_file) )
	$subscribers = @file($subscriber_file);

# Check for the submitters email address in the subscribers list
if ( !in_array($clean_post['fromaddr'], $subscribers) )
	die('I don't see your email address in this list.  ' . $msg);

# By this point, everything should be checked and you should be ready
# to send your messages

# NOTE: Download and install a copy of phpmailer
# NOTE: the remaining is from the example.php included in phpmailer
# NOTE: I suggest using SMTP auth because it will allow you to generate
#       an email using a 3rd party email address then what is probably
#       allowed by your local mail server.

include '/path/to/phpmailer/class.phpmailer.php';

$mail = new PHPMailer();

$mail->IsSMTP();             // set mailer to use SMTP
$mail->Host = "mail.example.com";  // specify mail server host name
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "<fill this in>";  // SMTP username
$mail->Password = "<fill this in>"; // SMTP password

$mail->From = $clean_post['fromaddr'];
if ( !empty($clean_post['fromname']) )
{
  $mail->FromName = $clean_post['fromname'];
  $mail->AddReplyTo($clean_post['fromaddr'], $clean_post['fromname']);
} else {
  $mail->AddReplyTo($clean_post['fromaddr']);
}
$mail->Subject = $clean_post['subject'];
$mail->Body    = $clean_post['message'];
$mail->AltBody = strip_tags($clean_post['message']);

# add each person
foreach ( $subscribers AS $addr )
{
  $mail->AddAddress($addr);
  # if you have a name associated to that address, do this instead
  # assuming line format as: email@xxxxxxxxxxx,My Name
  list($addr, $name) = explode(',', $addr, 2)
  $mail->AddAddress($addr, $name);
}

if(!$mail->Send())
{
  echo "Message could not be sent. <p>";
  echo "Mailer Error: " . $mail->ErrorInfo;
  exit;
}

if ( ! headers_sent() )
  header('Location: http://www.example.com/done.php');

?>

--
Jim Lucas

DISCLAIMER: Unless otherwise noted, all code is untested!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux