请谁能解释一下我正在处理发送电子邮件 php 代码并在 GitHub 上找到此代码的代码?


private function getSMTPSettingList()
{
$configs = OW::getConfig()->getValues('base');
return array(
'connectionPrefix' => $configs['mail_smtp_connection_prefix'],
'host' => $configs['mail_smtp_host'],
'port' => $configs['mail_smtp_port'],
'user' => $configs['mail_smtp_user'],
'password' => $configs['mail_smtp_password']
);
}

这是我在 oxwal 邮件服务的 GitHub 上找到的代码.php

我是 php 的新手,所以我正在从这段代码中得到帮助......但我什么都听不懂...

这是整个代码现在如何使用此功能从我的网站发送电子邮件......

类BOL_MailService {

const MAIL_COUNT_PER_CRON_JOB = 50;
const TRANSFER_SMTP = 'smtp';
const TRANSFER_MAIL = 'mail';
const TRANSFER_SENDMAIL = 'sendmail';
private $mailDao;
private $defaultMailSettingList = array();
private function __construct()
{
$this->mailDao = BOL_MailDao::getInstance();
$siteName = OW::getConfig()->getValue('base', 'site_name');
$siteEmail = OW::getConfig()->getValue('base', 'site_email');
$senderSuffix = defined('OW_SENDER_MAIL_SUFFIX') ? OW_SENDER_MAIL_SUFFIX : null;
$this->defaultMailSettingList = array(
'sender' => array($siteEmail, $siteName),
'senderSuffix' => intval($senderSuffix)
);
}
/**
* Class instance
*
* @var BOL_MailService
*/
private static $classInstance;
/**
*
* @var PHPMailer
*/
private $phpMailer;
/**
* Returns class instance
*
* @return BOL_MailService
*/
public static function getInstance()
{
if ( !isset(self::$classInstance) )
{
self::$classInstance = new self();
}
return self::$classInstance;
}
/**
*
* @return PHPMailer
*/
private function getMailer()
{
if ( !isset($this->phpMailer) )
{
$this->phpMailer = $this->initializeMailer($this->getTransfer());
}
return $this->phpMailer;
}
/**
*
* @return PHPMailer
*/
private function initializeMailer( $transfer )
{
$mailer = new PHPMailer(true);
switch ( $transfer )
{
case self::TRANSFER_SMTP :
$this->smtpSetup($mailer);
break;
case self::TRANSFER_SENDMAIL :
$mailer->IsSendmail();
break;
case self::TRANSFER_MAIL :
$mailer->IsMail();
break;
}
$mailer->CharSet = "utf-8";
return $mailer;
}
public function getTransfer()
{
if ( OW::getConfig()->getValue('base', 'mail_smtp_enabled') )
{
return self::TRANSFER_SMTP;
}
return self::TRANSFER_MAIL;
}
private function getSMTPSettingList()
{
$configs = OW::getConfig()->getValues('base');
return array(
'connectionPrefix' => $configs['mail_smtp_connection_prefix'],
'host' => $configs['mail_smtp_host'],
'port' => $configs['mail_smtp_port'],
'user' => $configs['mail_smtp_user'],
'password' => $configs['mail_smtp_password']
);
}
/**
*
* @param PHPMailer $mailer
*/
private function smtpSetup( $mailer )
{
$settingList = $this->getSMTPSettingList();
$mailer->SMTPSecure = $settingList['connectionPrefix'];
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->SMTPKeepAlive = true;
$mailer->Host = $settingList['host'];
if ( !empty($settingList['port']) )
{
$mailer->Port = (int) $settingList['port'];
}
$mailer->Username = $settingList['user'];
$mailer->Password = $settingList['password'];
}
public function smtpTestConnection()
{
if ( $this->getTransfer() !== self::TRANSFER_SMTP )
{
throw new LogicException('Mail transfer is not SMTP');
}
$mailer = $this->getMailer();
try
{
return $mailer->SmtpConnect();
}
catch ( phpmailerException $e )
{
throw new InvalidArgumentException($e->getMessage());
}
}
/**
*
* @return BASE_CLASS_Mail
*/
public function createMail()
{
$mail = new BASE_CLASS_Mail($this->defaultMailSettingList);
return $mail;
}
private function createMailFromDto( BOL_Mail $mailDto )
{
$mail = new BASE_CLASS_Mail();
$mail->addRecipientEmail($mailDto->recipientEmail);
$mail->setSender($mailDto->senderEmail, $mailDto->senderName);
$mail->setSubject($mailDto->subject);
$mail->setTextContent($mailDto->textContent);
$mail->setHtmlContent($mailDto->htmlContent);
$mail->setSentTime($mailDto->sentTime);
$mail->setPriority($mailDto->priority);
$mail->setSenderSuffix($mailDto->senderSuffix);
return $mail;
}
private function prepareFromEmail( $email, $suffix )
{
if ( empty($email) )
{
return null;
}
$suffix = intval($suffix);
if ( empty($suffix) )
{
return $email;
}
list($user, $provider) = explode('@', $email);
return $user . '+' . $suffix . '@' . $provider;
}
public function send( BASE_CLASS_Mail $mail )
{
$mailer = $this->getMailer();
$mailState = $mail->saveToArray();
$event = new OW_Event('base.mail_service.send.check_mail_state', array(), $mailState);
OW::getEventManager()->trigger($event);
$mailState = $event->getData();
if ( empty($mailState['recipientEmailList']) )
{
return false;
}
$fromEmail = $this->prepareFromEmail($mailState['sender'][0], $mailState['senderSuffix']);
$mailer->SetFrom($fromEmail, $mailState['sender'][1]);
$mailer->Sender = $mailState['sender'][0];
if ( !empty($mailState['replyTo']) )
{
$mailer->AddReplyTo($mailState['replyTo'][0], $mailState['replyTo'][1]);
}
foreach ( $mailState['recipientEmailList'] as $item )
{
$mailer->AddAddress($item);
}
$isHtml = !empty($mailState['htmlContent']);
$mailer->Subject = $mailState['subject'];
$mailer->IsHTML($isHtml);
$mailer->Body = $isHtml ? $mailState['htmlContent'] : $mailState['textContent'];
$mailer->AltBody = $isHtml ? $mailState['textContent'] : '';
$result = $mailer->Send();
$mailer->ClearReplyTos();
$mailer->ClearAllRecipients();
return $result;
}
private function mailToDtoList( BASE_CLASS_Mail $mail )
{
$mailState = $mail->saveToArray();
$resultList = array();
foreach ( $mailState['recipientEmailList'] as $email )
{
$mailDto = new BOL_Mail();
$mailDto->senderEmail = $mailState['sender'][0];
$mailDto->senderName = $mailState['sender'][1];
$mailDto->subject = $mailState['subject'];
$mailDto->textContent = $mailState['textContent'];
$mailDto->htmlContent = $mailState['htmlContent'];
$mailDto->sentTime = empty($mailState['sentTime']) ? time() : $mailState['sentTime'];
$mailDto->priority = $mailState['priority'];
$mailDto->recipientEmail = $email;
$mailDto->senderSuffix = intval($mailState['senderSuffix']);
$resultList[] = $mailDto;
}
return $resultList;
}
public function addToQueue( BASE_CLASS_Mail $mail )
{
$dtoList = $this->mailToDtoList($mail);
foreach ( $dtoList as $dtoMail )
{
$this->mailDao->save($dtoMail);
}
}
public function addListToQueue( array $mailList )
{
$fullDtoList = array();
foreach ( $mailList as $mail )
{
$dtoList = $this->mailToDtoList($mail);
foreach ( $dtoList as $mailDto )
{
$fullDtoList[] = $mailDto;
}
}
if ( !empty($fullDtoList) )
{
$this->mailDao->saveList($fullDtoList);
}
}
public function processQueue( $count = self::MAIL_COUNT_PER_CRON_JOB )
{
$list = $this->mailDao->findList($count);
$processedIdList = array();
foreach ( $list as $item )
{
try
{
$mail = $this->createMailFromDto($item);
$this->send($mail);
}
catch ( Exception $e )
{
//Skip invalid email adresses
}
$this->mailDao->updateSentStatus($item->id);
}
$this->mailDao->deleteSentMails();
}
public function getEmailDomain()
{
switch ( $this->getTransfer() )
{
case self::TRANSFER_SMTP:
$settings = $this->getSMTPSettingList();
return $settings['host'];
default:
$urlInfo = parse_url(OW_URL_HOME);
return $urlInfo['host'];
}
}
public function deleteQueuedMailsByRecipientId( $userId )
{
$user = BOL_UserService::getInstance()->findUserById($userId);
if ( $user === null )
{
return;
}
$this->mailDao->deleteByRecipientEmail($user->email);
}
public function __destruct()
{
$this->getMailer()->SmtpClose();
}

}

