How can i send data from sessions using PHPMailer?

I am using dreamweaver and i have used sessions to transfer data from my first page to the 2nd page, but i want to send the data to my email address using php mailer, but im stuck because cant get how to use phpmailer to send it.

here is the code.

1.php (first page)

======

<?php session_start();?>
	  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="2.php">
  <table width="807" border="0">
    <tr>
      <td width="120">&nbsp;</td>
      <td width="120">&nbsp;</td>
      <td width="120">&nbsp;</td>
      <td width="429">&nbsp;</td>
    </tr>
    <tr>
      <td height="75">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="28">&nbsp;</td>
      <td>Name:</td>
      <td><label>
        <input name="name" type="text" id="name" />
      </label></td>
      <td><label>
        <input type="submit" name="Submit" value="Submit" />
      </label></td>
    </tr>
    <tr>
      <td height="106">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="110">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

you are not even using phpmailer anywhere.

2.php (2nd Page)

<?php session_start();

$_SESSION['sessname'] = $_POST['name'];

?>
	  
	  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="3.php">
  <table width="644" border="0">
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Message</td>
      <td><label>
        <textarea name="message" id="message"></textarea>
      </label></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><a href="3.php">
        <label>
        <input type="submit" name="Submit" value="Submit" />
        </label>
      </a></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

3.php (3rd page)

<?php session_start();

$_SESSION['sessmessage'] = $_POST['message'];

?>
      
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="sendmail.php">
  <table width="677" border="0">
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><label>
      <input name="name" type="text" id="name" />
      <?php echo $_SESSION['sessname']; ?></label></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><label>
        <textarea name="message" id="message"></textarea>
        <?php echo $_SESSION['sessmessage']; ?></label></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="Submit" value="Submit" />
      </label></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

sendmail.php (this is my mailer script)

<?php session_start(); ?>
<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$name=$_POST($_SESSION['sessname']);
$message=$_POST($_SESSION['sessmessage']);

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer();

$mail->IsSMTP();     // set mailer to use SMTP
$mail->Host = "mail.mydomainname.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "info@mydomainname.com";  // SMTP username
$mail->Password = "12345678"; // SMTP password

$mail->From = "info@mydomainname.com";
$mail->FromName = "mydomainname.com";
$mail->AddAddress("info2@mydomainname.com", "info2");

$mail->WordWrap = 50;  // set word wrap to 50 characters

$mail->Subject = "message enquiry";
$mail->Body    = "Name: $name\nMessage: $message;



if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

header( "Location: https://mydomainname.com" );
?>

i have posted all the codes, please help

Off Topic

@taylor85p: when you post code on the forums, you need to format it so it will display correctly.

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

This

$name=$_POST($_SESSION['sessname']);
$message=$_POST($_SESSION['sessmessage']);

is not correct. Do you want the $_POST variables, or the $_SESSION variables? You’ve used both in the earlier code correctly, then done it wrong in the last bit.

i don’t know where to use the $_POST or $_SESSION variable here, i only want after the visitor enter his/her email and message and submit, the message goes to my email using PHPMailer, please review and help, and send the correct code and where to place the code. thanks

Do i also need the 3.php (3rd page)? or is it just the first and second page i need? because i would want the email to be sent after the visitor enters the message. hope you understand me?

Well, you could start by using the same syntax you’ve used in the other bits of code, rather than combining them in this way. Look at how you retrieved the session variables to display them in the form in 3.php, and compare that to how you’ve tried to retrieve them in sendmail.php and see the difference.

That’s up to you. I must admit I did wonder why you ask for one thing in the first page, another thing on the second page, then ask for them both again on the third page, and then send the message. Why don’t you just put all the form variables in a single form on a single page, and send that directly to sendmail.php?

No, sorry - I’m happy to try to help, but I’m not just going to do it for you.

2 Likes

I’m learning how to use sessions in PHP

i replaced with
$name=$_SESSION[‘sessname’];
$message=$_SESSION[‘sessmessage’];

its still not working
Message could not be sent.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

OK, but you’ve made progress - it may well still not be working, but it’s failing differently now. Presumably you’ve checked that the form variables are coming through correctly and that the message is correctly formed, so you know that the forms and your session variable handling is working, so you can forget about that bit unless you decide to streamline it into a single form. Only thing is, doing that won’t help you learn about session variables.

All you have to do now is fix whatever is wrong with your PHPMailer setup, which I can’t help with because I’ve never used it.

2 Likes

I suggest you add

$mail->SMTPDebug = 3;

before

$mail->IsSMTP();

so you can debug what is going on with PHPMailer.

1 Like

You’re missing SMTPSecure and the port. You need to figure out what the SMTP server offers as the outgoing mails as port and what kind of encryption they offer. It’s either tls or ssl. But if the SMTP server doesn’t offer ssl, you won’t be able to use it even if you want to so your next choice would be tls.

1 Like

I have included ssl and still not working, the phpmailer configuration is very ok, the problem is this

$name=$_SESSION([‘sessname’]);
$message=$_SESSION([‘sessmessage’]);

PHPMailer says Fatal error: Function name must be a string in /home/t3w00j6/public_html/sendmail.php on line 5

and line 5 is
$name=$_SESSION([‘sessname’]);
$message=$_SESSION([‘sessmessage’]);

That’s because you are writing it wrong. You’re wrapping those variables around parentheses which means that they are being called as a function hence your error message. It’s saying that functions have to have string names. You can’t use PHP variables as a function name.

If you are trying to assign and create those variables, you should always do it left to right and not right to left because it will confuse you even more when you try to write code. What I am basically saying is, you should make the assignments on the left side and whatever you are appending to the assigned variables on the right. Most people write code like this and not the way you are doing it.

just fix it up for me, because i am a bit confuse here

I am not going to do your work for you. If you want me to do it, hire me. Otherwise, take the advice and fix the part I am saying is wrong.

1 Like