HOW TO:使用PHPMailer类或PHPMailerAutoload发送邮件?

时间:2016-06-16 01:02:57

标签: php phpmailer

首先:我正在发送一封没有附件的简单电子邮件与PHPMailer类

第二:我的代码没有使用 要求( '../库/ PHPMailer的/ class.phpmailer.php'); //这里给我一个致命的错误

enter image description here

第三:我已经尝试过PHPMailerAutoload类

       require('../ libs / PHPMailer / PHPMailerAutoload.php'); //此处它不会抛出任何错误,但不会向我发送任何内容

<?php
require('../libs/PHPMailer/class.phpmailer.php');

echo $_POST['InputName'];
echo $_POST['InputEmail'];
echo $_POST['InputMessage'];

if(!isset($_POST['InputName']) || 
    !isset($_POST['InputEmail']) || 
    !isset($_POST['InputMessage'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$inputName = $_POST['InputName'];  
$inputEmail = $_POST['InputEmail'];  
$inputMessage = $_POST['InputMessage']; 
$error_message = "";

$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';

$mail->IsSMTP();
$mail->Host       = 'mail.domain.com.mx';

$mail->SMTPSecure = 'tls';
$mail->Port       = 465;
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;

$mail->Username   = 'contact@domain.com.mx';
$mail->Password   = 'mypassword';

$mail->SetFrom($inputEmail, 'something here');
// $mail->AddReplyTo('no-reply@mycomp.com','no-reply');
$mail->Subject    = 'CONTACT '.$inputEmail;
$mail->MsgHTML($inputMessage);

$mail->AddAddress('contact@domain.com.mx', 'contact');
$mail->AddAddress('other@domain.com.mx', 'other'); /* ... */

// $mail->AddAttachment($fileName);
$mail->send(); 

&GT;

1 个答案:

答案 0 :(得分:1)

您的代码甚至不会尝试检测故障。您不启用例外:

$mail=new PHPMailer();

...而且您没有验证结果:

$mail->send();

我建议您启用例外:

$mail=new PHPMailer(true);

...或者至少验证服务器是否接受了消息:

if (!$mail->send()) {
   // Log to file and warn user
}

此外,PHPMailer是您应该尝试的几个调试功能。请检查SMTPDebugDebugoutput属性。