duanci5913 2014-12-07 04:15
浏览 93
已采纳

phpMailer仅适用于localhost

I'm new here in this forum and a little newbie on the coding php aswell.

so here is the subject, I'm using phpMailer to send an email. But it only works on localhost. I was working fine with it and when I uploaded the project to the webhost, it stopped sending properly right away. And I didn't even changed one single line.

so here it is.

<?php
if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
  echo "Nenhum argumento fornecido.";
  return false;
   }

require 'class.phpmailer.php';
require 'PHPMailerAutoload.php';

$mail = new PHPmailer();
$mail->setLanguage('pt');

$host       = 'smtp.live.com';
$username   = 'myemail@hotmail.com';
$password   = 'mypassword';
$port       = 587;
$secure     = 'tls';

$from = $username;
$fromName = 'Pedido-'.$_POST['name'];

$mail->isSMTP();
$mail->Host         = $host;
$mail->SMTPAuth     = true;
$mail->Username     = $username;
$mail->Password     = $password;
$mail->Port         = $port;
$mail->SMTPSecure   = $secure;

$mail->From     = $from;
$mail->FromName = $fromName;
$mail->addReplyTo($from, $fromName);

$mail->addAddress('toemail@gmail.com', $_POST['name']);

$mail->isHTML(true);
$mail->Charset  = 'utf-8';
$mail->WordWrap = 70;

$mail->Subject  = 'Envios do formulario';
$mail->Body     = '<b>Nome:</b> '.$_POST['name'];
$mail->Body    .= '<br><b>Responder para:</b> '.$_POST['email'];
$mail->Body    .= '<br><b>Telefone:</b> '.$_POST['phone'];
$mail->Body    .= '<br><b>Mensagem:</b> <br>'.$_POST['message'];
$mail->AltBody  = '';

$send = $mail->Send();

  return 0;

?>

It works perfecly fine when on localhost.

  • 写回答

2条回答 默认 最新

  • dongxiao3694 2014-12-07 06:23
    关注

    You didn't mention whether you checked this or not, but it's possible your host has a firewall that is blocking connections on that port. If it worked locally and stopped working when uploading it to your host, that's certainly a possibility. If they give you access to configure that, make sure outbound connections to that port are enabled. You may also want to check if the SMTP server returned a response at all. That can often tell you a lot about what happened.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