/** * */ protected function send() { if (!attrib('to') || !attrib('body')) { pr(<<<'EOD' ------------------------------------------------------------ arguments: --to send to email address --subject email subject --body email content message example: php send --to xxx@gmail.com --subject "hello" --body "hi" php send --to xxx@gmail.com --body "$(cat message.txt)" ------------------------------------------------------------ EOD ); exit; } $getFrom = function () { $fromEmail = conf('gmail.email'); $fromName = conf('gmail.name'); return "{$fromName} <{$fromEmail}>"; }; $to = attrib('to'); $subject = attrib('subject', 'not-subject'); $body = attrib('body'); $result = \GmailManager::sendMessage($getFrom(), $to, $subject, $body); if ($result) { pr('Send success'); } else { pr('Send fail !'); } }
/** * import all emails * - 將 send 信件匯入, 然後刪除 * - 將 get 的未讀信信匯入, 設定成已讀 * - 導入 send 信件要早於 get, 奇怪的原因如下面 "NOTE" 寫的 * * NOTE: 一個待查的問題, 如果寄信給自己, 只會收到一封信, 然而 匯入 "get" & "send" 都會取得這封信, 這個會造成一些問題.... * */ protected function importAll() { if ("exec" !== attrib(0)) { pr('---- debug mode ---- (你必須要輸入參數 exec 才會真正執行)'); exit; } di('log')->record('start PHP ' . phpversion()); $show = []; $inboxes = new \Inboxes(); // import "get", "send" emails $messages = array_merge(\GmailManager::getUnreadMessages(), \GmailManager::getSendMessages()); // 時間為軸 舊 -> 新 排序 $messageSortByTime = []; foreach ($messages as $message) { $time = strtotime($message['headers']['date']); $messageSortByTime[$time] = $message; } sort($messageSortByTime); // foreach ($messageSortByTime as $message) { $inbox = $this->makeInbox($message); $result = $inboxes->addInbox($inbox); if ($result) { if ('unread' == $message['customType']) { // 將信件設定為 已讀 \GmailManager::setMessageLabelToIsRead($message['googleMessageId']); } else { // 刪除該信件!! \GmailManager::deleteMessage($message['googleMessageId']); } } else { $result = 'fail'; } if ('unread' == $message['customType']) { $type = 'get from: ' . $inbox->getFromEmail(); } else { $type = 'send to : ' . $inbox->getToEmail(); } $show[] = [$message['googleMessageId'], $inbox->getMessageId(), $inbox->getSubject(), $type, date('Y-m-d H:i:s', $inbox->getEmailCreateTime()), $result]; } if ($show) { table($show, ['google message id', 'message id', 'subject', 'from/to', 'date', 'result']); } }
/** * package init */ public function packageInit() { \GmailManager::init(conf('app.path') . '/var/attach'); }