作者:hlkzm
项目:crowdfundin
function send()
{
//第一步: 设置相关的headers
//1.设置邮件的Content-Type,要不然网站的消息内容里面的html标签会被当做纯文本处理
$smtpHeaderContentType = new SmtpHeaderContentType();
$smtpHeaderContentType->setType('text/html');
//2.设置编码集并添加Content-Type
$headers = new SmtpHeaders();
$headers->setEncoding('utf-8');
$headers->addHeader($smtpHeaderContentType);
//第二步:设置消息的相关
$message = new SmtpMessage();
$message->setHeaders($headers);
$message->addTo($this->mailTo)->addFrom($this->mailFrom)->setSubject($this->subject)->setBody($this->body);
//邮件的内容
//第三步:设置Smtp的相关链接参数
$smtpOptions = new SmtpOptions();
$smtpOptions->setHost($this->host)->setPort($this->port)->setConnectionClass('login')->setConnectionConfig(array('username' => $this->username, 'password' => $this->password, 'ssl' => 'ssl'));
//第四步:加载配置,发送消息
$smtpTransport = new SmtpTransport();
$smtpTransport->setOptions($smtpOptions);
//加载配置
$smtpTransport->send($message);
//发送消息
}
作者:eltrin
项目:DiamanteEmailProcessingBundl
/**
* Retrieves Message Reference
*
* @param \Zend\Mail\Headers $headers
* @return string|null
*/
protected function processMessageReference($headers)
{
$messageReference = null;
if ($headers->get('references')) {
preg_match('/<([^<]+)>/', $headers->get('references')->getFieldValue(), $matches);
$messageReference = $matches[1];
}
return $messageReference;
}
作者:haoyanfe
项目:zf
public function testSendMinimalMail()
{
$headers = new Headers();
$headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200');
$message = new Message();
$message->setHeaders($headers)->setSender('ralph.schindler@zend.com', 'Ralph Schindler')->setBody('testSendMailWithoutMinimalHeaders')->addTo('zf-devteam@zend.com', 'ZF DevTeam');
$expectedMessage = "RSET\r\n" . "MAIL FROM:<ralph.schindler@zend.com>\r\n" . "DATA\r\n" . "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n" . "Sender: Ralph Schindler <ralph.schindler@zend.com>\r\n" . "To: ZF DevTeam <zf-devteam@zend.com>\r\n" . "\r\n" . "testSendMailWithoutMinimalHeaders\r\n" . ".\r\n";
$this->transport->send($message);
$this->assertEquals($expectedMessage, $this->connection->getLog());
}
作者:pnaq5
项目:zf2dem
public function testSendEscapedEmail()
{
$headers = new Headers();
$headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200');
$message = new Message();
$message->setHeaders($headers)->setSender('ralph.schindler@zend.com', 'Ralph Schindler')->setBody("This is a test\n.")->addTo('zf-devteam@zend.com', 'ZF DevTeam');
$expectedMessage = "EHLO localhost\r\n" . "MAIL FROM:<ralph.schindler@zend.com>\r\n" . "DATA\r\n" . "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n" . "Sender: Ralph Schindler <ralph.schindler@zend.com>\r\n" . "To: ZF DevTeam <zf-devteam@zend.com>\r\n" . "\r\n" . "This is a test\r\n" . "..\r\n" . ".\r\n";
$this->transport->send($message);
$this->assertEquals($expectedMessage, $this->connection->getLog());
}
作者:gitter-badge
项目:diamantedesk-applicatio
private function headers()
{
$headers = new Headers();
$messageId = new MessageId();
$messageId->setId('testId');
$headers->addHeader($messageId);
$addressList = new AddressList();
$addressList->add('test@gmail.com');
$from = new From();
$from->setAddressList($addressList);
$headers->addHeader($from);
$to = new To();
$to->setAddressList($addressList);
$headers->addHeader($to);
return $headers;
}
作者:bradley-hol
项目:zf
/**
* Access headers collection
*
* Lazy-loads if not already attached.
*
* @return Headers
*/
public function headers()
{
if (null === $this->headers) {
$this->setHeaders(new Headers());
$this->headers->addHeaderLine('Date', date('r'));
}
return $this->headers;
}
作者:yakamoz-fan
项目:concret
/**
* Access headers collection
*
* Lazy-loads if not already attached.
*
* @return Headers
*/
public function getHeaders()
{
if (null === $this->headers) {
$this->setHeaders(new Headers());
$date = Header\Date::fromString('Date: ' . date('r'));
$this->headers->addHeader($date);
}
return $this->headers;
}
作者:dabielkabut
项目:eventu
/**
* Merge duplicate header fields into single headers field.
* The headers must be AbstractAddressList.
*
* @param Headers $headerBag
* @param HeaderInterface|AbstractAddressList[] $headers
*/
private function mergeDuplicateHeader(Headers $headerBag, $headers)
{
if ($headers instanceof HeaderInterface) {
// all good
return;
}
// use first headers as base and collect addresses there
$header = $headers[0];
unset($headers[0]);
if (!$header instanceof AbstractAddressList) {
throw new DomainException(sprintf('Cannot grab address list from headers of type "%s"; not an AbstractAddressList implementation', get_class($header)));
}
$addressList = $header->getAddressList();
foreach ($headers as $h) {
$addressList->merge($h->getAddressList());
}
$headerBag->removeHeader($header->getFieldName());
$headerBag->addHeader($header);
}
作者:xamin12
项目:platfor
/**
* Sets additional message headers
*
* @param \Zend\Mail\Headers $headers
* @param array $data
*/
protected function setExtHeaders(&$headers, array $data)
{
$headers->addHeaderLine(self::UID, $data[self::UID]);
$headers->addHeaderLine('InternalDate', $data[self::INTERNALDATE]);
}
作者:karnuri
项目:zf2-turtoria
/**
* Access headers collection
*
* Lazy-loads if not already attached.
*
* @return Headers
*/
public function getHeaders()
{
if (null === $this->headers) {
if ($this->mail) {
$part = $this->mail->getRawHeader($this->messageNum);
$this->headers = Headers::fromString($part);
} else {
$this->headers = new Headers();
}
}
return $this->headers;
}
作者:ramunas
项目:platfor
/**
* @dataProvider getEmbeddedContentIdProvider
* @param $rawHeaders
* @param $expected
*/
public function testGetEmbeddedContentId($rawHeaders, $expected)
{
$headers = Headers::fromString(implode(PHP_EOL, $rawHeaders), PHP_EOL);
$contentIdHeader = null;
$contentDispositionHeader = null;
if (isset($rawHeaders['Content-ID'])) {
$contentIdHeader = GenericHeader::fromString($rawHeaders['Content-ID']);
}
if (isset($rawHeaders['Content-Disposition'])) {
$contentDispositionHeader = GenericHeader::fromString($rawHeaders['Content-Disposition']);
}
$this->part->expects($this->any())->method('getHeader')->willReturnMap([['Content-ID', null, $contentIdHeader], ['Content-Disposition', null, $contentDispositionHeader]]);
$this->part->expects($this->any())->method('getHeaders')->willReturn($headers);
$this->assertEquals($expected, $this->attachment->getEmbeddedContentId());
}
作者:navassouz
项目:zf
/**
* Public constructor
*
* This handler supports the following params:
* - file filename or open file handler with message content (required)
* - startPos start position of message or part in file (default: current position)
* - endPos end position of message or part in file (default: end of file)
*
* @param array $params full message with or without headers
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
*/
public function __construct(array $params)
{
if (empty($params['file'])) {
throw new Exception\InvalidArgumentException('no file given in params');
}
if (!is_resource($params['file'])) {
$this->_fh = fopen($params['file'], 'r');
} else {
$this->_fh = $params['file'];
}
if (!$this->_fh) {
throw new Exception\RuntimeException('could not open file');
}
if (isset($params['startPos'])) {
fseek($this->_fh, $params['startPos']);
}
$header = '';
$endPos = isset($params['endPos']) ? $params['endPos'] : null;
while (($endPos === null || ftell($this->_fh) < $endPos) && trim($line = fgets($this->_fh))) {
$header .= $line;
}
$this->_headers = Headers::fromString($header);
$this->_contentPos[0] = ftell($this->_fh);
if ($endPos !== null) {
$this->_contentPos[1] = $endPos;
} else {
fseek($this->_fh, 0, SEEK_END);
$this->_contentPos[1] = ftell($this->_fh);
}
if (!$this->isMultipart()) {
return;
}
$boundary = $this->getHeaderField('content-type', 'boundary');
if (!$boundary) {
throw new Exception\RuntimeException('no boundary found in content type to split message');
}
$part = array();
$pos = $this->_contentPos[0];
fseek($this->_fh, $pos);
while (!feof($this->_fh) && ($endPos === null || $pos < $endPos)) {
$line = fgets($this->_fh);
if ($line === false) {
if (feof($this->_fh)) {
break;
}
throw new Exception\RuntimeException('error reading file');
}
$lastPos = $pos;
$pos = ftell($this->_fh);
$line = trim($line);
if ($line == '--' . $boundary) {
if ($part) {
// not first part
$part[1] = $lastPos;
$this->_partPos[] = $part;
}
$part = array($pos);
} else {
if ($line == '--' . $boundary . '--') {
$part[1] = $lastPos;
$this->_partPos[] = $part;
break;
}
}
}
$this->_countParts = count($this->_partPos);
}
作者:nevvermin
项目:zf
/**
* Prepare header string from message
*
* @param Message $message
* @return string
*/
protected function prepareHeaders(Message $message)
{
$headers = new Headers();
foreach ($message->headers() as $header) {
if ('Bcc' == $header->getFieldName()) {
continue;
}
$headers->addHeader($header);
}
return $headers->toString();
}
作者:xamin12
项目:platfor
/**
* Gets an email importance
*
* @param Headers $headers
*
* @return integer
*/
protected function getImportance(Headers $headers)
{
$importance = $headers->get('Importance');
if ($importance instanceof HeaderInterface) {
switch (strtolower($importance->getFieldValue())) {
case 'high':
return 1;
case 'low':
return -1;
default:
return 0;
}
}
$labels = $headers->get('X-GM-LABELS');
if ($labels instanceof HeaderInterface) {
if ($labels->getFieldValue() === '\\\\Important') {
return 1;
}
} elseif ($labels instanceof \ArrayIterator) {
foreach ($labels as $label) {
if ($label instanceof HeaderInterface && $label->getFieldValue() === '\\\\Important') {
return 1;
}
}
}
return 0;
}
作者:gridguy
项目:zor
/**
* Compose headers
*
* @param \Zend\Mail\Headers $headers
* @return \Zork\Mail\Message
*/
public function setHeaders(Headers $headers)
{
$headers->addHeaderLine('User-Agent', static::USER_AGENT)->addHeaderLine('X-Mailer', static::USER_AGENT);
return parent::setHeaders($headers);
}
作者:thefo
项目:phpcha
public function mailAdd($version, $id, $srcNodeId, $srcUserNickname, $dstNodeId, $subject, $text, $checksum, $relayCount, $encryptionMode, $status, $timeCreated, $timeReceived)
{
$this->log->info('mail add: ' . $id);
$this->log->info('subject: ' . $subject);
$this->log->info('from: ' . $srcNodeId);
$this->log->info('nick: ' . $srcUserNickname);
$headers = new ZendMailHeaders();
$headers->addHeaderLine('Date', date('r', $timeReceived));
$headers->addHeaderLine('X-Version', $version);
$headers->addHeaderLine('X-Id', $id);
$headers->addHeaderLine('X-Checksum', $checksum);
$headers->addHeaderLine('X-RelayCount', $relayCount);
$headers->addHeaderLine('X-EncrptionMode', $encryptionMode);
$headers->addHeaderLine('X-Status', $status);
$headers->addHeaderLine('X-TimeCreated', $timeCreated);
$headers->addHeaderLine('X-TimeReceived', $timeReceived);
$message = new ZendMailMessage();
$message->setHeaders($headers);
$message->addFrom($srcNodeId . '@phpchat.fox21.at', $srcUserNickname);
$message->addTo($dstNodeId . '@phpchat.fox21.at');
$message->setSubject($subject);
$message->setBody($text);
$this->server->addMail($message);
}
作者:bradley-hol
项目:zf
/**
* @dataProvider expectedHeaders
*/
public function testDefaultPluginLoaderIsSeededWithHeaders($plugin, $class)
{
$headers = new Headers();
$loader = $headers->getPluginClassLoader();
$test = $loader->load($plugin);
$this->assertEquals($class, $test);
}
作者:ppiedaderawne
项目:concrete
/**
* split a message in header and body part, if no header or an
* invalid header is found $headers is empty
*
* The charset of the returned headers depend on your iconv settings.
*
* @param string|Headers $message raw message with header and optional content
* @param Headers $headers output param, headers container
* @param string $body output param, content of message
* @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
* @param bool $strict enable strict mode for parsing message
* @return null
*/
public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND, $strict = false)
{
if ($message instanceof Headers) {
$message = $message->toString();
}
// check for valid header at first line
$firstlinePos = strpos($message, "\n");
$firstline = $firstlinePos === false ? $message : substr($message, 0, $firstlinePos);
if (!preg_match('%^[^\\s]+[^:]*:%', $firstline)) {
$headers = [];
// TODO: we're ignoring \r for now - is this function fast enough and is it safe to assume noone needs \r?
$body = str_replace(["\r", "\n"], ['', $EOL], $message);
return;
}
// see @ZF2-372, pops the first line off a message if it doesn't contain a header
if (!$strict) {
$parts = explode(':', $firstline, 2);
if (count($parts) != 2) {
$message = substr($message, strpos($message, $EOL) + 1);
}
}
// find an empty line between headers and body
// default is set new line
if (strpos($message, $EOL . $EOL)) {
list($headers, $body) = explode($EOL . $EOL, $message, 2);
// next is the standard new line
} elseif ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) {
list($headers, $body) = explode("\r\n\r\n", $message, 2);
// next is the other "standard" new line
} elseif ($EOL != "\n" && strpos($message, "\n\n")) {
list($headers, $body) = explode("\n\n", $message, 2);
// at last resort find anything that looks like a new line
} else {
ErrorHandler::start(E_NOTICE | E_WARNING);
list($headers, $body) = preg_split("%([\r\n]+)\\1%U", $message, 2);
ErrorHandler::stop();
}
$headers = Headers::fromString($headers, $EOL);
}
作者:navassouz
项目:zf
/**
* split a message in header and body part, if no header or an
* invalid header is found $headers is empty
*
* The charset of the returned headers depend on your iconv settings.
*
* @param string|Headers $message raw message with header and optional content
* @param Headers $headers output param, headers container
* @param string $body output param, content of message
* @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
* @return null
*/
public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND)
{
if ($message instanceof Headers) {
$message = $message->toString();
}
// check for valid header at first line
$firstline = strtok($message, "\n");
if (!preg_match('%^[^\\s]+[^:]*:%', $firstline)) {
$headers = array();
// TODO: we're ignoring \r for now - is this function fast enough and is it safe to asume noone needs \r?
$body = str_replace(array("\r", "\n"), array('', $EOL), $message);
return;
}
// find an empty line between headers and body
// default is set new line
if (strpos($message, $EOL . $EOL)) {
list($headers, $body) = explode($EOL . $EOL, $message, 2);
// next is the standard new line
} else {
if ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) {
list($headers, $body) = explode("\r\n\r\n", $message, 2);
// next is the other "standard" new line
} else {
if ($EOL != "\n" && strpos($message, "\n\n")) {
list($headers, $body) = explode("\n\n", $message, 2);
// at last resort find anything that looks like a new line
} else {
@(list($headers, $body) = @preg_split("%([\r\n]+)\\1%U", $message, 2));
}
}
}
$headers = Headers::fromString($headers, $EOL);
}
作者:nevvermin
项目:zf
/**
* Prepare the textual representation of headers
*
* @param Message $message
* @return string
*/
protected function prepareHeaders(Message $message)
{
$headers = $message->headers();
// On Windows, simply return verbatim
if ($this->isWindowsOs()) {
return $headers->toString();
}
// On *nix platforms, strip the "to" header
$headersToSend = new Headers();
foreach ($headers as $header) {
if ('To' == $header->getFieldName()) {
continue;
}
$headersToSend->addHeader($header);
}
return $headersToSend->toString();
}