【问题标题】:PhpMailer Invalid address:(setFrom)PhpMailer 无效地址:(setFrom)
【发布时间】:2020-11-20 04:54:53
【问题描述】:

我不知道为什么它不起作用。据我了解,我的发帖请求没有被接受......所以这是我的代码

<meta charset="utf-8" type="text/html">

<?php
echo !extension_loaded('openssl')?"Not Available":"Available";
echo'<br>';
include 'PHPMailer/PHPMailerAutoload.php';
$name = $_POST['nombre'];
$mailfrom = $_POST['email'];
$context = $_POST['context'];

echo $name,$mailfrom,$context;
echo "<br>";

echo "<br>";
$subject = 'Information';
/*i'm not sure if i can use any gmail or it needs to be
  registred on my server admin panel*/
$to ="my@gmail.com";
define ('GUSER','my@gmail.com');
define ('GPWD','pass');


// make a separate file and include this file in that. call this function in that file.

function smtpmailer( $mailfrom, $name, $subject, $context) {
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->SMTPAutoTLS = false;
    $mail->Host = 'ssl://smtp.gmail.com';
    $mail->Port = 587;
    $mail->CharSet = "UTF-8";

    $mail->Username = GUSER;
    $mail->Password = GPWD;
    $mail->SetFrom ($mailfrom); //here it's my error
    $mail->Subject = $subject;
    $mail->Body = $context;
    $mail->AddAddress('my@gmail.com');
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo;
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}
 smtpmailer();

  ?>

其他需要提及的重要事项。这是我的托管服务提供商给我的设置。而且我不确定我是否正确使用它们。我已经尝试了所有端口和主机,但得到了同样的错误。

smtp settings

ports

我真的很感谢所有的帮助:)

【问题讨论】:

  • setForm 需要 2 个参数,第一个是您的邮件地址,第二个是描述它的文本。这里是 phpmailer 官方主页的示例:$mail-&gt;setFrom('from@example.com', 'Your Name'); 并且您必须检查 $mailForm 的内容是否真的是一个字符串和一个有效的邮件地址
  • 试过还是同样的错误
  • 没有。 setFrom的第二个参数是可选的,如果你提供了一个无效的地址会返回false,所以你不需要提前检查,看它的返回值即可。
  • @Eduardo Rawrdríguez 当你调用函数`smtpmailer(); ` 你需要像这样传递 4 个参数 ` smtpmailer( $mailfrom, $name, $subject, $context); `

标签: php email web hosting phpmailer


【解决方案1】:

不要这样做:

$mailfrom = $_POST['email'];
...
$mail->SetFrom($mailfrom);

即使$mailfrom 是有效地址,这也是伪造的,您的邮件将无法通过 SPF 检查,要么被退回,要么最终进入垃圾邮件文件夹。这就是 PHPMailer 文档和示例告诉您不要这样做的原因。

第二个参数完全是可选的;与直接设置From 相比,调用setFrom 的主要优点是它会立即验证地址,因此您无需等到尝试发送才能找到。

另外,你是通过 gmail 发送的,它不允许你从任意地址发送,只能从预定义的别名发送,所以无论如何它都不起作用。

正确的做法是:

$mail->setFrom('my@gmail.com');
if (!$mail->addReplyTo($mailfrom)) {
    echo 'Invalid email address';
    exit;
}

您还使用$mail-&gt;Host = 'ssl://smtp.gmail.com'; 设置Host 值,同时还设置SMTPSecure = 'tls';这是不兼容的,将不起作用。将Host 设置为简单的smtp.gmail.com

如果您使用 PHPMailer 提供的 gmail 示例来避免所有这些问题,那将会更容易。

【讨论】:

  • 好的,所以我试了一下,它应该可以工作。至少它没有向我发送任何错误,但也没有发送任何邮件。 D:
【解决方案2】:

试试这个:

include 'PHPMailer/PHPMailerAutoload.php';

define ('GUSER','my@gmail.com');
define ('GPWD','pass');

$name       = $_POST['nombre'];
$mailfrom   = $_POST['email'];
$context    = $_POST['context'];

$subject    = 'Information';
$to         = "my@gmail.com";

smtpmailer($mailfrom, $name, $subject, $context);


// make a separate file and include this file in that. call this function in that file.

function smtpmailer($from, $name, $subject, $context) {
    global $error;
    $mail = new PHPMailer();                    // create a new object

    $mail->isSMTP();                            // Set mailer to use SMTP
    $mail->Host      = 'smtp.gmail.com';        // Specify main and backup SMTP servers
    $mail->SMTPAuth  = true;                    // Enable SMTP authentication
    $mail->CharSet   = "UTF-8";
    $mail->SMTPDebug = 2;                       // Enable verbose debug output
    $mail->isHTML(true);                        // Set email format to HTML
    
    
    $mail->Username = GUSER;                    // SMTP username
    $mail->Password = GPWD;                     // SMTP password
    
    //$mail->SMTPAutoTLS    = false;
    $mail->SMTPSecure   = 'tls';                // Enable TLS encryption, `ssl` also accepted
    $mail->Port         = 587;                  // TCP port to connect to

    $mail->setFrom($from,$name);            // Mail Form
    $mail->addAddress('my@gmail.com');          // Name is optional

    $mail->Subject = $subject;
    $mail->Body    = $context;
    
   if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo;
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}

【讨论】:

  • @Eduardo Rawrdríguez 如果此代码不起作用您需要先注销所有 gmail,然后登录您在 CONSTANT 变量GUSER 使用的邮件,然后转到此链接:click here 更改此:允许不太安全应用程序:关闭以允许不太安全的应用程序:打开。然后再次尝试发送邮件。
  • 是的,你说的我都试过了,还是没有收到邮件。实际上我的帐户与我的手机同步以验证......但仍然没有你,你
猜你喜欢
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
  • 2015-01-08
  • 2020-09-01
  • 1970-01-01
  • 2011-04-28
  • 2014-08-15
相关资源
最近更新 更多