2014-02-10 65 views
0

我試圖發送電子郵件給用戶,並使用PHPMailer的,但只有一個電子郵件的同時,以管理員發送多個電子郵件發送..要麼用戶或管理員..來自同一頁面的PHPMailer

我曾嘗試沒有運氣的每一種可能的方式。請幫助

下面是我的代碼。

$html.="This is my message to user"; 

$bcc = "[email protected]"; 
$to= $clientemail; 
$subject = "This is my subject; 

$host = $SmtpServer; 
$username = $SmtpUser; 
$password = $SmtpPass; 

$mail = new PHPMailer(); 


$mail->IsSMTP(); 
$mail->Host = "$host"; // specify main and backup server 
$mail->SMTPAuth = true;  // turn on SMTP authentication 
$mail->Username = $username; // SMTP username 
$mail->Password = $password; // SMTP password 
$mail->Port = 587;   
$mail->SMTPSecure = 'tls';  
try { 
$mail->From = $from; 
$mail->FromName = $Name; 
$mail->AddAddress(''.$to.''); 
$mail->AddBCC('[email protected]'); 
$mail->WordWrap = 50; 
$mail->IsHTML(true); 
$mail->Subject = $subject; 
$mail->Body = $html; 
$mail->AltBody = $html; 
$mail->Send(); 
    // echo "Message Sent OK<p></p>\n"; 
    } catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
    } catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
    } 

$message="Thank you for your request."; 

$html="this is my second message to admin"; 

$bcc = "[email protected]"; 
$to= $admin; 
$subject = "message to admin; 

$host = $SmtpServer; 
$username = $SmtpUser; 
$password = $SmtpPass; 

$mail2 = new PHPMailer(); 


$mail2->IsSMTP(); 
$mail2->Host = "$host"; // specify main and backup server 
$mail2->SMTPAuth = true;  // turn on SMTP authentication 
$mail2->Username = $username; // SMTP username 
$mail2->Password = $password; // SMTP password 
$mail2->Port = 587;   
$mail2->SMTPSecure = 'tls';  
try { 
$mail2->From = $from; 
$mail2->FromName = $from; 
$mail2->AddAddress(''.$to.''); 
$mail2->AddBCC('[email protected]'); 
$mail2->WordWrap = 50; 
$mail2->IsHTML(true); 
$mail2->Subject = $subject; 
$mail2->Body = $html; 
$mail2->AltBody = $html; 
$mail2->Send(); 
    // echo "Message Sent OK<p></p>\n"; 
    } catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
    } catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
    } 

回答

0

我不知道,如果你想發送相同的電子郵件或不型動物的人,但第一個改進要做的就是因式分解你的代碼。

// here an example of a function to send the SAME email to differents persons 
// $emails is an array 
function sendMail($emails, $subjects, $msg) { 
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 

    try { 
     $mail->Host = "$host"; // specify main and backup server 
     $mail->SMTPAuth = true;  // turn on SMTP authentication 
     $mail->Username = $username; // SMTP username 
     $mail->Password = $password; // SMTP password 
     $mail->Port = 587;   
     $mail->SMTPSecure = 'tls';  

     $mail->From = $from; 
     $mail->FromName = $Name; 

     foreach ($emails as $to) { 
      $mail->AddAddress($to); 
     } 

     $mail->AddBCC('[email protected]'); 
     $mail->WordWrap = 50; 
     $mail->IsHTML(true); 
     $mail->Subject = $subject; 
     $mail->Body = $html; 
     $mail->AltBody = $html; 

     if(!$mail->Send()) { 
      // log if needed 
     } 
    } catch (phpmailerException $e) { 
      echo $e->errorMessage(); 
    } 
} 

現在,您可以使用此功能與不同的消息或主題......任何你想要的!

編輯:

發送不同的信息給用戶和管理員:

//settings for admin 
$emails = array('[email protected]', '[email protected]'); // or just array('[email protected]'); 
$subjects = 'my sub'; 
$msg = 'my msg'; 

sendMail($emails, $subjects, $msg); 

//settings for users 
$emails = array('[email protected]'); 
$subjects = 'different or not sub'; 
$msg = 'different or not msg'; 

sendMail($emails, $subjects, $msg); 
+0

謝謝您的答覆..但是這並沒有解決我的問題。 – user3292533

+0

我想發送多封電子郵件 - 一個與不同的內容管理和與別不同的內容的用戶.......現在它只能發送一個電子郵件傳送至用戶或管理員 – user3292533

+0

很好,看到我編輯發送不同的郵件...也許這將解決您的問題。 –

相關問題