6

我对 PHP 完全陌生,我想使用 PHP 发送邮件。我有一个联系我们表格,该表格将接受与我联系的人的电子邮件,因此邮件将发送给我。我正在使用来自https://github.com/PHPMailer/PHPMailer/tree/master的 PHPMailer 库,以下是我正在使用的代码片段。

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();  

$mail->SMTPSecure = 'tls';

$mail->Host     = "resolver1.opendns.com"; // this SMTP server of my machine
//$mail->Host     = "208.67.222.222";//ip ; which one to use the resolver1.opendns.com or 208.67.222.222 ???

$mail->From     = "xyz@gamil.com;//email id of the person 

$mail->AddAddress("datta.dhonde@coreathena.com");//my email id

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) 
{
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
}  
else 
{
  echo 'Message has been sent.';
}
?> 

我收到错误“消息未发送。邮件程序错误:SMTP 连接()失败。” 我没有得到什么问题..?$邮件->主机=“”;请评论这代表什么?

4

4 回答 4

24

添加$mail->SMTPDebug = 1;并尝试调试问题。

于 2013-10-10T06:08:49.193 回答
4

正如@joydesigner 所举的很好的例子,要通过 SMTP 连接,您需要通过hostname, username and password,然后它应该连接并发送电子邮件。

$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // tls or ssl connection as req

在这里我看到你只通过了,host信息,请添加username & password并尝试一次。

还要检查TLS/SSL PORT您的服务器是否打开:

检查:

telnet resolver1.opendns.com 25
于 2013-10-10T07:06:06.493 回答
1

可能是你的配置问题。

phpmailer 配置示例如下:

<?php
 require 'class.phpmailer.php';

 $mail = new PHPMailer;

 $mail->isSMTP();                                      // Set mailer to use SMTP
 $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
 $mail->SMTPAuth = true;                               // Enable SMTP authentication
 $mail->Username = 'jswan';                            // SMTP username
 $mail->Password = 'secret';                           // SMTP password
 $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

 $mail->From = 'from@example.com';
 $mail->FromName = 'Mailer';
 $mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
 $mail->addAddress('ellen@example.com');               // Name is optional
 $mail->addReplyTo('info@example.com', 'Information');
 $mail->addCC('cc@example.com');
 $mail->addBCC('bcc@example.com');

 $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
 $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
 $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
 $mail->isHTML(true);                                  // Set email format to HTML

 $mail->Subject = 'Here is the subject';
 $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
 $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

 if(!$mail->send()) {
  echo 'Message could not be sent.';
  echo 'Mailer Error: ' . $mail->ErrorInfo;
  exit;
 }

 echo 'Message has been sent';

这里的 $mail->Host 是 smtp 邮件服务器。通常以 smtp 开始。

于 2013-10-10T06:09:51.233 回答
0

您应该检查 resolver1.opendns.com 的 tcp 端口 25,它被阻止或未启动 stmpd,例如 sendmail 或某些 MTA。

尝试 telnet resolver1.opendns.com 25

你会发现 tcp 端口 25 没有打开。

于 2013-10-10T06:07:28.460 回答