PhpMailer 无效地址:(setFrom)
PhpMailer Invalid address:(setFrom)
我不知道为什么它不起作用。据我了解,我的 post 请求未被接受……所以这是我的代码
<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
非常感谢大家的帮助:)
不要这样做:
$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->Host = 'ssl://smtp.gmail.com';
设置了 Host
值,同时还设置了 SMTPSecure = 'tls'
;这是不兼容的,不会工作。将 Host
设置为简单的 smtp.gmail.com
.
如果您使用 PHPMailer 提供的 gmail 示例来避免所有这些问题,那么整个加载过程会轻松很多。
试试这个:
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;
}
}
我不知道为什么它不起作用。据我了解,我的 post 请求未被接受……所以这是我的代码
<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
非常感谢大家的帮助:)
不要这样做:
$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->Host = 'ssl://smtp.gmail.com';
设置了 Host
值,同时还设置了 SMTPSecure = 'tls'
;这是不兼容的,不会工作。将 Host
设置为简单的 smtp.gmail.com
.
如果您使用 PHPMailer 提供的 gmail 示例来避免所有这些问题,那么整个加载过程会轻松很多。
试试这个:
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;
}
}