使用PHP获取空电子邮件

时间:2015-04-29 11:25:28

标签: php

我正在使用php类发送电子邮件。它已成功发送电子邮件,但在我的Gmail电子邮件帐户中,它显示了一条空消息。我不明白为什么它会向我展示空消息内容。它是一个华丽的未知发件人和一个空的邮件正文。这是我的Gmail帐户中出现的图片。你能帮我解决一下吗?

你的帮助更加值得欣赏:)

enter image description hereenter image description here

以下是我用于使用php发送电子邮件的完整代码。

<?php 
$name="";
$from="";
$message="";

if(isset($_POST['submit'])){

$name=mysql_real_escape_string($_POST['name']);
$from=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['comments']);
$to="myemail@gmail.com"; // Add your e-mail here

include("library/send_email.php");  
}
  ?>

send_emaiil.php页面

<?php
require_once("class_email_sender.php");
$send_email= new class_email_sender($name,$from,$to,$message);
$send_email->send_email();
?>

class_email_sender.php page

<?php 
class class_email_sender{
private $name; 
private $from;
private $to;
private $to_email;
private $subject;
private $headers;
private $mail_message;

public function __construct($name,$from,$to_email,$mail_message){

$this->name=$name;
$this->from=$from;
$this->to_email=$to_email;
$this->mail_message=$mail_message;
}

public function send_email(){

$this->to=$this->to_email;
//$this->headers="From : $this->from<$this->from>";
$this->headers = 'From:'.$this->from. "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

$this->subject="Contact Message From your site";
$this->message="Name :" .$this->name. "\n";
$this->message.="Email :" .$this->from."\n\n";
$this->message.="Message :" . $this->mail_message."\n\n";
mail($this->to_email,$this->subject,$this->message,$this->headers);

echo '<script>alert("Thanks for your mail.We will notify you shortly")
</script>';
}   
}
?>

1 个答案:

答案 0 :(得分:0)

例如这里是为我工作的代码

<?php 
$username = $_POST["username"]; //Email address you want it to 'appear' to come 
$email = $_POST["email"]; 

if(strlen($username) && strlen($email))
{
     $mailText ="The Contact Details: <br>";
}
              $mailText=$mailText."<table border=1 cellspacing=0 cellpadding=0>";
                  while(list($Key, $Val)= each($_POST))
                   {
                    $mailText=$mailText."<tr><td width=50%>"; 
                    $mailText=$mailText."<b>".$Key."</b></td>";
                    $mailText=$mailText."<td width=50%>";   
                    $mailText=$mailText.$Val;
                    $mailText=$mailText."</td></tr>";  

             }

$to = "youremailid.com";
if(strlen($username) && strlen($email))
{
$subject = "your subject"; //Subject Line
  $headers .= "From:yourmail.com\n";
    $headers .= "X-Sender:  yourheader.com\n";
     $headers .= "Content-Type: text/html; charset=iso-8859-1\n";         // Mime type
$mailforms = mail($to, $subject, $mailText, $headers);
}
?>

尝试保持这样的acc代码并检查