【问题标题】:how to get e-mails on gmail account using PHPmailer如何使用 PHPmailer 在 gmail 帐户上获取电子邮件
【发布时间】:2014-10-29 07:24:47
【问题描述】:

我对这个领域真的很陌生。我正在研究我的投资组合。我已经为 “联系我” 制作了模态联系表(引导程序)。我的 gmail 帐户没有收到来自 php 的邮件。我正在使用 PHP 邮件程序,以下是我的 php 代码,文件名为 contact-form.php:

<?php

// check for form submission - if it doesn't exist then send back to contact form**
if (!isset($_POST['submit'])) {

$message=
    'Name:  '.$_POST['name'].'<br />
    Email:  '.$_POST['email'].'<br />
    Message:    '.$_POST['contact-message'];
require "phpmailer/class.phpmailer.php"; //include phpmailer class

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

// Set up SMTP
$mail->IsSMTP();    // Set up SMTP connection
$mail->SMTPAuth = true;     // Connection with SMTP does require authorization
$mail->SMTPSecure = "ssl";      // Connect using a TLS connection
$mail->Host ="smtp.gmail.com"; //Gmail SMTP address
$mail->Port = 465; // Gmail SMTP port No idea what this is suppose to be
$mail->Encoding ='7bit';

$mail->Username = "mygmailid@gmail.com";  // Gmail address
$mail->Password ="Password";  // Gmail Password

// Compose
$mail->SetFrom($_POST['email'],$_POST['name']);
$mail->AddReplyTo($_POST['email'],$_POST['name']);
$mail->Subject = "Mail from Portfolio";
$mail->MsgHTML($message);

//Send To
$mail->AddAddress("recipientmail@gmail.com", "Recipient Name");
$result = $mail->Send();
$message = $result ? 'Successfully Sent!' : 'Sending Failed';
unset($mail);

}

我已将我所有的 html 页面与 contact-form.php 链接为:

<form action="contactform.php" class="form-horizontal" method="post" enctype="text/plain">

当我点击发送消息按钮时,它会将我引导至contact-form.php,但没有任何反应。

请帮帮我,自上周以来,我一直陷入这个问题。

任何建议或帮助都将不胜感激。

提前谢谢你:) 塔亚巴。

【问题讨论】:

  • 你能做一些调试输出来验证数据是否传输到这个表单?
  • 正如我所说,我是这个领域的新手,您能详细说明您的问题吗?谢谢
  • 我看过下面的视频link
  • 例如,在if (!isset($_POST['submit'])) { 条件中添加一些echo "something",以便您知道代码是否已执行。然后检查你的$message变量的内容是成功还是失败

标签: php gmail phpmailer


【解决方案1】:

感谢您迄今为止应用的代码,因为很难调试您的代码,如果您使用静态信息测试代码会更好, 我在下面给出了一个工作示例:

    $email = new PHPMailer();
    $email->From      = 'amrinder.salentro@gmail.com';
    $email->FromName  = 'Amrinder Singh';
    $email->Subject   = 'Hello';
    $email->Body      = "How are you karan";
    $email->AddAddress( 'amrinder.salentro@gmail.com' );
    $email->AddAttachment( 'PATH_OF_YOUR_FILE_HERE' , 'NameOfFile.pdf' );   //optional 
    return $email->Send();

希望对您有所帮助。请随它去... 谢谢... :)

【讨论】:

  • 感谢您的回答。我已经应用了它,但它对我不起作用。可能是我错过了一些东西。我是否必须在我的 gmail 帐户设置中添加 POP 服务器? @失眠
  • @Synchro 问题是“到 gmail 帐户”而不是“通过 gmail 帐户”。再读一遍..
  • OP 的代码正在通过 gmail 帐户发送。
【解决方案2】:

首先,我建议您使用a known-good example 重新开始通过gmail 发送,并确保您使用的是最新的PHPMailer。至于您的脚本,这行意味着您的邮件处理代码只会在表单未提交时运行,并且由于这将导致发送错误,而您没有显示,您将不会什么都没发生!

if (!isset($_POST['submit'])) { 

这应该是:

if (isset($_POST['submit'])) {

【讨论】:

  • 谢谢@Synchro 我会处理你的建议并让你知道。
  • 我已经尝试过您提供的 GitHub 解决方案。幸运的是它正在工作,但它给了我以下错误:无法发送邮件。邮件程序错误:SMTP 连接()失败。这是什么意思???
  • 你需要read the docs
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 2021-09-01
  • 2014-09-08
  • 2021-12-16
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
相关资源
最近更新 更多