这是发送邮件的简单PHP邮件函数

$to      = $email_id;
$subject = "Test Subject";
$message = "Your mail Body Content. you also use here HTML Tags for better UI. ";
$headers = 'From: xyz@domainname.com' . "rn" .
'Reply-To: xyz@domainname.com' . "rn" .
'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "rn";
$headers.= "Content-type:text/html;charset=UTF-8" . "rn";
$test=mail($to, $subject, $message, $headers);

或者下载 PHPMailer 库并参考以下示例

ini_set('SMTP', "smtp.gmail.com"); // Your domain SMTP
ini_set('smtp_port', "465"); // here we write your domain SMTP port
ini_set('sendmail_from', "xxxx@domain.com"); // your email make sure email id is available on above mention SMTP 
ini_Set('password',"xxxxxx"); // email account password

require 'C:xampphtdocsdemoPHPMailer_5.2.4class.phpmailer.php'; //load phpmailer library here
$mail = new PHPMailer;
$mail->IsSMTP();         // send via SMTP
//IsSMTP();             // send via SMTP
$mail->SMTPAuth = true;  // turn on SMTP authentication
$mail->Host="smtp.gmail.com";
$mail->Mailer="smtp"; // domain SMTP
$mail->port=465; // Domain mail port
$mail->Username = "abc@gmail.com";   // SMTP username
$mail->Password = "xxxxx";      // SMTP password
$webmaster_email = "abc@gmail.com";     //Reply to this email ID
$email="abc@test.com";                 // Recipients email ID
$name="Test mail";             // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Test Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz");        // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");   // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Thanks For Connecting";
$mail->Body = "This is your mail body"; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format";     //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

相关内容

  • 没有找到相关文章

最新更新