2010-09-28 160 views
-1

我有一個我創建的表單,完成後他們被要求從下拉列表中選擇他們希望通過電子郵件發送的人。使用PHPMailer發送電子郵件到變量地址

我的問題是如何將該變量添加到$郵件程序。

現在它是這樣寫的

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

如何讓我的變量有

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

我也試過

"'"$emailAddress"'" - 這給了我 - 無效地址:'[email protected]'這是令人沮喪的,因爲這是它正在尋找的格式。

謝謝你,讓我知道

這裏是我使用調用郵件

$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 = "[email protected]"; // 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(); 
+0

$ mailer-> AddAddress($ emailAddress);應該工作,如果$ emailAddress =「[email protected]」;你試過在$ mailer-> AddAddress($ emailAddress)附近的代碼中回顯$ emailAddess; ?你能告訴我們什麼「不起作用」? – Shikiryu 2010-09-28 13:20:47

+0

我做了回聲,然後頁面崩潰。我也試着在html下拉單引號中使用一個變量,然後調用$ mailer中的變量,但也沒有奏效。 我每次使用除特定('[email protected]')以外的任何地址時都會收到無效地址。 – FixingGenie 2010-09-28 13:41:05

+0

我也嘗試過使用ASCII碼來調用同樣在值中產生\的單引號。 ' 我還沒有試過‘ ’還沒有,但我現在就試試。 – FixingGenie 2010-09-28 13:49:17

回答

0

的完整代碼嘗試->AddAddress()呼叫前右做一個

var_dump($emailAddress); 

看看什麼出來。如果你在一個函數中這樣做,有可能你沒有通過$ emailAddress作爲參數,忘記使它成爲global;

同樣,不要用雙引號括住電子郵件地址。這不是必需的:

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

var_dump($ emailAddress) - 「[email protected]」 然後顯示地址無效,我發現它需要單引號來實際接受電子郵件有效。 這是我的問題,因爲如果我使用單引號,我不能再使用該變量。 – FixingGenie 2010-09-28 13:36:38

+0

什麼是確切的錯誤信息?只有兩個地方可以從phpmailer輸出「無效地址」,一個私有方法('AddAnAddress()')和一個公共方法'SetFrom()' – 2010-09-28 14:25:55

0

如果$ emailAddress來自POST,則使用該值的反斜槓。

確保選擇框在標記中具有正確的值(檢查您的查看源)。

如上所示,回顯變量以檢查它。

+0

我將POST轉換成全局會話,然後把它成陣列。 從那裏我將會話調用成一個變量,然後回顯變量。 當我得到回聲,它回來很好。 當我嘗試用斜槓轉義時,我在實際變量中得到了斜槓。 \'[email protected] \' – FixingGenie 2010-09-28 13:45:43

+0

我有我自己的郵件類。但是,它們基本上都做同樣的事情:構建頭文件,清理一些變量,並調用mail()。您可以編輯$ mail類以回顯收件人(郵件()的第一個參數)。 – AutoSponge 2010-09-28 13:57:41

0

我得到它的工作,有一個問題在我的價值觀。

其實有一對夫婦。

讓我們說一些拼寫錯誤。

儘管感謝所有的信息!

0

下面給出的代碼適用於我。

$mail->AddAddress($_POST['email']); //將值從html表單直接傳遞給phpmailer。

0

嘗試strval($emailAddress),爲我工作。

相關問題