Page 1 of 1

[SOLVED] Inserting Variables into an email

Posted: Sat Dec 20, 2003 1:53 pm
by lazersam
Hi all

I have a form that requests some text input. This is then passed on to a handler script. The text input ($email_text) will be emailed out automatically using PHP... however, I want to include some variables inside the text where the submitter has used variable names such as $first_name and $last_name.

Whenever I try to do this I am unable to insert the data and the actual variable names remain there instead!

Can anyone tell me how to do this please?

Regards

Lawrence.

Posted: Sat Dec 20, 2003 2:04 pm
by Derfel Cadarn
Did you look in php.net for mail?
Or could we have a look at your code?

Posted: Sat Dec 20, 2003 2:11 pm
by m3mn0n
Are you transforming the variables from $_POST['firstname'] to $firstname?

Check this out, maybe it can help.
viewtopic.php?t=15856

Posted: Sat Dec 20, 2003 2:26 pm
by lazersam
This is my form:

Code: Select all

<head>
<title>Sender</title>
</head>

<body>

<form method="POST"  action= "process_sender.php"   >
  
  <p>Email to:</p>
  <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="278" id="table1">
    <tr>
      <td width="44"><input type="radio" value="all_verified" checked name="radio_but"></td>
      <td>All Verified Subscribers</td>
    </tr>
    <tr>
      <td width="44"><input type="radio" name="radio_but" value="non_verified"></td>
      <td>Non Verified Subscribers</td>
    </tr>
  
  </table>
  
  <p>Subject Line: 
  <input type="text" name="subject_line" size = "26" >
  
  </p>
  
  <p>Paste Email Here....</p>
  <p><textarea rows="7" name="email_text" cols="32"></textarea></p>
  <p><input type="checkbox" name="email_to_self" value="ON"> Send Test to Self!</p>
  <p><input type="submit" value="Send" name="B1"><input type="reset" value="Reset" name="B2"></p>
  <p> </p>
</form>

</body>

</html>
The text input $email_text is carried to process_sender.php. Here it accesses a database and retrieves $first_name etc..

This is the process_sender.php


Code: Select all

<?php



if (  !$radio_but or !$subject_line or !$email_text ){
	echo "Not all fields are complete. Press Back! <br>";
	exit ();
	}
	
	
	
	#Make the connection to my_sql
	$conn = @mysql_connect("localhost", "lazersam_admin", "*****")
	or die ("Sorry - could not connect to MySQL: " .mysql_error());
	
	#Select the specified database
	$rs = @mysql_select_db("lazersam_members", $conn) or die ("Err:Db");
		
		
		
				
	
	if ($radio_but == "all_verified")
	{
	#Create query
	$sql = "select * from subscribers where verified = "1"    ";
	}#End of "All Verified" If Statement
	else
	{
	$sql = "select * from subscribers where verified = "0"    ";
	}
	
	
	#Execute Query
	$rs = mysql_query($sql, $conn) or die ("Could not do query: " .mysql_error());
	
	#Get Num of rows
	$num_of_rows = mysql_num_rows($rs);
	
	echo "Num of email addresses found: $num_of_rows <br>";
		if ($num_of_rows == 0){
		echo "Error... did not find entry in database!";
	
		exit ();
					}
	
	
	#Send messages
		include ("headers.php");
		
		#Get Data=====================================================
		#This bit works fine...
		while ($row = mysql_fetch_assoc($rs) ){
			$first_name = $row['first_name'];
			$last_name = $row['last_name'];
			$affiliate_id = $row['affiliate_id'];
			$email = $row ['email'];
			
			$msg = $email_text;
			
			
			#  Although I DO have the $first_name and $last_name could easily
			#  print out the data, I cant insert the data into the text string!
			#  
			
			
			
			echo "$msg <br>";
			
			
			#$success=mail($email, $Subject_line, $msg, $headers);		

?>
The processor has the $email_text data PLUS the $first_name and $last_name data. The user has inserted $first_name into the email_text field when he submits the form. This is apparent when the data reaches the processor.


Lawrence.

Posted: Sat Dec 20, 2003 2:45 pm
by Derfel Cadarn
As Sami wrote, I think you need to do this

Code: Select all

<?php
$firstname = $_POST['firstname'];

?>
for all the variables you want to use in the e-mail.


===================================
= Yes, I'm trying to get my 101st posting today =
===================================

Posted: Sat Dec 20, 2003 3:45 pm
by lazersam
Hi

I think I have complicated my question so I have written a script to demonstate the problem...

This is a form that sends the text string "$inserted_text" to the processor.php.

Code: Select all

&lt;html&gt;

&lt;head&gt;

&lt;title&gt;Semd Email&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;form method="POST" action="process.php"&gt;
  
  &lt;p&gt;Message:&lt;/p&gt;
  &lt;p&gt;&lt;textarea rows="2" name="message" cols="28"&gt;$inserted_text&lt;/textarea&gt;&lt;/p&gt;
  &lt;p&gt;&lt;input type="submit" value="Submit" name="B1"&gt;&lt;input type="reset" value="Reset" name="B2"&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;
The processor assigns a value to $inserted_text (namely "Insert this into the message").

It then prints the message. I would like the $inserted_text variable to be replaced by the "Insert this into the message" string.



Code: Select all

<?php

$msg = $_POST['message'];
	


	$inserted_text = "Insert this into the message";
	
	echo "$msg";
	

?>
Please can you advise?

Regards

Lawrence.

Posted: Sun Dec 21, 2003 4:24 am
by lazersam
It's OK now :D

I have gone back to my PHP Bible and studied long and hard. I can solve my problem by using the str_replace () function.

Regards

Lawrence.