PHP发送邮件错误3

时间:2017-06-07 09:58:48

标签: php email sendmail

当我尝试在我的网站上发送表单时,我收到了这个错误:

  

抱歉......,似乎我的邮件服务器没有响应。请稍后再试!

我的问题是contact_me.php文件

有代码

<?php
  // Check for empty fields
  if(empty($_POST['name'])          ||
     empty($_POST['email'])         ||
     empty($_POST['phone'])         ||
     empty($_POST['message'])   ||
     !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
  {
     echo "No arguments Provided!";
     return false;
  }

  $name = strip_tags(htmlspecialchars($_POST['name']));
  $email_address = strip_tags(htmlspecialchars($_POST['email']));
  $phone = strip_tags(htmlspecialchars($_POST['phone']));
  $message = strip_tags(htmlspecialchars($_POST['message']));

  error_reporting(E_STRICT);

  date_default_timezone_set('Portugal/Lisbon');

  require_once('class.phpmailer.php');

  $mail = new PHPMailer();

  $mail->IsSMTP(); // telling the class to use SMTP
  $mail->Host       = "a.a.pt"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "a.a.pt"; // sets the SMTP server
  $mail->Port       = 25;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "a@a.pt"; // SMTP account username
  $mail->Password   = "a";        // SMTP account password

  $mail->SetFrom('a@a.pt', 'First Last');

  $mail->AddReplyTo("a@a.pt","First Last");

  $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

  $address = "a@a.pt";
  $mail->AddAddress($address, "a@a.pt");



  $formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
  $formcontent= eregi_replace("[\]",'',$formcontent);
  $mail->MsgHTML($formcontent);


  if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
  } else {
           echo "Message sent!";
         }
?>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用我的课程,我确信这样可以100%运行,即使它不起作用是凭据问题或电子邮件服务器因某种原因浪费连接

class Mailplus{
    protected  $host = "ssl://smtps.*.com";
    protected $port = "123";
    protected $username = "email@example";
    protected $password = "*";
    protected $from = "Email from - Name <email@example>";
    protected $smtp;
    function __construct() {
        require_once "Mail.php";
        $this->smtp = Mail::factory('smtp',
            array ('host' => $this->host,
                'port' => $this->port,
                'auth' => true,
                'username' => $this->username,
                'password' => $this->password));
    }

    function SendMail($to ,$subject,$body){
        $headers = array ('From' => $this->from,
            'Subject' => $subject,
            'MIME-Version' =>'1.0',
            'Content-Type' =>'text/html',
            'charset' => 'ISO-8859-1');
        $mail = $this->smtp->send($to, $headers, $body);
        if (PEAR::isError($mail)) {
            return "Ex_001";
        } else {
            return true;
        }
    }
}

然后使用此命令

$mail = new Mailplus (); 
if ($mail.SendMail("to@example.com","Subject","html emial")==ture){
   //email sent
else{
   //email not sent
}