CodeIgniter项目本地可发邮件,线上服务器无法发送的问题
问题描述
本地环境下CodeIgniter项目的邮件发送功能正常,但部署到线上服务器后无法发送邮件,报错信息如下:
severity : Warning
Message: fsockopen(): Unable to connect to ssl://smtp.gmail.com:465 (Connection refused)
Filename: libraries/Email.php
控制器代码
public function contactload(){ // to load the register.php // here the data is stored to database $this->load->model('User_model'); //storing the data into the array $formArray =array(); $formArray['name']= $this->input->post('name'); $formArray['email']= $this->input->post('email'); $formArray['country']= $this->input->post('country'); $formArray['subject']= $this->input->post('subject'); // calling the create function in user_model $check=$this->User_model->create($formArray); if($check==true){ if ($this->User_model->sendEmail($this->input->post('email'), $this->input->post('name'))) { // successfully sent mail echo '<script>alert("Mail Is sent kindly check the Mail")</script>'; } else { // error echo '<script>alert("UNABLE TO SEND THE MAIL")</script>'; } }else{ echo "error !"; } }
模型代码
public function sendEmail($to_email,$name) { $from_email = '*********@gmail.com'; //change this to yours $subject = 'Your meaasge Has Sucessfully reached Us'; $message = ' <html> <head> <title>Conformation Mail</title> </head> <body>'; $this->load->library('email'); $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.gmail.com', 'smtp_port' => '465', 'smtp_user' => '***************@gmail.com', 'smtp_pass' => '****************', 'mailtype' => 'html', 'smtp_timeout' => '60', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE, 'newline' => "\r\n" ); $this->load->library('email',$config); $this->email->initialize($config); //send mail $this->email->from($from_email); $this->email->to($to_email); $this->email->subject($subject); $this->email->message($message); return $this->email->send(); echo $this->email->print_debugger(); }
解决方案
1. 检查线上服务器端口限制
大部分云服务器默认封禁465端口,需在服务器安全组/防火墙中开启465端口的出站权限:
- 登录服务器管理控制台,进入安全组配置页面
- 添加出站规则:允许TCP协议,端口465,目标地址设为
smtp.gmail.com或0.0.0.0/0
2. 调整Gmail账号安全设置
- 启用应用密码(推荐):若账号开启了2步验证,必须使用应用密码替代登录密码。操作路径:Google账号 → 安全 → 应用密码 → 生成对应设备的应用密码,替换代码中的
smtp_pass - 临时开启低安全应用访问:若未开启2步验证,可在Google账号设置中开启"允许不安全的应用访问"(注意:此方法存在安全风险,仅作为临时调试使用)
3. 切换为TLS协议端口
将SMTP配置改为使用TLS协议的587端口,很多服务器对该端口限制更宽松:
$config = Array( 'protocol' => 'smtp', 'smtp_host' => 'tls://smtp.gmail.com', 'smtp_port' => '587', // 其余配置保持不变 );
4. 确认PHP扩展状态
确保线上服务器的PHP已开启openssl扩展(SSL/TLS连接依赖此扩展):
- 访问
phpinfo()页面,搜索"openssl"确认状态为已启用 - 若未开启,修改
php.ini文件,取消extension=openssl的注释,重启PHP服务
5. 修复代码潜在问题
- 补全邮件内容标签:模型中
$message变量未闭合HTML标签,需补充</body></html>,避免邮件内容异常 - 调整调试代码位置:
return $this->email->send();之后的echo $this->email->print_debugger();永远不会执行,建议移至return之前,方便调试:$debug = $this->email->print_debugger(); $result = $this->email->send(); echo $debug; // 上线前可改为日志记录或注释 return $result;
6. 测试服务器连通性
在服务器终端执行以下命令,测试与Gmail SMTP服务器的连通性:
# 测试465端口 telnet smtp.gmail.com 465 # 测试587端口 telnet smtp.gmail.com 587
若连接失败,说明服务器网络存在限制,需联系服务商解除;若连接成功,再排查代码或配置细节。
内容的提问来源于stack exchange,提问作者Samyama
相关产品推荐
相关产品推荐

