Irfana
Irfana

Reputation: 231

Issue with $mail->Body in the PHPMailer

a beginner here.

I have already checked the forum to find out answers but I was not successful, other questions were specifically on some parts of PHPMailer but mine is more general. So I hope no one will mark my question as duplicate as I am in learning curve.

I am working on a PHP project. How it works is that the user goes to the page and writes some comments in a form (a text editor) and clicks on the send button. I am able to receive his message in my email. I have set the password and host empty for obvious reasons but later it will be my real email with my own domain.

Problem is 2 problems here:

Could you please give your suggestion or sample code that helps. Thank you.

if($_POST['mode']=='send'){

    $mail = new PHPMailer();

    $mail->IsSMTP();                                // telling the class to use SMTP
    $mail->Host     = "myhost";                     // SMTP server
                                                    //$mail->SMTPSecure = 'ssl';
    $mail->From     = "my email";
    $mail->port     = '26';                             // can also change to 465
    $mail->SMTPAuth = true; 
    $mail->Username= "my email"; 
    $mail->Password = "my password"; 

    $mail->AddAddress("my email");

    $Name = mysql_real_escape_string($_POST['part_fname']);    //this code is not getting the name and email
    $Email = mysql_real_escape_string($_POST['part_email']);

    $mail->Subject  = "New Bug Report";
    $mail->Body     = "User Information, Name = " . $Name . " ||  Email = " . $Email; // I want to get the user's message not a static message!
    $mail->WordWrap = 50;

    if(!$mail->Send())
     {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } 
    else 
    {
            $message = 'Thanks for your suggestion';
    }
 }

Here is the form in html:

<form role="form" name="Form2" action="" method="post" class="form-horizontal">
<textarea name="bug" cols="100" rows="15" id="textarea" placeholder="Enter text ..."></textarea>
<input name="mode" value="send" type="hidden">
<p style="margin-top:5px;"><input type="reset" value="Clear Text!" class="btn btn-danger">&nbsp; <button type="submit" class="btn btn-primary">Send report</button></p>
</form>

Upvotes: 0

Views: 163

Answers (2)

meda
meda

Reputation: 45490

Remove mysql_real_escape_string it requires a connection to mysql.

$Name = $_POST['part_fname'];    
$Email = $_POST['part_email'];
$message= $_POST['bug'];//add this to get the textarea value

then append the value to the body:

$mail->Body = "User Information, Name = " . $Name . " ||  Email = " . $Email;
$mail->Body .= "Message: ".$message;

If you wanted sessions

when to user logs in:

session_start();
$_SESSION['part_fname'] = $Name;
$_SESSION['part_email'] = $Email;

when sending email:

session_start();
$Name = $_SESSION['part_fname']; 
$Email = $_SESSION['part_email']; 
$message= $_POST['bug']; 

Upvotes: 1

user557846
user557846

Reputation:

to the form you need to add the fields for the name and email like so:

<input name="part_fname" value="" type="text">
<input name="part_email" value="" type="text">

then alter the code:

$Name = $_POST['part_fname'];    
$Email = $_POST['part_email'];
$message= $_POST['bug'];// textarea value

$mail->Body = "User Information, Name = " . $Name . " ||  Email = " . $Email;
$mail->Body .= "Message: ".$message;

Upvotes: 1

Related Questions