Sending data of an associative array to an email address. Please help me out

Started on the old forum in May 2017 · view the original in the archive · old links redirect here

2 replies · 58 views · started 9 years ago · 1 min read

2 participants
zervas Junior · 16 posts #1

I am trying to create a php file that sends some information to the recipient's email address . My code works but , it only sends the $data and not the $key as well .

I want this php script to make the following output :

Firstname : Theodore (for example) Lastname : Zerbas (for examle)

Is there somebody to help me out ? thank you for your time.

 
 $firstname = $_POST['fname']; $lastname  = $_POST['lname'];  $to = "example@example.com";  $message = array("name"=>$firstname,"lastname:"=>$lastname);   foreach($message as $key=>$data) {            $message[]=implode("<br/>",$key.':'.$data);        }  if(mail($to,$subject,implode("<br/>",$message),implode("\r\n",$headers))) { 	echo "Your message has been sent successfully"; 	} else  {echo " Something went wrong";} 
something else Fellow · 2,296 posts #2

I'm a little confused on why all the arrays and imploding etc when you are hard writing the result into the array. Would it not be easier not to use an array and just hard write the result into the email?

$firstname = $_POST['fname']; 
$lastname  = $_POST['lname'];  
$to = "example@example.com";   
$message = 'name: '.$firstname.'<br/>lastname: '.$lastname.'<br/>';
if(mail($to, $subject, $message, implode("\r\n",$headers))) 
{     
    echo "Your message has been sent successfully";     
} 
else  
{
    echo " Something went wrong";
}

Or if for some reason an array is required due to checkboxes or something similar then:

$firstname = $_POST['fname']; 
$lastname  = $_POST['lname'];   
$to = "example@example.com";  
$message = array("name"=>$firstname,"lastname:"=>$lastname);  
$newMessage = '';
foreach($message as $key=>$data) 
{            
    $newMessage .= $key.':'.$data.'<br/>';
}  

if(mail($to, $subject, $newMessage, implode("\r\n",$headers))) 
{     
    echo "Your message has been sent successfully";     
} 
else  
{
    echo " Something went wrong";
}
zervas Junior · 16 posts #3

Thank you for your reply . I've tried the following code and it worked as well .

$message = array("firstname" => "name: ".$firstname,"lastname" => "lastname: ".$lastname);

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close