【发布时间】:2020-01-30 22:31:49
【问题描述】:
我正在尝试在PHPMailer 的帮助下发送电子邮件,一切正常,除了现在我想在Sent Items 中获得发送邮件的副本。
该网站目前托管在Bluehost。我尝试按照PHPMailer - GMail 的示例进行操作,但我被困在应该指定的路径上。
以PHPMailer - GMail 为例,Sent Items 的路径为:
{imap.gmail.com:993/imap/ssl}[Gmail]/已发送邮件
我不知道我应该指定什么路径。在我的代码中一切正常,只是缺少发送项目的路径。
<?php
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$username = 'john@mydomain.com';
$password = 'password';
function save_mail( $mail ) {
//path or folder for sent items
$path = "{imap.mydomain.com:993/imap/ssl}[...]/..."; //what exactly is the path for my sent item.
//Tell your server to open an IMAP connection using the same username and password as you used for SMTP
$imapStream = imap_open( $path, $mail->Username, $mail->Password );
$result = imap_append( $imapStream, $path, $mail->getSentMIMEMessage() );
imap_close( $imapStream );
return $result;
}
function send(){
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = 'mail.mydomain.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->setFrom( $username, 'John Doe' );
$mail->addAddress( 'someone@example.com', 'David Doe' );
$mail->Subject = 'TEST SUBJECT';
$mail->msgHTML('<b>TEST</b>');
$mail->AltBody = 'TEST';
if ( !$mail->send() ) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
if (save_mail($mail)) {
echo "Message saved!";
}
}
}
?>
【问题讨论】:
-
使用 imap_list 函数列出您的文件夹。 Gmail 中的“已发送邮件”文件夹因语言而异。
标签: php smtp phpmailer imap bluehost