还有其他方法可以测试我的代码。我想在电子邮件中使用带有网址的php发送电子邮件。
sendemail.ini
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=xxxxxxx@gmail.com
auth_password=xxxxxxxx
php.ini
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP=smtp.gmail.com
smtp_port=587
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = xxxxxxx@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
mail.add_x_header=Off
php我用
<?php
$subject = "Simple mail";
$message = "Here is a test mail";
$to = "xxxxxxx@gmail.com";
$from = "xxxxxxx@gmail.com";
$header = "From: ".$from;
if(mail($to, $subject, $message, $header)){
print "success<br>";
}else{
print "fail<br>";}
?>
我在debug.log中找到的东西
14/04/19 04:42:11 ** --- MESSAGE BEGIN ---
14/04/19 04:42:11 ** To: xxxxxxxx@gmail.com
14/04/19 04:42:11 ** Subject: Simple mail
14/04/19 04:42:11 ** From: xxxxxxxx@gmail.com
14/04/19 04:42:11 **
14/04/19 04:42:11 ** Here is a test mail
14/04/19 04:42:11 ** --- MESSAGE END ---
14/04/19 04:42:12 ** Connecting to smtp.gmail.com:587
14/04/19 04:42:12 ** Connected.
14/04/19 04:42:12 << 220 mx.google.com ESMTP ac5sm61735964pbc.37 - gsmtp<EOL>
14/04/19 04:42:12 >> EHLO C-PC<EOL>
14/04/19 04:42:12 << 250-mx.google.com at your service, [1.64.44.192]<EOL>250-SIZE 35882577<EOL>250-8BITMIME<EOL>250-STARTTLS<EOL>250-ENHANCEDSTATUSCODES<EOL>250 CHUNKING<EOL>
14/04/19 04:42:12 ** Authenticating as xxxxxxxx@gmail.com
14/04/19 04:42:12 >> STARTTLS<EOL>
14/04/19 04:42:13 << 220 2.0.0 Ready to start TLS<EOL>
14/04/19 04:42:13 >> QUIT<EOL>
14/04/19 04:42:13 << <--some random code here, cannot be copied-->
14/04/19 04:42:13 << <--some random code here again, cannot be copied-->
14/04/19 04:42:13 ** Disconnected.
14/04/19 04:42:13 ** Disconnecting from smtp.gmail.com:587
14/04/19 04:42:13 ** Disconnected.
14/04/19 04:42:13 ** Disconnected.
14/04/19 04:42:13 ** Connection Closed Gracefully.
是Windows 8.1还是我错过了一些???我花了将近一天的时间来解决这个问题。 继续在php中失败!
答案 0 :(得分:0)
试试这个
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}