-1

I am trying to send an email using php and trying to define the to and from headers using php strings.

HTML:

<form action="send_email.php" name="form">

From Email:<br>
<input type="text" name="post4">
<br><br>
To Email:<br>
<input type="text" name="post1">
<br><br>
Subject:<br>
<input type="text" name="post2">
<br><br>
Body:<br>
<textarea name="post3" form="form">Enter Message Body</textarea> 
<br><br>
<input type="submit" value="Submit">
</form> 

PHP:

<?php
session_start();

$post1 = $_POST['post1'];
$post2 = $_POST['post2'];
$post3 = $_POST['post3'];
$post4 = $_POST['post4'];


$to      = '$post1';
$subject = '$post2';
$message = '$post3';
$headers = 'From: $post4' . "\r\n" .
    'Reply-To: $post4' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo 'done';

?> 

I can't figure out why I am not receiving an email when I run the script on my hosted server with heart internet.

Can someone please show me where I am going wrong? Thanks

Mark harris
  • 525
  • 15
  • 39
  • First thing is you need to define action method. This php code show us, you should add method='POST'. Other thing is you need check your configuration. Maybe your mail function need to edit in php.ini. – hkucuk Jul 21 '15 at 09:36

6 Answers6

0

Try this code

<?php
session_start();

$post1 = $_POST['post1'];
$post2 = $_POST['post2'];
$post3 = $_POST['post3'];
$post4 = $_POST['post4'];


$to      = $post1;
$subject = $post2;
$message = $post3;
$headers = 'From: '.$post4. "\r\n" .
    'Reply-To:'. $post4 . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo 'done';

?>

AND HTML

<form action="send_email.php" method="post" name="form">

From Email:<br>
<input type="text" name="post4">
<br><br>
To Email:<br>
<input type="text" name="post1">
<br><br>
Subject:<br>
<input type="text" name="post2">
<br><br>
Body:<br>
<textarea name="post3" form="form">Enter Message Body</textarea> 
<br><br>
<input type="submit" value="Submit">
</form> 
Dhinju Divakaran
  • 893
  • 1
  • 8
  • 11
0

The problem here is, potentially, not in the PHP.

Your form will need a method. By default, forms submit data via GET parameters in the URL, which in PHP, and in this case, would go to for example $_GET['post1']. I'd suggested adding method='post' as an attribute to your <form> as a first step, as all the $_POST variables you're referring to currently wouldn't contain anything, if that is an accurate copy/paste of your HTML form code.

d0ug7a5
  • 692
  • 4
  • 7
0

One cannot use a PHP variable inside single quotes: '$var' will return literally '$var'. However, "$var" will return a string with the value of $var. Thus you must use:

$to      = $post1; // or $to = "$post1";
$subject = $post2; // ''
$message = $post3; // ''
$headers = "From: $post4" . "\r\n" .
    "Reply-To: $post4" . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

And you can clean up your code by merging the strings "From..." and "\r\n".

sampie777
  • 134
  • 6
0

Please try this

<?php
session_start();

$post1 = $_POST['post1'];
$post2 = $_POST['post2'];
$post3 = $_POST['post3'];
$post4 = $_POST['post4'];


$to      = '$post1';
$subject = '$post2';
$message = '$post3';

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // Or use "Content-type:text/plain;charset=UTF-8" . "\r\n";

$headers.= 'From: $post4' . "\r\n" .
    'Reply-To: $post4' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo 'done';

?> 
dhi_m
  • 1,235
  • 12
  • 21
0

You first install a PEAR Package then add the below code

include('Mail.php');

$recipients = $_POST['post2'];

$headers['From']    = $_POST['1'];
$headers['To']      = $_POST['2'];
$headers['Subject'] = 'Test message';

$body = 'Test message';

$params['sendmail_path'] = '/usr/lib/sendmail';

$mail_object =& Mail::factory('sendmail', $params);

$mail_object->send($recipients, $headers, $body);

Use Google SMTP port number 465

Vishnu R Nair
  • 335
  • 2
  • 17
0

Try this (works on my end) :

<?php
session_start();

$email = htmlspecialchars($_POST['post1']);
$subject = htmlspecialchars($_POST['post2']);
$message = htmlspecialchars($_POST['post3']);
$from = htmlspecialchars($_POST['post4']);

$headers = "From: <$from> $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$ok = @mail($email, $subject, $message, $headers, "-f" . $from);   

echo 'done';
?>

AND

<form action="send_email.php" method="post" name="form">

From Email:<br>
<input type="text" name="post4">
<br><br>
To Email:<br>
<input type="text" name="post1">
<br><br>
Subject:<br>
<input type="text" name="post2">
<br><br>
Body:<br>
<textarea name="post3" form="form">Enter Message Body</textarea> 
<br><br>
<input type="submit" method="post" value="Submit">
</form> 
Mikey
  • 3
  • 2
  • this gives the following error syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home/sites/hewdenportal.co.uk/public_html/secret/send_email.php on line 4 – Mark harris Jul 21 '15 at 10:54
  • you can try the code now, it should work like this. it also provide injection safelty – Mikey Jul 21 '15 at 12:30