Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2003, 09:37 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Send Emails Dependably or verify receipt

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
 
Old July 23rd, 2003, 10:59 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Just a thought:
But why don't you just do a loop when you send the mail and include a single address in the 'to:' field? That would eliminate the need for blind carbon copy mailing and elevate your problem with hotmail and other providers.

They are bouncing your mail because the recipient's address isn't specified in the 'to:' field. It has nothing to do with sending mail from a script, there's no way of knowing *what* made the mail.

hth
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 23rd, 2003, 11:00 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

'alleviate' not 'elevate' ...lol!

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 24th, 2003, 10:05 PM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Rich,

It may have seemed like a dumb problem, but it had me stumped for a long time. I read something on devshed about not being able to put the mail function in a loop and so just never tried it for myself. I'm seeing my emails actually get to all of the recipients now, but haven't tested it for large numbers of recipients yet.

:)


---------------
Trey Carroll
 
Old July 25th, 2003, 01:51 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

There are no dumb problems!

Hotmail has a feature which allows users to deflect messages which do not specifically address them in the 'To:' line. What happens when this feature is enabled, I'm not sure... they are either deleted or sent to the 'junk' folder... which is probably the case. That's why you weren't getting the bounced messages.

If your recipients list grows too large for mail() to be practical, you might consider an SMTP mail class. I'm actually fooling with one at the moment, code I picked up out of the wrox professional php programming book, the older one.

I can share that code with you if you like, I copied it directly from the book... it breaks down the recipient list, connects to an SMTP server and sends to a single recipient at a time. At least that's my interpretation on it. Haven't actually got to the point of testing it yet.

The other thing you might consider with a large mailing list is running a cronjob (if you are on Unix), You can set it to run at a certain time, presumably when there is the least server usage, and it will automatically fire off your script and send out the mail.

Good luck in your mail endeavors!

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old July 25th, 2003, 09:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by quesadilla5


The other thing you might consider with a large mailing list is running a cronjob (if you are on Unix), You can set it to run at a certain time, presumably when there is the least server usage, and it will automatically fire off your script and send out the mail.
Well, if you find that you're into running cron jobs, it would be easier, surely, to use proper listserver software to do the job. Most Unux hosts can offer Majordomo, which for all its faults, is better than relying on whatever MTA your using not falling over in the middle of its send. The problem with using any MTA, such as sendmail, to send to big lists, is that they fork whenever asked to send to multiple addresses, eating up memory as they do so.

Listservers also have lots of built-in features to help cope with bounces, transient problems, resends, and whathaveyou - problems specific to mailing lists.

Dan

Sales brochure for MS Office that just came through my door:
"Not just an upgrade. A whole new revenue opportunity."
Ha ha ha!

"Assume the position: it's time to realy start generating some revenue!"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to send emails maitias C# 2005 2 February 1st, 2006 01:09 PM
send emails to several users femig VBScript 4 April 15th, 2005 07:42 AM
Send two emails instead of one ss2003 Beginning PHP 3 January 28th, 2005 11:54 AM
Send Emails in ASP tdaustin Classic ASP Basics 4 August 12th, 2004 07:09 PM
Send Emails and Attachments. mistry_bhavin General .NET 2 August 11th, 2004 10:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.