Solved

How can I use a $_SESSION[EMail]  variable for the send to email

Posted on 2014-03-23
6
854 Views
Last Modified: 2014-03-23
I'm using a php script to send out an email.
on the line:" $mail->addAddress('josh@example.net');  // Add a recipient", I want to use the $_SESSION[EMail] variable value to replace "josh@example.net".
 I've tried  $mail->addAddress('$_SESSION[EMail]') and the email gets sent to the cc and bcc recipients but not the main recipient which just shows up empty in email.

Here is an example code:

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net');  // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
0
Comment
Question by:dynorich
  • 3
  • 2
6 Comments
 

Author Comment

by:dynorich
ID: 39948466
Sorry I meant to type  I've tried  $mail->addAddress('$_SESSION[EMail]') but left out the underscore in $_SESSION
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39948498
You must use $_SESSION['EMail']; with quotes and be careful with case EMail or Email?
0
 

Author Comment

by:dynorich
ID: 39948533
Thank you. I tried $mail->addAddress('$_SESSION['EMail']'); and get the error:
Parse error: syntax error, unexpected T_STRING in /home3/dynorich/public_html/extremeleverage/noc/purchase_member.php on line 221.

Also tried $mail->addAddress("$_SESSION['EMail']"); and get the error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/dynorich/public_html/extremeleverage/noc/purchase_member.php on line 221

I also tried $mail->addAddress = $_SESSION['EMail']; and the email got sent but only to the cc and bcc and not the main recipient which still shows up blank.

Here is a list of the
SESSION VARIABLES

      Array
(
    [TempID] => 172909
    [GuestID] => 899820
    [cid] => 899820
    [Firstname] => email test
    [Lastname] => Genealogy
    [EMail] => edthomas@telus.net
    [St1] => B.C.
    [Country] => U.S.A.
    [Phone] => 604 532-7919
)
 
   


Message has been sent
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39948534
Lol. Sorry, but it seems you haven't tried the right one:

 $mail->addAddress($_SESSION['EMail']);
0
 

Author Closing Comment

by:dynorich
ID: 39948541
Thank you so much!!! Works perfectly.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39948542
You're overusing the quotes.  There are two articles here at EE that can help you understand the PHP session and how quotes work in PHP syntax.

This $mail->addAddress('$_SESSION[EMail]') should almost certainly be $mail->addAddress($_SESSION['EMail'])

Sessions:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

Quotes (and Apostrophes):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html

You can check things like this by printing out the contents of a variable with var_dump().  For example, you could check the session array with var_dump($_SESSION) and you could check the PHPMailer object properties with var_dump($mail).  By comparing the outputs of the two var_dump() statements, you can verify that you're getting the right information from the session into the mailer object.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question