Search code examples
phpemailwebhostingphpmailer

PhpMailer Invalid address:(setFrom)


I don't know why it's not working. As far as i understand in some point my post request it's not been accepted ... so here is my code

<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();

  ?>

Other important things to mention. This are the setting my hosting provider gives me. And i'm not sure if i'm using them properly. I've tried with all ports and host, but just get the same error.

smtp settings

ports

i really appreciate all the help :)


Solution

  • Try this:

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