当传入邮件插入数据库时​​,只有一个电子邮件详细信息插入数据库

时间:2022-09-14 23:02:05

with * developers, I won my first task. I am creating incoming email insert to the database query.

使用*开发人员,我赢得了我的第一个任务。我正在创建传入的电子邮件插入数据库查询。

this is my code :

这是我的代码:

#!/usr/bin/php -q
<?PHP 

/* connect to gmail */
$hostname = '{example.org:995/pop3/ssl/novalidate-cert}'; 
$username = 'user';
$password = 'pass';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to SERVER: ' . imap_last_error());


/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);

        /* output the email header information */
        //$read_status  = '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $subject = $overview[0]->subject;
        $from    = $overview[0]->from;
        $received_date = '<span class="date">on '.$overview[0]->date.'</span>';
        //$output.= '</div>';

        /* output the email body */
        $output= '<div class="body">'.$message.'</div>';
    }

    //echo $output;
$servername = "localhost";
$username = "username ";
$password = "password ";
$dbname = "dbname";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

//INSERT INTO DATABASE

$sql = "INSERT INTO ads(`banner_name`, `hd_image`)VALUES('".$message."', '".$subject."') ";
$result = mysqli_query($conn, $sql);
} 

/* close the connection */
imap_close($inbox);

?>

This is working well. my problem is, when I send a new mail to my email address. query will execute and insert first email details every time. not newly send email details. please help me to resolve this.

这很好用。我的问题是,当我向我的电子邮件地址发送新邮件时。查询将每次执行并插入第一封电子邮件详细信息。不新发送电子邮件详情。请帮我解决这个问题。

also, how can I download if email receive with an attachment?

另外,如果电子邮件收到附件,我该如何下载?

appreciate your great help

感谢你的帮助

1 个解决方案

#1


0  

Here you need to use asort() function because rsort() will use to reverse the order and you need the last index of array which is latest record than you will use asort() for getting latest record on last index.

在这里你需要使用asort()函数,因为rsort()将用于反转顺序,你需要最新的数组索引,而不是你将使用asort()来获取最后一个索引的最新记录。

asort($emails); // it will give you latest email on last index.

#1


0  

Here you need to use asort() function because rsort() will use to reverse the order and you need the last index of array which is latest record than you will use asort() for getting latest record on last index.

在这里你需要使用asort()函数,因为rsort()将用于反转顺序,你需要最新的数组索引,而不是你将使用asort()来获取最后一个索引的最新记录。

asort($emails); // it will give you latest email on last index.