【问题标题】:xampp php email (gmail) doesnt workxampp php电子邮件(gmail)不起作用
【发布时间】:2014-06-03 10:41:42
【问题描述】:
  • 我一直在尝试使用 xampp 使用 php 发送电子邮件(我使用的是 windows 8.1)
  • 我就是搞不定
  • 试过 smtp_port= 465/ 587/ 25
  • 我已使用 IMAP 启用 gmail
  • 从 debug.log 中,我想我已连接到 google

有没有其他方法可以测试我的代码。我想使用 php 发送电子邮件,并在电子邮件中包含 url。

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 中失败!

【问题讨论】:

  • 您的 ISP 可能会阻止外发电子邮件。我的 ISP 阻止从其 IP 地址发出的外发电子邮件以避免垃圾邮件。也许您的 ISP 也在这样做。 authsmtp.com/faqs/faq-4.html
  • 谢谢您的信息。有没有其他方法可以测试我的代码。我想使用 php 发送电子邮件,并在电子邮件中包含 url。
  • 您使用的是动态 IP 地址吗?如果您使用的是静态 IP 地址,您是否设置了服务器主机名和地址?
  • 我使用的是动态IP地址,我需要这样做吗??

标签: php email gmail xampp


【解决方案1】:

试试这个

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!";
}

【讨论】:

  • wt 是“file_get_contents('contents.html');” >.
猜你喜欢
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 2015-12-03
  • 2015-04-09
  • 1970-01-01
  • 2015-02-23
  • 2014-05-01
  • 2019-09-18
相关资源
最近更新 更多