2016-10-03 45 views
-3

我有一個正常工作的php代碼。我想發郵件給管理員說user - A已提出請求。當我查看電子郵件時,它顯示user -。它不顯示用戶名。PHP - 發送電子郵件中的用戶名

PHP代碼:

<?php 
    require 'PHPMailerAutoload.php'; 
    $username = $_SESSION ["username"]; 

function leave_mail($user, $message){ 

    //Some connection and credentials for the gmail 

    $username = $_SESSION ["username"];  
    $mail = new PHPMailer; 

    leave_mail($username, "Your message has been sent to the <b>admin.</b>"); 
    leave_mail('[email protected]', 'This message is to notify that you have a <b>new leave request</b> from <i><b>USER - <?php echo $username;?></b></i>');  
?> 

請幫幫忙!

+0

嘿它不是一個有效的代碼! – Turtle

+0

我不明白!? –

+0

您已經在PHP中,連接在一起。 – chris85

回答

-2
leave_mail('[email protected]', 'This message is to notify that you have a <b>new leave request</b> from <i><b>USER - ' . $username . '</b></i>'); 

那是因爲你正在寫的東西已經是php代碼。所以'xxx' . $username . 'yyy'是一個string concat在PHP中。

何時使用<?php xxx; ?>?當你正在編寫一個.php文件時,你正在默認編寫html代碼。所以,當你想寫的PHP,使用它。

例子:

<html> 
<body> 
<div>hello i am html</div> 
<?php for($i=0;$i<5;++$i)echo 'Hey this is php code'; ?> 
</body> 
</html> 
+1

解釋會有所幫助.. – chris85

+0

@ chris85添加... – Turtle

+0

謝謝!有效。 –