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

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题