Jump to content

Send Email and Populate Email List


Scud

Recommended Posts

Hey guys;

im just wondering, on our website users can submit their details and they are then sent to our mysql database where they are stored. This is all working fine and well, i now need the database to send an email to the address to user inputted when a new submission occurs. For example; a member registers, the record is added to the database and then the database sends an email back to the member confirming they registered.

Also is it possible for the database to populate a email list, this way when we have to send an email out, the database just puts all the email addresses together!

Link to comment
Share on other sites

Well i would use this bit of code

$to = $emailaddress;
$subject = "You have just registered!";
$body = "Welcome to our site";
mail($to, $subject, $body)

intergrate that into you registration and it should work depending if your server allows emails to be sent via php

Link to comment
Share on other sites

As suggested, mail() will throw an email to that user, also assuming u write some headers to avoid spam and correctly show the from address.

 

About the mailing list, u could setup a script which loops through all your users and sends an email to them, assuming u have a database field for emails. Illustration:

 

<?php
$resultsUsers = mysql_query('SELECT email FROM users');
while($valuesUsers = mysql_fetch_array($resultsUsers)){
   mail($valuesUsers['email'], 'Hi', 'Mail from me');
}
?>

 

If that's the scenario, the only problem u should care is the server load and amount of emails u can send daily. Hope that helps.

Link to comment
Share on other sites

  • 2 weeks later...

in regards to the mail suggestion, which should be intergrated. how would i get it so when the email is sent it says it was sent from such and such email addres.. also how do i intergrate that, has somebody got a full code for it?

Link to comment
Share on other sites

Below is the code, thanks in advance.

 

<link href="stylesheet.css" rel="stylesheet" type="text/css">
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("dbname", $con);$sql="INSERT INTO Table (Title, FirstName, LastName, Gender, Address, Suburb, State, Postcode, Country, Email, ContactNumber, ContactStatus)
VALUES
('".mysql_escape_string($_POST[Title])."','".mysql_escape_string($_POST[FirstName])."','".mysql_escape_string($_POST[LastName])."','".mysql_escape_string($_POST[Gender])."','".mysql_escape_string($_POST[Address])."','".mysql_escape_string($_POST[suburb])."','".mysql_escape_string($_POST[state])."','".mysql_escape_string($_POST[Postcode])."','".mysql_escape_string($_POST[Country])."','".mysql_escape_string($_POST[Email])."','".mysql_escape_string($_POST[ContactNumber])."','".mysql_escape_string($_POST[ContactStatus])."')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<center><font size=5 family=trebuchet MS color=#FFFFFF>You have successfully registered to our Mailing List</font></center>";mysql_close($con)
?>

<br><br><center><font size = 3 family=trebuchet MS color=#FFFFFF>Redirecting within 5 seconds.</font><center>
<meta http-equiv='refresh' content='5;url=http://google.com.au'>

Link to comment
Share on other sites

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("dbname", $con);$sql="INSERT INTO Table (Title, FirstName, LastName, Gender, Address, Suburb, State, Postcode, Country, Email, ContactNumber, ContactStatus)
VALUES
('".mysql_escape_string($_POST[Title])."','".mysql_escape_string($_POST[FirstName])."','".mysql_escape_string($_POST[LastName])."','".mysql_escape_string($_POST[Gender])."','".mysql_escape_string($_POST[Address])."','".mysql_escape_string($_POST[suburb])."','".mysql_escape_string($_POST[state])."','".mysql_escape_string($_POST[Postcode])."','".mysql_escape_string($_POST[Country])."','".mysql_escape_string($_POST[Email])."','".mysql_escape_string($_POST[ContactNumber])."','".mysql_escape_string($_POST[ContactStatus])."')";if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
$to = $_POST[Email];
$subject = "You have just registered!";
$body = "Welcome to our site";
mail($to, $subject, $body)
echo "<center><font size=5 family=trebuchet MS color=#FFFFFF>You have successfully registered to our Mailing List</font></center>";mysql_close($con)
?>

This code is untested and am pritty tired so it might not work anyway good look post back if it does not.

Link to comment
Share on other sites

I rewrote your script, fixing additional problems (like no quotes in post indexes), tiding up in some places and added headers for the mail(). Didnt test is so aware of possible typos, espacially in all those post variables.

 

<?php
$con = @mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('dbname');

$Title = mysql_real_escape_string($_POST['Title']);
$FirstName = mysql_real_escape_string($_POST['FirstName']);
$LastName = mysql_real_escape_string($_POST['LastName']);
$Gender = mysql_real_escape_string($_POST['Gender']);
$Address = mysql_real_escape_string($_POST['Address']);
$Suburb = mysql_real_escape_string($_POST['Suburb']);
$State = mysql_real_escape_string($_POST['State']);
$Postcode = mysql_real_escape_string($_POST['Postcode']);
$Country = mysql_real_escape_string($_POST['Country']);
$Email = mysql_real_escape_string($_POST['Email']);
$ContactNumber = mysql_real_escape_string($_POST['ContactNumber']);
$ContactStatus = mysql_real_escape_string($_POST['ContactStatus']);

$sql="INSERT INTO Table (Title, FirstName, LastName, Gender, Address, Suburb, State, Postcode, Country, Email, ContactNumber, ContactStatus)
  VALUES ('$Title', '$FirstName', '$LastName', '$Gender', '$Address', '$Suburb', '$State', '$Postcode', '$Country', '$Email', '$ContactNumber', '$ContactStatus')";
$results = @mysql_query($sql) or die('Query error: ' . mysql_error());

$to = $Email;
$from = 'your@email.com';
$subject = 'Welcome to our site';
$message = "You are now registered to mysite.com \r\n";
$message .= "Feel free to explore our site";
$headers = "From: " . $from . " \r\n";
$headers .= "Reply-To: " . $from . " \r\n";
$headers .= "Return-Path: " . $from . " \r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);

echo 'You have successfully registered to our Mailing List';
?>

Link to comment
Share on other sites

may i asked, what is the difference between the mysql_escape_... and the mysql_real_escape.

also if i wanted to set the table to encrypted fields, how do i decrypt them?

Link to comment
Share on other sites

may i asked, what is the difference between the mysql_escape_... and the mysql_real_escape.

also if i wanted to set the table to encrypted fields, how do i decrypt them?

 

From the manual:

 

This function will escape the unescaped_string , so that it is safe to place it in a mysql_query(). This function is deprecated.

 

This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current charset setting.

 

U should use mysql_real_escape_string() instead of it. As for the enctypted fields, that is handled by php, not by mysql. Its called hashing and cannot be decrypted back and thats its strength. If the decryption algorithm was possible and public, it would make no sense encrypting the passwords in first place.

Link to comment
Share on other sites

Also is it possible to format the email being sent, in terms of centering it and so on. I have a particular email that i use at the moment, (eml file) is it possible to format the code provided above to center text, and so on?

Link to comment
Share on other sites

U can format your email however u like using html, as long as u add these headers:

 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 

So from the last code i gave u, the full headers will be:

 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . " \r\n";
$headers .= 'Reply-To: ' . $from . " \r\n";
$headers .= 'Return-Path: ' . $from . " \r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();

Link to comment
Share on other sites

<?php
$message = 'Welcome to my site' . "\r\n";
$message .= "<a href='http://www.mysite.com/login'>Click here to login</a>\r\n";
$message .= "This is our logo <img src='http://www.mysite.com/mylogo.jpg' />";
?>

 

U need more guidelines?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.