gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:25:30 25 4
gpt4 key购买 nike

与 stackoverflow 开发人员一起,我赢得了第一个任务。我正在为数据库查询创建传入电子邮件插入。

这是我的代码:

#!/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);

?>

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

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

感谢您的大力帮助

最佳答案

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

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

关于php - 当传入邮件插入数据库时​​,只有一封电子邮件详细信息插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35355581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com