代码之家  ›  专栏  ›  技术社区  ›  E.Rawrdríguez.Ophanim

PhpMailer无效地址:(setFrom)

  •  1
  • E.Rawrdríguez.Ophanim  · 技术社区  · 8 年前

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

    <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

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

    2 回复  |  直到 8 年前
        1
  •  2
  •   Synchro    8 年前

    不要这样做:

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

    即使 $mailfrom

    setFrom 过度设置 From 直接的原因是它会立即验证地址,这样您就不需要等到尝试发送时才发现。

    此外,你通过gmail发送邮件,它不允许你从任意地址发送,只允许使用预定义的别名,所以无论如何它都无法工作。

    正确的方法是:

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

    Host 具有的值 $mail->Host = 'ssl://smtp.gmail.com'; 同时也设置 SMTPSecure = 'tls' ; 这是不兼容的,不会起作用。设置 主办 smtp.gmail.com

        2
  •  1
  •   Rashid    5 年前

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