php PHPMailer from 显示为 Root 用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19437544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:27:39  来源:igfitidea点击:

PHPMailer from is showing as Root User

php

提问by charlie

I am using PHP Mailer to send emails and i am using SMTP

我正在使用 PHP Mailer 发送电子邮件,我正在使用 SMTP

here is the code i am using:

这是我正在使用的代码:

$email = new PHPMailer();

            $email->IsSMTP(); // telling the class to use SMTP
            $email->Host       = "mail.integradigital.co.uk"; // SMTP server
            $email->SMTPDebug  = 2; // enables SMTP debug information (for testing)
                                                       // 1 = errors and messages
                                                       // 2 = messages only
            $email->SMTPAuth   = true;  // enable SMTP authentication
            $email->Host       = "mail.integradigital.co.uk"; // sets the SMTP server
            $email->Port       = 26;    // set the SMTP port for the GMAIL server
            $email->Username   = "sending@************.co.uk"; // SMTP account username
            $email->Password   = "********";        // SMTP account password

            $email->SetFrom($result["emailfrom"]);
            $email->FromName($result["emailfrom"]);

            $email->Subject   = $result["subject"];
            $email->Body      = $result["message"];

but its sending and showing as from Root User- root@localhost

但它发送和显示为来自Root 用户- root@localhost

i know i can change this in the class.phpmailer.php file but i want to be able to set it in the code above

我知道我可以在 class.phpmailer.php 文件中更改它,但我希望能够在上面的代码中设置它

is this possible?

这可能吗?

回答by Fancy John

Okay, in short:

好吧,简而言之:

$email->From = "[email protected]";
$email->FromName = "Support Team";

This worked out for me.

这对我有用。

回答by tlenss

Wrap the whole thing in a try catch. And initialize phpmailer with exceptions enabled

将整个事情包裹在 try catch 中。并在启用异常的情况下初始化 phpmailer

$email = new PHPMailer(true);

It looks like the default values are used. So something goes wrong when calling setFrom(). And it's failing silently because it returns falsewithout exceptions enabled.

看起来使用了默认值。所以调用时出了点问题setFrom()。它默默地失败了,因为它在false没有启用异常的情况下返回。

try {
  // your mail code here
} catch (phpmailerException $e) {
  echo $e->getMessage();
}

And change the setting of email and from name to:

并将电子邮件和名称的设置更改为:

$email->setFrom($result["emailfrom"], $result["emailfrom"]);

FromNameis a property not a method.

FromName是属性而不是方法。

回答by Kipngetich Yegon

class.phpmailer.php file contains the "var $FromName = 'Root User';" section which can be changed to "Desired Name" from "Root User" and have your job done.

class.phpmailer.php 文件包含“var $FromName = 'Root User';” 部分可以从“根用户”更改为“所需名称”并完成您的工作。

回答by Jturtle

Inside your form when you are getting the 1. emailand 2. nameit should look like this

当您收到 1. email和 2. name时,在您的表单内,它应该如下所示

  1. <input type="text" name="emailfrom">

  2. <input type="text" name="namefrom">

  1. <input type="text" name="emailfrom">

  2. <input type="text" name="namefrom">

Inside your php code it should look like this.

在您的 php 代码中,它应该是这样的。

$mail->setFrom($_POST['emailfrom'],$_POST['namefrom']);

$mail->setFrom($_POST['emailfrom'],$_POST['namefrom']);

I was having the same issue when I was using these settings on infinityhost I hope this helps.

我在 infinityhost 上使用这些设置时遇到了同样的问题,希望这会有所帮助。

$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.```

回答by tivnet

I had this problem when I was sending only the first parameter to setFrom(), as name <email>.

当我只将第一个参数发送到setFrom(), as时,我遇到了这个问题name <email>

setFrom()did not know how to parse that construction.

setFrom()不知道如何解析该构造。

Changing to two separate parameters solved the issue.

更改为两个单独的参数解决了这个问题。

回答by vicky

Use:

用:

$mail->setFrom('here email add of sender','here name of the sender');

First parameter is the email addres if it is empty or not a valid email address it will show as ROOT user while sending mail using php mailer

第一个参数是电子邮件地址,如果它为空或不是有效的电子邮件地址,它将在使用 php mailer 发送邮件时显示为 ROOT 用户