dynorich
asked on
How can I use a $_SESSION[EMail] variable for the send to email
I'm using a php script to send out an email.
on the line:" $mail->addAddress('josh@ex ample.net' ); // Add a recipient", I want to use the $_SESSION[EMail] variable value to replace "josh@example.net".
I've tried $mail->addAddress('$_SESSI ON[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.e xample.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@ex ample.net' ); // Add a recipient
$mail->addAddress('ellen@e xample.com '); // Name is optional
$mail->addReplyTo('info@ex ample.com' , 'Information');
$mail->addCC('cc@example.c om');
$mail->addBCC('bcc@example .com');
on the line:" $mail->addAddress('josh@ex
I've tried $mail->addAddress('$_SESSI
Here is an example code:
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.e
$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@ex
$mail->addAddress('ellen@e
$mail->addReplyTo('info@ex
$mail->addCC('cc@example.c
$mail->addBCC('bcc@example
You must use $_SESSION['EMail']; with quotes and be careful with case EMail or Email?
ASKER
Thank you. I tried $mail->addAddress('$_SESSI ON['EMail' ]'); and get the error:
Parse error: syntax error, unexpected T_STRING in /home3/dynorich/public_htm l/extremel everage/no c/purchase _member.ph p on line 221.
Also tried $mail->addAddress("$_SESSI ON['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_htm l/extremel everage/no c/purchase _member.ph p 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
Parse error: syntax error, unexpected T_STRING in /home3/dynorich/public_htm
Also tried $mail->addAddress("$_SESSI
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much!!! Works perfectly.
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('$_SESSI ON[EMail]' ) should almost certainly be $mail->addAddress($_SESSIO N['EMail'] )
Sessions:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
Quotes (and Apostrophes):
https://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.
This $mail->addAddress('$_SESSI
Sessions:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
Quotes (and Apostrophes):
https://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.
ASKER