Hello all,
I have created a basic email console for a site that allows users to send email. It works very simply, taking the content of a textarea as the message and then sending the message to a list of recipients. The one 'twist' is that it is important that other recipients not be able to see the other recipients' email addresses. For this reason I add each one to bcc line in the header rather than to the usual 'to' parameter.
It seems to work well in most cases, with some puzzling exceptions. It works predictably, meaning that people who don't get the emails never get them, and people who do get emails always do. So it's like the fact that I'm sending email from a script is something that some email server detect and disallow. For example any email that I send to a hotmail address disappears.
Here come my 'How Do I?' question:
For this thing to be remotely useful to anyone I need to at least be able to tell who didn't get their email. Right now even when I send mail using a valid from address, they never receive any bounced emails- even when sending email that that address does produce a bounce with a regular email program. What do you have to do to get the bounces routed correctly? How can I at least show people a list of recipients who they will need to contact another way?
Here is the code I'm now using to send the emails:
<?php
session_start();
require_once("../db.inc");
$aryEmailList = $_POST['checked_recipient'];
$aryEmails = $_POST['email'];
$arySendList = array();
$cnt =0;
//regular expression for a valid email
$pattern = '/^([-a-zA-Z0-9_.]+)@(([-a-zA-Z0-9_]+[.])+[a-zA-Z]+)$/i';
//iterate through the array putting valid emails into the array $arySendList
for($i=0;$i<=sizeof($aryEmailList);$i++){
if(isset($aryEmailList[$i]) && (preg_match($pattern,$aryEmails[$i]) ) ){
$arySendList[$cnt] = $aryEmails[$i];
$cnt++;
}
}
//eliminate duplicate emails from the array
$aryUniqueSendList = array_unique($arySendList);
//sort the array alphabetically ASC
sort($aryUniqueSendList,SORT_STRING);
$subject = stripslashes($_POST['subject']);
$email_text = stripslashes($_POST['msg']);
$headers = "From:".$_POST['from']."\r\n";
//used by the sql statement
$from = $_POST['from'];
$max = sizeof($aryUniqueSendList) -1;
for ($i=0;$i<sizeof($aryUniqueSendList);$i++){
$headers .= "Bcc:".$aryUniqueSendList[$i]."\r\n";
if ($i==$max){
$recipients .= $aryUniqueSendList[$i];
}
else{
$recipients .= $aryUniqueSendList[$i].", ";
}
}
mail("P.R.E.P. Parent", $subject, $email_text, $headers);
//IF THIS IS A TEACHER TO PARENT EMAIL
if(preg_match("/teacheradmin/",$referer)){
$link = db_connect();
$file_name = "../teacheradmin/sent_emails/$teacher_id/{$teacher_id}_".time().".txt";
$strSQL = "INSERT INTO emails (email_id, subject, sender, recipients, file_name, teacher_id) VALUES (NULL, '$subject', '$from', '$recipients','$file_name','$teacher_id')";
if ($worked = mysql_query($strSQL,$link)){
if (!($fp = fopen($file_name,"w+"))) die("cant open file: $file_name for writing! /nReport this error to:
[email protected]");
fwrite($fp,$email_text);
fclose($fp);
}
}//end if preg_match("/teacheradmin...
?>
<html>
<head>
<link rel="stylesheet" href="../style/prep_style.css" type="text/css">
</head>
<body>
<center><h3>Sending Email...</h3></center>
<?php
//had to do this because of problems with sessions and the back button
$referer = $_POST['referer'];
echo " <script language=\"JavaScript\">\n";
echo " setTimeout(\"document.location.href='$referer';\", 2000);\n";
echo " </script>";
?>
</body>
</html>
---------------
Trey Carroll