dongxiji0687 2014-10-04 18:53
浏览 75
已采纳

SMTP连接超时PHP电子邮件

Please read before marking this as a duplicate. I have tried everything!

I have been trying to use SwiftMailer and PHPMailer, both which I have use before, to send emails from my website form. For some reason, for the last few days, I haven't gotten them to work. Each time, no matter the settings, I get error 110 - Connection timeout to the smtp server. I have tried using authentication from my gmail account and 1and1.com email account. I have even added a check using fsockopen and gmail, godaddy nor 1and1's smtp server can be connected to. And I've even called 1and1 twice to ask about firewalls and anything that would prevent my emails and they've said they don't know what the problem could be. I have tried everything listed on multiple Stackoverflow questions and cannot fix this. Is there anything going on that would prevent this? My internet connection (tried home and work) maybe? I'm very lost! My code for PHPMailer and SwiftMailer will be pasted below. Please remember that I have tried using port 465/ssl and port 587/tls and neither has worked.

require 'PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'inquiry@example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'inquiry@example.com';
$mail->addAddress('example@gmail.com');
$mail->isHTML(true);

$checkconn = fsockopen($mail->Host, $mail->Port, $errno, $errstr, 5);

if($_GET['formName'] == 'join')
{
    $text = 'Name: <b>' . $_POST['first_name_join']. " " .$_POST['last_name_join']. "</b>".
             "<br>Phone: <b>".$_POST['phone_join']. "</b>".
             "<br>Email: <b>".$_POST['email_join']. "</b>".
             "<br>Date of Birth: <b>".$_POST['birth_month']. "/" .$_POST['birth_day']. "/" .$_POST['birth_day']. "</b>".
             "<br>Message: <br><b>".$_POST['message_join']. "</b>";
}
else
{
    $text = 'Name: <b>' . $_POST['full_name_sponsor']. "</b>".
             "<br>Company/Organization: <b>" . $_POST['company_sponsor']. "</b>".
             "<br>Phone: <b>".$_POST['phone_sponsor']. "</b>".
             "<br>Email: <b>".$_POST['email_sponsor']. "</b>".
             "<br>Message: <br><b>".$_POST['message_sponsor']. "</b>";
}



$mail->Subject = $_GET['formName'] == 'join' ? 'Join Inquiry' : 'Sponsorship Inquiry' ;
$mail->Body    = $text;

if (!$checkconn)
{
    echo "($errno) $errstr

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

and

require_once 'swiftmailer/swift_required.php';


// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 587)
    ->setUsername('inquiry@example.com')
    ->setPassword('password');


// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

if($_GET['formName'] == 'join')
{
    $text = 'Name: <b>' . $_POST['first_name_join']. " " .$_POST['last_name_join']. "</b>".
             "<br>Phone: <b>".$_POST['phone_join']. "</b>".
             "<br>Email: <b>".$_POST['email_join']. "</b>".
             "<br>Date of Birth: <b>".$_POST['birth_month']. "/" .$_POST['birth_day']. "/" .$_POST['birth_day']. "</b>".
             "<br>Message: <br><b>".$_POST['message_join']. "</b>";
}
else
{
    $text = 'Name: <b>' . $_POST['full_name_sponsor']. "</b>".
             "<br>Company/Organization: <b>" . $_POST['company_sponsor']. "</b>".
             "<br>Phone: <b>".$_POST['phone_sponsor']. "</b>".
             "<br>Email: <b>".$_POST['email_sponsor']. "</b>".
             "<br>Message: <br><b>".$_POST['message_sponsor']. "</b>";
}

// Create a message
$subject = $_GET['formName'] == 'join' ? 'Join Inquiry' : 'Sponsorship Inquiry' ;
$message = Swift_Message::newInstance($subject)
  ->setFrom(array('inquiry@example.com'))
  ->setTo(array('example@gmail.com')) 
  ->setBody($text, 'text/html');

// Send the message
$result = $mailer->send($message);
  • 写回答

1条回答 默认 最新

  • duannaiying9662 2014-10-04 19:16
    关注

    Well it might a case where the ISP is Blocking the SMTP. And I know I am not giving a direct solution. It won't hurt to try other services to send your mail out thru API, thereby eliminating SMTP altogether.
    I have good experiences with Mailgun and Mandrill . They both have extension documentation and PHP SDK for API and can also be used thru SMTP.
    Best Wishes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记