[SOLVED] Inserting Variables into an email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

[SOLVED] Inserting Variables into an email

Post 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.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Did you look in php.net for mail?
Or could we have a look at your code?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Are you transforming the variables from $_POST['firstname'] to $firstname?

Check this out, maybe it can help.
viewtopic.php?t=15856
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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 =
===================================
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
Post Reply