Log in

View Full Version : Please help me to set up php emails to be sent to different person



linux1880
12-14-2007, 11:31 AM
?php
$to = "me@headache.com";
$subject = "hi how are you";
$body = "helllo hello this is test.. this is test ........";
$headers = "From: mail@mail.co.uk\r\n" .
"X-Mailer: xxxx LONDON";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

Hello friends,

I want to send this email to different users.

I already have variables like

emails: <?=$row['email_add'] ?>
body: <?=$row['noteforstudents'] ?>

I just want different person to receive email according to the details above. Do I need to set up session or require_once for this ?

I don't know how to setup pls help

boogyman
12-14-2007, 01:55 PM
so basically you would like to send it to this person AND yourself correct?

you can do something like this



// Creates an array of emails to send
$receip = array('me@headache.com');
$receip[] = $_POST['email'];
// Declare rest of email properties
...

// Declare error and success sent arrays
$error = array();
$success = array();

// Checks if email can be sent to each person, then puts address in appropriate array
foreach($receip as $to)
if( !mail($to, $subject, $body, $headers) )
{
$error[] = "Cannot Send Email To ". $to ."! Please try again later";
}
else
{
$success[] = "Email Sent Successfully To ". $to ."! Thank You.";
}
}

// Checks and prints error emails
if(isset($error)) {
echo "\n<ul>";
foreach($error as $err)
{
echo "\n\t<li>". $err ."</li>";
}
echo "\t</ul>";
}

// Checks and prints successful emails sent
if(isset($success)) {
echo "\n<ul>";
foreach($success as $succ)
{
echo "\n\t<li>". $succ ."</li>";
}
echo "\n</ul>";
}



now if you are grabbing them from the database you can replace


$receip[] = $_POST['email'];


with


$receip[] = $row['email_add'];

linux1880
12-14-2007, 03:11 PM
Hi boogyman

i made a sendmail.php file using

?php
$to = "me@headache.com";
$subject = "hi how are you";
$body = "helllo hello this is test.. this is test ........";
$headers = "From: mail@mail.co.uk\r\n" .
"X-Mailer: xxxx LONDON";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

then I get
helllo hello this is test.. this is test .......
on my email message

but when I used

<?php
$to = "webmaster@placett.co.uk";
$subject = "You have received feedback from LONDON";
$body = $row['noteforstudents'];
$headers = "From: an@angliancollege.co.uk\r\n" .
"X-Mailer:LONDON";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

I don't know why it's not sending the message now, I get empty message box. while $row['noteforstudents']; has got some message on it.

Please help

boogyman
12-14-2007, 09:05 PM
$body = $row['noteforstudents'];
the [b] should cause an error every time. That is bbCode, not php. I am not sure why you would like to bold the entire message but you can do something like


$body = "<strong>".$row['noteforstudents']."</strong>";

and that will create the bold weight that it would appear you were trying to do.

now what do you mean empty message box? like there is no body of the message or it doesnt send the message at all? If you are getting an empty message box try to output on the page the portion that is empty


if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
echo($row['noteforstudents']);
} else {
echo("<p>Message delivery failed...</p>");
}

and see what that produces. if there is nothing there, then its not recognizing anything being in that field, thus you would need to check your process and make sure everything is named correctly without any typos

thetestingsite
12-14-2007, 09:11 PM
the [b] should cause an error every time. That is bbCode, not php.


I think the user was just trying to show what code he/she is talking about; not that it is in the code itself.

Question to the OP, do have a query to the database in order to get the information? If not, you will need to do this.

Hope this helps.

linux1880
12-17-2007, 02:45 PM
$body = $row['noteforstudents'];

This code did work on the same page as there are other codings and the mail goes everytime the page refresh or when i enter the page.

But when i move this page to another php page like something.php it doesn;t pull the data from the database, any help would be greatful


Code:
<?php
$to = "memem@gmail.com";
$subject = "This is a email feedback from me";
$body = $row['noteforstudents'];
$headers = "From: ashfh@djd.co.uk\r\n" .
"X-Mailer: xxxx LONDON";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

boogyman
12-17-2007, 04:07 PM
move it how?

nothing will be retrieved from the database unless you have established a connection, and have selected the appropriate database you wish to collect information from.

if you are still having this dilemma after reviewing the above can you please post your code? so we can better help you troubleshoot.

linux1880
12-17-2007, 05:06 PM
I did just made a newpage with sendmail.php

and the page contains only


<?php
$to = "memem@gmail.com";
$subject = "This is a email feedback from me";
$body = $row['noteforstudents'];
$headers = "From: ashfh@djd.co.uk\r\n" .
"X-Mailer: xxxx LONDON";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

