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 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多