Re: Mail sending program (beginner)

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

 



i don't really understand what your trying to do here but if im correct
there is a much easier way...

if you are trying to make a newletter sign up this is simple. there is no
reason to send the email to the database simply have the enter thier email
address, use regex to check it, if it passes that test, send it and a uniqe
conformation number to a database, and use mail() to send them the
conformation information with the link to the confirm page.  they then click
on the link to confirm.php or whatever and it will check the database for
that email or number... if they are both there and the both match it will
erase it from the confirm database and send the info to the newsletter
database. i will give you an example...

//start index.php or news_signup.php or something of the sort...
<?php
//first we define the function for the email check...

 function is_valid ($email) {

  if
(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})
$", $email)) {

  return true;

  }
 }
//check to see if the signup form button was pressed and perform tests on
the form data..

 if ($HTTP_POST_VARS[ADD_USER]) {
  if (!$HTTP_POST_VARS[email]) {
   $e = "error! you must enter your email address!";
  }
  else {
  if (!is_valid($HTTP_POST_VARS[email]) {
   $e = "error! your email address is invalid!";
  }
  else {
   //all the tests passed at this point, so we make that conf # i talked
about...
   $conf = uniqid("nuser");
   //now we eneter all the information in to a database called add that has
three feilds.. auto_increment id. email. confid.
   mysql_query("insert into add (id, email, confid) VALUES (null, '$email',
'$conf')");
   //now we send the email
   $to      = "$email";
   $subject = 'Your Newsletter subscription...';
   $message = "Hello $email, your registration is almost complete! all you
have to do now is confirm your registration. to do this simply click on this
link: <a
href=\"http://yoursite.com/newsletter/confirm.php?confid=$conf\";>Complete
Registration Now</a> or, copy and paste this into your web browser:
www.yoursite.com/newsletter/confirm.php?confid=$conf <br><br><b>Please Note:
your resgistration will expire in exactly 24 hours from when you clicked the
register button, so please confirm your registration now!</b>";
   $message = wordwrap($message);
   $headers = 'From: do_not_reply@xxxxxxxxxxxxxxxx' . "\r\n";
   $headers  .= 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   mail($to, $subject, $message, $headers);
   $e = "congratulations you have signed up! You should recieve an email
shortly to confirm.";

  }
  }
  }
  else {
  //now we actually display the form
?>

<form method="post" action="whateveryoucallit.php">
email:<br>
<input type="text" name="email" value="<?php echo("$email"); ?>"
length="35"><br>
<input type="submit" name="ADD_USER" value="SIGN UP!">
</form>

<?php
//now we wnd the orginal php block
}

//now we dipslay the error message if there was any...

if ($e) {
echo("$e");
}

?>

this is the whole first page. simple. not too many languages. now the
confirm page.


//start confirm.php or whatever.

<?php
//first we make sure they got to this page by email, making sure the
variable $conf is set
if (!$conf) {

 $e = ("Sorry, you must have an id in order to complete the registration...
please click on or copy and paste the link from your email into your web
browser.. thank you - staff");
}
//now we make sure that the confid is in the database
else {
$cc1 = mysql_query("SELECT * from add where confid = '$conf'") or
die(mysql_error());
$ccc1 = mysql_fetch_array($cc1);
if (!$ccc1) {
$e = ("Sorry, The id you have enterd does not match any of our records...you
may have already confirmed your email - staff");
}
//grab the data where the ids match and enter the in to the new database
else {
$query = mysql_query("select * from conf where confid = '$conf'");
while ($d = mysql_fetch_array($query)) {
$email = "$d[email]";
mysql_query("insert into users (id, email) VALUES (NULL, '$email')") or
die(mysql_error());
//delete from the conf database
mysql_query("delete from conf where conf = '$confid'") or
die(mysql_error());
$s = ("Congratulations <b>$email</b>, you have successfully signedup... you
will now get our monthly emails...");
}
}
}
}
if ($e) {
echo("<b><center>$e</b></center>");
}
if ($s) {
echo("<b><center>$s</b></center><br><b><center>$s1</b></center><br><center><
b>You will now be redirected to login...</b></center>");
}
?>

this is not perfect, but should give you an idea of what your doing and a
different way (possibly better) of doing this.

----- Original Message ----- 
From: "aci india" <foraci@xxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Friday, June 09, 2006 10:24 PM
Subject:  Mail sending program (beginner)


> Dear group,
>
> Description:
>
> Following is the code which I tried for learning propuses. It is a mail
> sending program
>
> which checks weather the user enters a correct mail id If he enters the
> correct mail ID then
>
> user name and e-mail will get stored in the mysql data base table. After
> getting stored
>
> then mail will sent to the user as a accknoledg process. For the propus of
> mail subject
>
> and mail body, I have created two external files.
>
> The problem:
>
> Filling the rows and col in the data base is no problem at all. The value
> getting stored.
>
> But after clicking the button for accknoledg the browser takes lot of time
> in execution. I doubt
>
> with either the file handling functions or infinit loop. Or Am I missing
> somthing basic? Pls mention if you faced a similear situation with file
> handling and infinit loop. Thanks.
>
>
>
> NOTE: Sorry for the very long code
>
> ============================== the
code=====================================
>
> The html file:
>
> ^^^^^^^^^^^^
>
> <HTML>
>
> <body>
>
> <form method="post" name="my_frm" action="mail.php">
>
> <TABLE align ="center">
>
> <tr>
>
> <td>
>
> Name:
>
> </td>
>
> <td>
>
> <input id="text" name="user_name">
>
> </td>
>
> </tr>
>
> <tr>
>
> <td>
>
> E-mail:
>
> </td>
>
> <td>
>
> <input id="text" name="user_email">
>
> </td>
>
> </tr>
>
> </TABLE>
>
> <table align = "center">
>
> <tr>
>
> <td>
>
> <input type="submit" name="click_btn" value="yes,I want my 'CMS'
NewsLetter
> NOW!" align=middle onClick="validt()">
>
> </td>
>
> </tr>
>
> </table>
>
> <table align="center">
>
> <tr>
>
> <td>
>
> <a href="test.html"><font size=2>Or click here to see how you can learn
> everything
>
> <br>
>
> you need to know about CMS without signing up for our free newsletter
> </font>
>
> </a>
>
> </td>
>
> </tr>
>
> </table>
>
> <script language="javascript">
>
> function validt()
>
> {
>
> var str= document.my_frm.user_email.value;
>
> var name = document.my_frm.user_name.value;
>
> if(str.indexOf("@") <0 || str.indexOf(".") < 0)
>
> {
>
> my_error()
>
> }
>
> else
>
> alert("thanks for entering valid e-mail");
>
> }
>
> function my_error()
>
> {
>
> alert("pls enter a correct e-mail ID");
>
> document.my_frm.user_email.value = " ";
>
> }
>
> </script>
>
> </form>
>
>
>
>
>
>
>
> </body>
>
> </HTML>
>
> the mail.php file:
>
> ^^^^^^^^^^^^^^^
>
> <?php
>
> /*mail body file*/
>
> function cnd($fname)
>
> {
>
> $fil = fopen($fname,"r");
>
> if(!$fil)
>
> die("fopen error");
>
> while(!feof($fil))
>
> $buffer = fgets($fil);
>
> while($buffer != NULL)
>
> {
>
> if($buffer == "\n.") /*a line should not start with .*/
>
> $buffer = str_replace("\n.","\n..", $buffer);
>
> if($buffer == "\n" && $buffer > 70) /*should be less than 70*/
>
> $buffer = '\n';
>
> }
>
> fclose($fil);
>
> return $buffer;
>
> }
>
> /*mail sub file*/
>
> function sub($fname)
>
> {
>
> $fil = fopen($fname,"r");
>
> if(!$fil)
>
> die("fopen err in sub file");
>
> while(!feof($fil))
>
> $buff = fgets($fil);
>
> while($buff != NULL)
>
> {
>
> if($buff > 15)
>
> {
>
> ?>
>
> <script language="javascript">
>
> alert("the subject line should not be less than 15, pls check the
sub.txtfile");
>
> </script>
>
> <?php
>
> }
>
> }
>
> fclose($fil);
>
> return $buff;
>
> }
>
> function fetch_names()
>
> {
>
> $var = mysql_connect("localhost","root","");
>
> if(!$var)
>
> die("could not connect".mysql_error());
>
> $db = mysql_select_db("sathya_clon",$var);
>
> if(!$db)
>
> die("could not find the data base".mysql_error());
>
> $result = mysql_query("select name from users",$var) or die("unable to
fetch
> rows");
>
> $rows = mysql_fetch_array($result);
>
> while($rows)
>
> {
>
> for($i=0; $i<= $rows ; $i++)
>
> for($j=0;$j <= i-1 ; $j++)
>
> $names[$i] = $rows[$j];
>
> }
>
> return $names;
>
> }
>
> function fetch_emails()
>
> {
>
> $var = mysql_connect("localhost","root","");
>
> if(!$var)
>
> die("could not connect".mysql_error());
>
> $db = mysql_select_db("sathya_clon",$var);
>
> if(!$db)
>
> die("could not find the data base".mysql_error());
>
> $result = mysql_query("select email from users",$var) or die("unable to
> fetch rows");
>
> $rows = mysql_fetch_array($result);
>
> while($rows)
>
> {
>
> for($i=0; $i<= $rows ; $i++)
>
> for($j=0;$j <= i-1 ; $j++)
>
> $email[$i] = $rows[$j];
>
> }
>
> return $email;
>
> }
>
> $var = mysql_connect("localhost","root","");
>
> if(!$var)
>
> die("could not connect".mysql_error());
>
> $db = mysql_select_db("sathya_clon",$var);
>
> if(!$db)
>
> die("could not find the data base".mysql_error());
>
> $name = $_POST['user_name'];
>
> $mail = $_POST['user_email'];
>
> $db_q = mysql_query("insert into users values('$name','$mail')");
>
> if(!$db_q)
>
> die("mysql error");
>
> $condt = cnd("cond.txt");
>
> $sub = sub ("sub.txt");
>
> $name = fetch_names();
>
> $email = fetch_emails();
>
> $mail_stat = mail($email,$sub,$condt,"from:comp@xxxxxxxxxx");
>
> if($mail_stat == NULL)
>
> echo "mail sent failed";
>
> else
>
> echo "mail sent sucess. Pls check for mail for further acction";
>
> ?>
> ==================code ends========================
>
> sorry english is not my native language.
>

-- 
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