On the above page nothing comes from the database,

then with my little knowledge I tried to include config.php page where database details are located, now it says


<?
require_once "config.inc.php";


if (isset($_SESSION['id']))
{
$sql = "select * from applications where id='$_SESSION[id]'";
$rs = mysql_query($sql) or SQLError("Getting all the details...");
$row = mysql_fetch_array($rs);
unset($_SESSION['id']);
}
else
{
echo("<p>still not conecting databse ...</p>");
}

?>




<?=$row['noteforstudents'] ?>

<?php
$to = "me@mail.com";
$subject = "This is a email feedback for studenst";
$body = $row['noteforstudents'];
$headers = "From: laura@.co.uk\r\n" .
"X-Mailer: xxxx LONDON";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>


Now it;s gives me this error



still not conecting databse ...

Message sent!


I am doing all this blindly :( Forgive me for my ignorance but i need serious hel.

Thank you very much.

boogyman
12-17-2007, 06:33 PM
you are getting the note from the database correct?

you are getting

still not conecting databse ...
because the session 'id' variable is not set.
I am assuming that you are creating the session variable in your config.inc.php file which you are including.
You need to figure out where the session variable is being initiated, and if you have started a session in the first place.


if you want to take the 'noteforstudents' from the user form then it would be a different code.

linux1880
12-18-2007, 12:04 AM
you are getting the note from the database correct?

That's correct.

How do I set the session variables ?

The session in config.inc.php is set as follows


<?
session_start();
$s_self_page = substr ($_SERVER["PHP_SELF"], strrpos( $_SERVER["PHP_SELF"], "/" ) + 1);
$s_db_link = NULL;
$SQLdebug_g = true;
/**
*--------------------------------
*Here is all server configurarions
*MySQL Server
*--------------------------------
*/
define ("C_MYSQL_HOST", "host");
define ("C_MYSQL_USER", "user");
define ("C_MYSQL_PWD", "pass");
define ("C_MYSQL_DB", "db");

define ("C_HTTP_ROOT", "http://www.mysite.com" );
define ("C_VIRTUAL_ROOT", "folder/" );

define ("C_PAGE_HEADING", "abcdef");

define ("C_ADMIN_USERNAME", "user" );
define ("C_ADMIN_PASSWORD", "password.." );

define ("C_RESULTS_PER_PAGE", 30);
define ("C_AD_RESULTS_PER_PAGE", 10);

//Time Difference
define ("C_TIME_DIF", +5*60*60);//In seconds

//Rooms display
define ("C_DISPLAY_LENGTH", 15);
define ("C_DEFAULT_BG", "#f9f9f9");
define ("C_BG_COL_1", "#FFCC00");
define ("C_BG_COL_2", "#99FF00");

header ("Cache-Control: private");
header ("Pragma: no-cache");

?>

boogyman
12-18-2007, 02:11 PM
if that is your entire config.inc.php file then there is a problem.
You have started / continued the session, but you have not declared the $_SESSION['id'] variable which is why you are continuing to obtain the cannot connect error.

It also doesn't look like anywhere in your config file or the original file that you are even trying to connect to the database to begin with, so even if the variable was present you would have received an error about connecting to the database.

in some file you need the code below.


$c = mysqli_connect(C_MYSQL_HOST, C_MYSQL_USER, C_MYSQL_PWD);
$conn = mysqli_select_db($c, C_MYSQL_DB);


then before you run your query you can do



if($conn)
{
_QUERY_
}

linux1880
12-18-2007, 05:11 PM
xxxxx

linux1880
12-18-2007, 05:51 PM
hi boogyman i have another file called common.inc.php and contains the following details:


<?
ConnectToDb();

function ConnectToDb()
{
global $s_db_link;
$s_db_link = mysql_connect (C_MYSQL_HOST, C_MYSQL_USER, C_MYSQL_PWD, true) or SQLError("Connecting to database...");
mysql_select_db (C_MYSQL_DB, $s_db_link) or SQLError("Selecting database...");
}

function GetCount( $tbl_name, $where = '' )
{
if ( $where == '' )
$sql = "select count(*) from $tbl_name";
else
$sql = "select count(*) from $tbl_name where $where";

$rs = mysql_query( $sql ) or die("Conunting failed, " . mysql_error());
$data = mysql_fetch_array($rs);
return $data[0];
}

function GetSingleData ($table_name, $field_name, $condition)
{
$sql = "select $field_name from $table_name where $condition";

$rs_temp = mysql_query ($sql) or SQLError("Getting $field_name ...");

if (mysql_num_rows ($rs_temp) != 1)
{
return -1;
}
else
{
$row_temp = mysql_fetch_array ($rs_temp);
return $row_temp[0];
}
}

function SQLError( $at = '' )
{

$errNo=mysql_errno();
global $debugSQL_g;

if ( $debugSQL_g )
{
print( mysql_error() );
print("<br>At ".$at);
}
else
{
switch ( $errNo )
{
case 1142:
print( '<br><h4 align=center>Your Not Authorized to Perform this Action!</h4>' );
break;

case 1217:
print( '<br><h4 align=center>Referental Intregrity failed! There May be Child rows Depending</h4>' );
break;

case 1062:
print( '<br><h4 align=center>The Combination Already Exist</h4>' );
break;


default:
if ( $at == '' ){
print( 'Error in the Query<br>'.mysql_error() );
}else{
print( 'Error in the Query at '.$at.'<br>'.mysql_error() );
}
}
}
mysql_query("ROLLBACK");
exit;
}

function Redirect($loaction)
{
header("location: " . $loaction);
exit;
}

function ReportInfo($msg = "Un-authorized operation!")
{
header("location: info.php?msg=".urlencode($msg));
exit;
}

function SaySuccess($msg)
{
header("location: success.php?msg=".urlencode($msg));
exit;
}

function ShowPaging($page_no, $total_pages, $tag_key='', $tag_val='')
{
?>
<table cellspacing="0" cellpadding="2">
<tr>
<?
global $s_self_page;

$query_string = "";

if ($tag_key == '')
{
foreach ( $_GET as $key=>$var )
if( $key != "page_no" )
$query_string .= '&'.$key.'='.$var;
}
else
{
foreach ( $_GET as $key=>$var )
if( !($key == "page_no" || $key ==$tag_key))
$query_string .= '&'.$key.'='.$var;
$query_string .= '&'.$tag_key.'='.$tag_val;
}

if ($page_no >1)
print ('<td valign="middle"><a href="' . $s_self_page . '?page=' . ($page_no-1) . $query_string . '"><img src="images/button_back_on.gif" border="0"></a></td>');
else
print ('<td valign="middle"><img src="images/dd.gif">');
print ('<td valign="middle" class="BodyText">');
for ($i=0; $i<$total_pages; $i++)
{
if ($i+1 == $page_no)
print (($i+1) . " " );
else
print ('<a href="' . $s_self_page . '?page_no=' . ($i+1) . $query_string . '" class="PageLink">' . ($i+1) . '</a> ');
}
print ('</td>');

if ($page_no < $total_pages)
print ('<td valign="middle"><a href="' . $s_self_page . '?page_no=' . ($page_no+1) . $query_string . '"><img src="images/button_foward_on.gif" border="0"></a></td>');
else
print ('<td valign="middle"><img src="images/button_foward_off.gif"></td>');

?>
</tr>
</table>
<?
}

function DrawSortColumn ($col, $name, $check_key ='sort')
{

global $s_self_page;

$query_string = "";
$class = "SortLink";

foreach ( $_GET as $key=>$var )
if( $key != $check_key )
$query_string .= '&'.$key.'='.$var;


$current = isset( $_GET[$check_key] ) ? $_GET[$check_key] : "";

if( $col == $current )
{
?>
<a href='<?="$s_self_page?$check_key=".urlencode("$col desc")."$query_string"?>' class='<?=$class ?>'><b><?=$name?></b></a>
<?
}
else
{
?>
<a href='<?="$s_self_page?$check_key=$col$query_string"?>' class='<?=$class ?>'><b><?=$name?></b></a>
<?
}
}

?>


now i have changed the sendmail.php configuration as like this


<?
require_once "http://www.mysite/staff/config.inc.php";
require_once "http://www.amysite/staff/common.inc.php";

if (isset($_SESSION['id']))
{
$sql = "select * from applications where id='$_SESSION[id]'";
$rs = mysql_query($sql) or SQLError("Getting all the details...");
$row = mysql_fetch_array($rs);
unset($_SESSION['id']);
}
else
{
echo("<p>Still no solution...<p>");
}

?>

<?php
$to = "me@gmail.com";
$subject = "Your application updates from college !";
$body = $row['noteforstudents'];
$headers = "From: info@something.com\r\n" .
"X-Mailer: xxxx MBB";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

Doing that I get this error on php page.


Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'C_MYSQL_HOST' (1) in /home/folder/YGYLGGI/htdocs/staff/common.inc.php on line 7
Error in the Query at Connecting to database...
Unknown MySQL server host 'C_MYSQL_HOST' (1)
Still no solution...


Message sent!