FixingGenie
FixingGenie

Reputation: 11

Using PHPMailer to send emails to Variable addresses

I have a form I've created, and on completion they are asked to select person they want it emailed to from a drop down list.

My issue is how do I add that variable to the $mailer.

right now it is written like this

$mailer -> AddAddress('[email protected]','First Last');

how do i get my variable in there

$mailer -> AddAddress($emailAddress) - Doesn't work.

I've also tried

"'"$emailAddress"'" - this gives me - Invalid address: '[email protected]' which is frustrating since that's the format it is looking for.

Thanks, let me know

here is the full code that I am using to call the emails

$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password
$mail->AddReplyTo('[email protected]', 'First Last');
$mail->AddAddress('[email protected]', 'John Doe');
$mail->SetFrom('[email protected]', 'First Last');
$mail->AddReplyTo('[email protected]', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; //     optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif');      // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();

Upvotes: 0

Views: 9749

Answers (5)

Nika Tsilosani
Nika Tsilosani

Reputation: 111

Try strval($emailAddress), worked for me.

Upvotes: 0

dineshnagarajan
dineshnagarajan

Reputation: 11

The code given below works perfectly for me.

$mail->AddAddress($_POST['email']); // Pass the value from html form directly with the phpmailer.

Upvotes: 1

FixingGenie
FixingGenie

Reputation: 11

I got it to work, there was an issue in my values.

Actually there were a couple.

Lets just saying some spelling was incorrect.

Thanks for all the info though!

Upvotes: 0

AutoSponge
AutoSponge

Reputation: 1454

If $emailAddress came from a POST, use stripslashes around the value.

Make sure the select box has the correct value in the markup (check your view source).

As suggested, echo the variable to check it.

Upvotes: 0

Marc B
Marc B

Reputation: 360562

Try doing an

var_dump($emailAddress);

right before the ->AddAddress() call and see what comes out. If you're doing this within a function, it's possible you've not passed $emailAddress in as a parameter, of forgotten to make it global;

As well, don't surround the email address with double quotes. It's not necessary:

$emailAddress = '[email protected]';  // correct
$emailAddress = "[email protected]"; // correct
$emailAddress = '"[email protected]"'; // incorrect
$emailAddress = "\"[email protected]\""; // incorrect.

Upvotes: 0

Related Questions