在PHPMailer中发送$ mail-> Body [英] Issue with $mail->Body in the PHPMailer

查看:129
本文介绍了在PHPMailer中发送$ mail-> Body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是初学者。



我已经检查过论坛,找出答案,但我没有成功,其他问题都是专门针对PHPMailer的一些部分,但我的更为普遍。所以我希望没有人会将我的问题标记为重复,因为我正在学习曲线。



我正在开发一个PHP项目。它的工作原理是用户进入页面,并以表单(文本编辑器)写入一些注释,并点击发送按钮。我可以在我的电子邮件中收到他的消息。我已经设置了密码并且主机为空,因为明显的原因,但后来它将是我自己的域的真实电子邮件。



问题是这里有2个问题:




  • 我不想用$ mail-> Body发送一个静态文本,但是我想让程序采用用户的消息,名称和电子邮件地址并发送该邮件发送到我的电子邮件。

  • 当我收到电子邮件时,我看不到发件人的姓名和电子邮件,这意味着该程序无法获取电子邮件和名称会话中的用户



可以请您提供有助于您的建议或示例代码。谢谢。

  if($ _ POST ['mode'] =='send'){

$ mail = new PHPMailer();

$ mail-> IsSMTP(); //告诉班级使用SMTP
$ mail-> Host =myhost; // SMTP server
// $ mail-> SMTPSecure ='ssl';
$ mail-> From =我的电子邮件;
$ mail-> port ='26'; //也可以更改为465
$ mail-> SMTPAuth = true;
$ mail->用户名=我的电子邮件;
$ mail-> Password =我的密码;

$ mail-> AddAddress(我的电子邮件);

$ Name = mysql_real_escape_string($ _ POST ['part_fname']); //这个代码没有得到名字和电子邮件
$ Email = mysql_real_escape_string($ _ POST ['part_email']);

$ mail-> Subject =新错误报告;
$ mail-> Body =用户信息,名称=。 $名称。 || Email =。 $电子邮件; //我想获取用户的消息不是静态消息!
$ mail-> WordWrap = 50;

如果(!$ mail-> Send())
{
echo'消息未发送。
echo'Mailer error:'。 $ MAIL-> ERRORINFO;
}
else
{
$ message =感谢您的建议;
}
}

这是html中的表单:

 < form role =formname =Form2action =method =postclass =form-horizo​​ntal> ; 
< textarea name =bugcols =100rows =15id =textareaplaceholder =输入文本...>< / textarea>
< input name =modevalue =sendtype =hidden>
< p style =margin-top:5px;>< input type =resetvalue =清除文本! class =btn btn-danger>& nbsp; < button type =submitclass =btn btn-primary>发送报告< / button>< / p>
< / form>


解决方案

删除 mysql_real_escape_string 它需要连接到mysql。

  $ Name = $ _POST ['part_fname']; 
$ Email = $ _POST ['part_email'];
$ message = $ _POST ['bug']; //添加它以获取textarea值

然后将值附加到正文:

  $ mail-> Body =用户信息,名称=。 $名称。 || Email =。 $电子邮件; 
$ mail-> Body。=Message:。$ message;






如果你想要会话



何时到用户登录:

  session_start(); 
$ _SESSION ['part_fname'] = $ Name;
$ _SESSION ['part_email'] = $ Email;

发送电子邮件时:

 在session_start(); 
$ Name = $ _SESSION ['part_fname'];
$ Email = $ _SESSION ['part_email'];
$ message = $ _POST ['bug'];


a beginner here.

I have already checked the forum to find out answers but I was not successful, other questions were specifically on some parts of PHPMailer but mine is more general. So I hope no one will mark my question as duplicate as I am in learning curve.

I am working on a PHP project. How it works is that the user goes to the page and writes some comments in a form (a text editor) and clicks on the send button. I am able to receive his message in my email. I have set the password and host empty for obvious reasons but later it will be my real email with my own domain.

Problem is 2 problems here:

  • I do not want to send a static body with $mail->Body, but I want the program to take the user's message, name and email address and send that message to my email.
  • When the email is received by me, I can not see the name and email of the sender which means the program is not able to get the email and name of the user from the session

Could you please give your suggestion or sample code that helps. Thank you.

if($_POST['mode']=='send'){

    $mail = new PHPMailer();

    $mail->IsSMTP();                                // telling the class to use SMTP
    $mail->Host     = "myhost";                     // SMTP server
                                                    //$mail->SMTPSecure = 'ssl';
    $mail->From     = "my email";
    $mail->port     = '26';                             // can also change to 465
    $mail->SMTPAuth = true; 
    $mail->Username= "my email"; 
    $mail->Password = "my password"; 

    $mail->AddAddress("my email");

    $Name = mysql_real_escape_string($_POST['part_fname']);    //this code is not getting the name and email
    $Email = mysql_real_escape_string($_POST['part_email']);

    $mail->Subject  = "New Bug Report";
    $mail->Body     = "User Information, Name = " . $Name . " ||  Email = " . $Email; // I want to get the user's message not a static message!
    $mail->WordWrap = 50;

    if(!$mail->Send())
     {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } 
    else 
    {
            $message = 'Thanks for your suggestion';
    }
 }

Here is the form in html:

<form role="form" name="Form2" action="" method="post" class="form-horizontal">
<textarea name="bug" cols="100" rows="15" id="textarea" placeholder="Enter text ..."></textarea>
<input name="mode" value="send" type="hidden">
<p style="margin-top:5px;"><input type="reset" value="Clear Text!" class="btn btn-danger">&nbsp; <button type="submit" class="btn btn-primary">Send report</button></p>
</form>

解决方案

Remove mysql_real_escape_string it requires a connection to mysql.

$Name = $_POST['part_fname'];    
$Email = $_POST['part_email'];
$message= $_POST['bug'];//add this to get the textarea value

then append the value to the body:

$mail->Body = "User Information, Name = " . $Name . " ||  Email = " . $Email;
$mail->Body .= "Message: ".$message;


If you wanted sessions

when to user logs in:

session_start();
$_SESSION['part_fname'] = $Name;
$_SESSION['part_email'] = $Email;

when sending email:

session_start();
$Name = $_SESSION['part_fname']; 
$Email = $_SESSION['part_email']; 
$message= $_POST['bug']; 

这篇关于在PHPMailer中发送$ mail-&gt; Body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