【问题标题】:PHPMailer not connecting HTML/PHPPHPMailer 未连接 HTML/PHP
【发布时间】:2017-06-15 10:50:42
【问题描述】:

我正在尝试让我的 HTML 联系表单与 PHPMailer 一起使用,但是当我尝试发送消息时,我无法连接到服务器:

2017-06-15 10:39:15 SMTP 错误:无法连接到服务器:(0) 2017-06-15 10:39:15 SMTP connect() 失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我正在尝试连接到 SiteGround SMTP 服务器。

表格代码:

<form id="contact" action="mailer.php" method="post">
    <div class="left">
        <input type="text" placeholder="Name" required="required" name="name"/>
        <input type="email" placeholder="Email" required="required" name="email"/>
        <input type="text" placeholder="Subject" required="required" name="subject"/>
    </div>
    <div class="right">
        <textarea placeholder="Message" required="required" name="message"></textarea>
    </div>
    <div class="send-button cl">
        <button type="submit" name="submit">Send</button>
    </div>
</form>

PHP 代码:

//Creating the message
$message =
'Name:  '.$_POST['name'].'<br />
Subject:    '.$_POST['subject'].'<br />
Email:  '.$_POST['email'].'<br />
Message:    '.$_POST['message'].'
';

require 'phpmailer/PHPMailerAutoload.php';

// Instantiate Class
$mail = new PHPMailer();

// Set up SMTP
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "mail.frankkreutzer.com";
$mail->Port = 465; // 587 | 465
// $mail->IsHTML(true);

// Authentication
$mail->Username = "email@domain.com";
$mail->Password = "password";

// Compose
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = $_POST['subject'];
$mail->MsgHTML($message);

// Send to
$mail->AddAddress("recipient@domain.com"); // Where to send it

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}

【问题讨论】:

  • $mail->From="sender id"//missing ?
  • 所以你的意思是添加那行?

标签: php html forms email


【解决方案1】:

刚刚修复了 PHP 代码:

<?php
require 'phpmailer/PHPMailerAutoload.php';

//Creating the message
$message =
'Name:  '.$_POST['name'].'<br />
Email:  '.$_POST['email'].'<br /><br />
Subject:    '.$_POST['subject'].'<br />
Message:    '.$_POST['message'].'
';

// Instantiate Class
$mail = new PHPMailer();

// Set up SMTP
$mail->IsSMTP(); // enable SMTP
// $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages 
only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // 587 | 465
// $mail->IsHTML(true);

// Authentication
$mail->Username = "example@email.com";
$mail->Password = "password";

// Compose
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->MsgHTML($message);

// Send to
$mail->AddAddress("example@email.com"); // Where to send it

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent. I'll get back to you as soon as possible!";
}
?>

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多