想调用发邮件,结果报错:Swift_TransportException Connection could not be established with host smtp.qq.com [php_network_getaddresses: getaddrinfo failed: Name or service not known #0]

问题咨询 · an94er · 于 5年前 发布 · 8971 次阅读

事情起因

我想把购物车列表的按钮,改成不去填写地址,而是直接发一封邮件,

然后我在@app/appfront/theme/base/front/checkout/cart/index.php 里面加调用发邮件的指令:Yii::$service->email->order->sendCreateEmail($data);

email的相关配置修改

(暂时不写相对路径,写绝对路径先吧,抱歉,绝对路径根目录是启动docker的目录) app/fecshop/vendor/fancyecommerce/fecshop/services/Email.php 这里改了qq号及相关账号 app/fecshop/common/config/fecshop_local_services/Email.php这里改了qq号相关,还是smtp.qq.com

报错信息

加发邮件之后文字报错如下:

Swift_TransportException Connection could not be established with host smtp.qq.com [php_network_getaddresses: getaddrinfo failed: Name or service not known #0]

图片报错,也就是报错详情

报错详情

错误分析

搜索网上结果

https://blog.csdn.net/ownfire/article/details/7850890 说是host问题 https://bugs.php.net/bug.php?id=11058 说是php的bug 都说是host问题,但是我改了没用

本论坛搜索

http://www.fecshop.com/topic/784 http://www.fecshop.com/topic/765 这些跟我不一样啊,所以目前不知怎么下手了

自行猜测

是否要自己搭建smtp服务器

求助版主

这个发邮件的问题

还有,我在里面想请求外面搭建在本机的服务器,也是连接不上,报错是一样的,麻烦指点下,@terry,谢谢

共收到 7 条回复
Fecmall#15年前 0 个赞

smtp失效了,自己找个smtp填写上去,QQ邮箱就有

an94er#25年前 0 个赞

你说的smtp失效,指的是:app/fecshop/common/config/fecshop_local_services/Email.php这里的配置吧,我取qq邮箱哪里开启了吧,配置如下图: 改了没用,报错是一样的

an94er#35年前 0 个赞

还有,我自己写的发邮件方式也是报这个错,用的163发,在我本机可以发,但是上服务器就会报这个标题一样的错,感觉应该是某个配置有问题,附上自定义代码:

<?php

class Mailer
{
  private $host;
  private $port = 25;
  private $user;
  private $pass;
  private $debug = false;
  private $sock;
 
  public function __construct($host,$port,$user,$pass,$debug = false)
  {
    $this->host = $host;
    $this->port = $port;
    $this->user = base64_encode($user); //用户名密码一定要使用base64编码才行
    $this->pass = base64_encode($pass);
    $this->debug = $debug;
  //socket连接
    $this->sock = fsockopen($this->host,$this->port);
    if(!$this->sock){
      exit('出错啦');
    }
  //读取smtp服务返回给我们的数据
    $response = fgets($this->sock);
    $this->debug($response);
        //如果响应中有220返回码,说明我们连接成功了
    if(strstr($response,'220') === false){
      exit('出错啦');
    }
  }
//发送SMTP指令,不同指令的返回码可能不同
  public function execCommand($cmd,$return_code){
    fwrite($this->sock,$cmd);
 
    $response = fgets($this->sock);
//输出调试信息
    $this->debug('cmd:'.$cmd .';response:'.$response);
    if(strstr($response,$return_code) === false){
      return false;
    }
    return true;
  }
 
  public function sendMail($from,$to,$subject,$body){
//detail是邮件的内容,一定要严格按照下面的格式,这是协议规定的
    $detail = 'From:'.$from."\r\n";
    $detail .= 'To:'.$to."\r\n";
    $detail .= 'Subject:'.$subject."\r\n";
    $detail .= 'Content-Type: Text/html;'."\r\n";
    $detail .= 'charset=gb2312'."\r\n\r\n";
    $detail .= $body;
    $this->execCommand("HELO ".$this->host."\r\n",250);
    $this->execCommand("AUTH LOGIN\r\n",334);
    $this->execCommand($this->user."\r\n",334);
    $this->execCommand($this->pass."\r\n",235);
    $this->execCommand("MAIL FROM:<".$from.">\r\n",250);
    $this->execCommand("RCPT TO:<".$to.">\r\n",250);
    $this->execCommand("DATA\r\n",354);
    $this->execCommand($detail."\r\n.\r\n",250);
    $this->execCommand("QUIT\r\n",221);
  }
 
  public function debug($message){
    if($this->debug){
      echo '<p>Debug:'.$message . PHP_EOL .'</p>';
    }
  }
 
  public function __destruct()
  {
    fclose($this->sock);
  }
 
}



$port = 25;
$user = ''; //请替换成你自己的smtp用户名
$pass = ''; //请替换成你自己的smtp密码
$host = 'smtp.163.com';
$from = 'an94er@163.com'; 
$to = 'an94er@live.com';

$body = $_GET["body"];;
$subjet = '购买下单';
$mailer = new Mailer($host,$port,$user,$pass,true);
$mailer->sendMail($from,$to,$subjet,$body);


?>

只需要改下 $user = ''; //请替换成你自己的smtp用户名 $pass = ''; //请替换成你自己的smtp密码 就可以直接用了的,本机可以,服务器不行

Maybe#45年前 0 个赞

it was smtp config error,you must config it by your email smtp info

Fecmall#55年前 0 个赞

楼上说得对,就是email smtp 信息问题,fecshop默认是用的是我的qq email 配置,很多人都用我的smtp配置,我给禁止掉了

jerou#64年前 1 个赞

借帖说一下踩的坑,使用163邮箱25端口tls的时候,本地可以发送而服务器不能发送。之后改为587、ssl就完全没问题了额。

cococup#74年前 1 个赞

用QQ的465发送不了,改为587就可以。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics