本文整理汇总了PHP中SMTP::tls方法的典型用法代码示例。如果您正苦于以下问题:PHP SMTP::tls方法的具体用法?PHP SMTP::tls怎么用?PHP SMTP::tls使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SMTP
的用法示例。
在下文中一共展示了SMTP::tls方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: smtpSend
/**
* Send mail via SMTP.
* @return boolean True on success.
*/
protected function smtpSend()
{
$this->smtp = new SMTP();
$allRecipients = array();
$badRecipients = array();
try {
// <editor-fold desc="Prepare SMTP host.">
$hostInfo = array();
if (!preg_match('/^((ssl|tls):\\/\\/)*([a-zA-Z0-9\\.-]*):?([0-9]*)$/', $this->smtpHost, $hostInfo)) {
throw new \Exception("Invalid SMTP host.");
}
$prefix = "";
$tls = $this->smtpSecure == "tls";
if ($hostInfo[2] == "ssl" || $hostInfo[2] == "" && $this->smtpSecure == "ssl") {
$prefix = "ssl://";
$tls = false;
} elseif ($hostInfo[2] == "tls") {
$tls = true;
}
$host = $prefix . $hostInfo[3];
$tport = (int) $hostInfo[4];
$port = $tport > 0 && $tport < 65536 ? $tport : $this->smtpPort;
// </editor-fold>
if (!$this->smtp->connect($host, $port)) {
throw new \Exception("Could not connect to SMTP host.");
}
$hello = !empty($this->smtpHelo) ? $this->smtpHelo : $this->serverHostname();
if (!$this->smtp->hello($hello)) {
throw new \Exception("Could not send HELO.");
}
if ($tls) {
if (!$this->smtp->tls()) {
throw new \Exception("Could not start TLS.");
}
$this->smtp->hello($hello);
}
if ($this->smtpAuth && !$this->smtp->authenticate($this->smtpUsername, $this->smtpPassword)) {
throw new \Exception("Could not authenticate.");
}
if (!$this->smtp->mail($this->from[0])) {
throw new \Exception("Could not send MAIL FROM.");
}
foreach ($this->to as $to) {
if (!$this->smtp->recipient($to[0])) {
$badRecipients[] = $to[0];
} else {
$allRecipients[] = $to[0];
}
}
// foreach ($this->cc as $cc) {
// if (!$this->smtp->recipient($cc[0]))
// $badRecipients[] = $cc[0];
// else
// $allRecipients[] = $cc[0];
// }
//
// foreach ($this->bcc as $bcc) {
// if (!$this->smtp->recipient($bcc[0]))
// $badRecipients[] = $bcc[0];
// else
// $allRecipients[] = $bcc[0];
// }
if (count($allRecipients) > 0 && !$this->smtp->data($this->header . $this->body)) {
throw new \Exception("Data was not accepted.");
}
if ($this->smtpKeepAlive) {
$this->smtp->reset();
} else {
$this->smtp->quit();
$this->smtp->close();
}
return true;
} catch (\Exception $e) {
if (count($badRecipients) > 0) {
$rcpt = implode(", ", $badRecipients);
throw new EmailException(array("Failed delivery to the following recipient(s): %s.", $rcpt));
}
throw new EmailException($e->getMessage());
}
}