以下是我从网上发送电子邮件后从localhost发送电子邮件的代码。
html表格:
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
email.php:
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require_once('class.phpmailer.php');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';
$mail = new PHPMailer();
$body='hellooooo';
$mail->IsSMTP();
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxxx@gmail.com"; // SMTP username
$mail->Password = "zzz"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("xxx", "xx");
$mail->SetFrom('xxx@gmail.com','xxxx');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
所以当我运行我的代码时,它会显示错误,
无法发送消息。
邮件程序错误:SMTP连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
我从以下文件中删除了此行;extension=php_openssl.dll
中的分号,然后重新启动xampp。
c/xampp/apache/bin/php.ini and c/xampp/php/php.ini
仍然保持相同的错误..
注意:我是php新手,但我想知道这一点,并解决问题。我在堆栈中提到了类似的问题,但它对我没有帮助,
有人可以帮我解决这个问题吗?
谢谢,
答案 0 :(得分:2)
看起来您连接到身份验证的凭据失败了。我经常从我的本地发送邮件,我发现它比使用mandrillapp之类的gmail更容易使用另一个SMTP,直到12,000封邮件。我不会在你的代码中理解很多东西,所以我会分享我的。
<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your@username.com'; // SMTP username
$mail->Password = 'mandrilapp_will_give_you_a_password'; // SMTP password
$mail->Port = 587; // TCP port to connect to
$mail->From = 'your@email.com';
$mail->FromName = 'Test phpmailer';
$mail->addAddress('who_are_you_sending@to.com'); // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
确保PHPMailer-master文件夹(可以从here下载)与php文件处于同一级别。这是我链接phpmailer的方式。希望它有所帮助,如果您有任何疑问,请问我!