PHPMailer ile Hata işleme

8 Cevap php

Ben küçük bir proje için PHPMailer kullanmaya çalışıyorum, ama ben bu yazılım hata işleme ile ilgili biraz kafam karıştı. Umut birisi onunla deneyime sahiptir. Ben bir e-posta kurmak ettik ve zaman kullanın:

$result = $mail->Send();

if(!$result) {
    // There was an error
    // Do some error handling things here
} else {
    echo "Email successful";
}

Gayet iyi çalışıyor hangisi daha çok ya da daha az. Sorun bir hata olduğu zaman, PHPMailer da hata echo gibi görünüyor, bu yüzden bir sorun varsa, sadece esasen herhangi bir hata işleme yapmaya çalışıyorum ben "m kırarak, doğrudan tarayıcıya bu bilgi gönderir olduğunu.

Bu iletileri susturmak için bir yolu var mı? Onun bir istisna atma değil, onun sadece benim test durumda olan hatayı, dışarı baskı:

invalid address: @invalid@email You must provide at least one recipient email address.

Onun bir hata olması gerekiyordu, ama $ mail-> errorInfo ikamet olmalıdır; yazılım tarafından dışarı echo'd varlık değildir.

8 Cevap

PHPMailer İstisnaları kullanır. Aşağıdaki benimsemeye çalışın code:

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

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', '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();
  echo "Message Sent OK\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

Biz tampon yakalar ve bir istisna yazdırılan çıktıyı dönüştüren bir sarıcı sınıf yazdı. Bu bize yankı beyanlara biz yükseltme her zaman açıklama için hatırlamak zorunda kalmadan phpmailer dosyayı yükseltme sağlar.

Sarıcı sınıf yöntemleri çizgisinde bir şey vardır:

public function AddAddress($email, $name = null) {
    ob_start();
    parent::AddAddress($email, $name);
    $error = ob_get_contents();
    ob_end_clean();
    if( !empty($error) ) {
        throw new Exception($error);
    }
}

Eğer özel durumlar kullanmak bile, yine de çıkış hataları. Siz Yanlış wich $ MailerDebug ayarlamak zorunda
bu gibi görünmelidir

$mail = new PHPMailer();
$mail->MailerDebug = false;

Bu bir iyi çalışır

use try { as above

use Catch as above but comment out the echo lines
} catch (phpmailerException $e) { 
//echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {   
//echo $e->getMessage(); //Boring error messages from anything else!
}

Sonra bu eklemek

if ($e) {
//enter yor error message or redirect the user
} else {
//do something else 
}
$mail = new PHPMailer();

$mail->AddAddress($email); 
$mail->From     = $from;
$mail->Subject  = $subject; 
$mail->Body     = $body;

if($mail->Send()){
    echo 'Email Successfully Sent!';
}else{
    echo 'Email Sending Failed!';
}

E-posta başarılı ya da başarısız gönderme işlemek için basit yolu ...

Just had to fix this myself. The above answers don't seem to take into account the $mail->SMTPDebug = 0; option. It may not have been available when the question was first asked.

If you got your code from the PHPMail site, the default will be $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)

https://github.com/Synchro/PHPMailer/blob/master/examples/test_smtp_gmail_advanced.php

Hatalarını bastırmak ve yukarıda expained gibi kod 'yakalama' bölümünü düzenlemek için 0 değerini ayarlayın.

Lütfen dikkat! PHPMailer başlatmasını yaparken aşağıdaki biçimi kullanmanız gerekir!

$mail = new PHPMailer(true);

Eğer istisnalar göz ardı edilir ve yoksa alırsınız tek şey rutin bir yankı! Ben bu oluşturuldu de sonra olduğunu biliyorum ama umarım birisi yardımcı olacaktır.

PHPMailer.php olarak, çizgiler aşağıda bulunmaktadır:

echo $e->getMessage()

Sadece bu satırları açıklama ve gitmek için iyi olacak.