2013-10-30 101 views
1

PHP的新手在這裏...PHPMailer如果其他人根據用戶的下拉選擇

試圖把一個if/elseif語句放入我的php代碼的表單電子郵件。

如果用戶選擇「桑迪」,那麼電子郵件應該去桑迪的電子郵件地址。如果用戶選擇「史蒂夫」發送到史蒂夫的電子郵件地址,等等。這是我到目前爲止的代碼。任何意見表示讚賞。

<?php 

require_once('phpmailer/class.phpmailer.php'); 

$mail = new PHPMailer(); 

if(isset($_POST['template-contactform-submit']) AND $_POST['template-contactform-submit'] == 'submit') { 

if($_POST['template-contactform-name'] != '' AND $_POST['template-contactform-email'] != '' AND $_POST['template-contactform-subject'] != '' AND $_POST['template-contactform-message'] != '') { 

    $name = $_POST['template-contactform-name']; 

    $email = $_POST['template-contactform-email']; 

    $subject = $_POST['template-contactform-subject']; 

    $message = $_POST['template-contactform-message']; 

    $botcheck = $_POST['template-contactform-botcheck']; 

    if($_POST['template-contactform-employee'] = 'Sandy'): 

     { 

     $toemail = '[email protected]'; 

     $toname = 'Sandy'; 

     } 


    elseif($_POST['template-contactform-employee'] = 'Steve'): 

     { 

     $toemail = '[email protected]'; 

     $toname = 'Steve'; 

     } 

    elseif($_POST['template-contactform-employee'] = 'Nick'): 

     { 

     $toemail = '[email protected]'; 

     $toname = 'Nick'; 

     } 

    else: { 

     $toemail = "[email protected]"; 

     $toname = "Support"; 

     } 

    endif; 

    $body = "$message"; 

    if($botcheck == '') { 

     $mail->SetFrom($email , $name); 

     $mail->AddReplyTo($email , $name); 

     $mail->AddAddress($toemail , $toname); 

     $mail->Subject = $subject; 

     $mail->MsgHTML($body); 

     $sendEmail = $mail->Send(); 

     if($sendEmail == true): 

      echo '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button>We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.</div>'; 

     else: 

      echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '</div>'; 

     endif; 

    } else { 

     echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>Bot <strong>Detected</strong>.! Clean yourself Botster.!</div>'; 

    } 

} else { 

    echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>Please <strong>Fill up</strong> all the Fields and Try Again.</div>'; 

} 

} else { 

echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>An <strong>unexpected error</strong> occured. Please Try Again later.</div>'; 

} 

?> 
+0

你的代碼有問題嗎?它不起作用嗎?如果是這樣,你會得到什麼錯誤? – andrewsi

+0

頁面提供了一條成功消息,表明郵件已發送,但電子郵件永遠不會到達收件箱。沒有錯誤信息。 –

+0

如果你拿出if/else語句,並在那裏硬連線你的細節,那麼怎麼辦?它在那種情況下工作嗎? – andrewsi

回答

1

此解決方案來自@andrewsi工作。感謝您發現我的錯誤!

if($_POST['template-contactform-employee'] = 'Sandy')是錯誤的 - 您正在使用=進行平等檢查,而不是==。嘗試if($_POST['template-contactform-employee'] == 'Sandy'),看看是否有不同(你需要修改每個if行)。

相關問題