我正在用symfony3构建一个应用程序,其中在SendGridService上有一个EmailService盆地。
可以发送电子邮件,但是我想安排我的电子邮件。
这是SendGridEmailService:
<?php
namespace AppBundle\Services;
use SendGrid;
use Swift_Attachment;
use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;
use Twig_Environment;
class SendGirdEmailService
{
/**
* Library to facilitate email messages being sent out, sendMail deprecated in symfony 1.2
*
* @param string $partial - Array with html and text partials ie array('text'=>'textPartial', 'html'=>'htmlPartial')
* @param array $parameters - Array we will pass into the partials
* @param string $mailFrom - Email source
* @param string $mailTo - Email destination
* @param string $subject - The subject of the email message
* @param array $sgHeaders - What we will be placing in the SMTPAPI header. Must be null or a non-empty array
* @param array $attachments - Email contains the attachments
*/
public static function sendEmail($partials, $parameters, $mailFrom, $mailTo, $subject, $sgHeaders = null, $attachments = null)
{
// verify we have username/password to send out emails - IMPORTANT
/* if (!sfconfig::has('app_sendgrid_username') or !sfconfig::has('app_sendgrid_password')) {
throw new sfException('SMTP username/password is required to send email out');
}*/
$text = null;
$html = null;
if (is_array($partials)) {
// load libraries
//sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
if (isset($partials['text'])) {
$text = $partials['text'];
}
if (isset($partials['html'])) {
$html = $partials['html'];
}
}
if ($text === null and $html === null) {
throw new sfException('A text and/or HTML partial must be given');
}
try {
/*
* Load connection for mailer
*/
$connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl')->setUsername('xxxxxx')->setPassword('xxxxxxx');
// setup connection/content
$mailer = Swift_Mailer::newInstance($connection);
$message = Swift_Message::newInstance()->setSubject($subject)->setTo($mailTo);
if ($text and $html) {
$message->setBody($html, 'text/html');
$message->addPart($text, 'text/plain');
} else if ($text) {
$message->setBody($text, 'text/plain');
} else {
$message->setBody($html, 'text/html');
}
// if contains SMTPAPI header add it
if (null !== $sgHeaders) {
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode($sgHeaders));
}
// update the from address line to include an actual name
if (is_array($mailFrom) and count($mailFrom) == 2) {
$mailFrom = array(
$mailFrom['email'] => $mailFrom['name']
);
}
// add attachments to email
if ($attachments !== null and is_array($attachments)) {
foreach ($attachments as $attachment) {
$attach = Swift_Attachment::fromPath($attachment['file'], $attachment['mime'])->setFilename($attachment['filename']);
$message->attach($attach);
}
}
// Send
$message->setFrom($mailFrom);
$mailer->send($message);
}
catch (Exception $e) {
throw new sfException('Error sending email out - ' . $e->getMessage());
}
}
}
这是我用来发送电子邮件的功能:
SendGirdEmailService::sendEmail(array(
'text' => $htmlContent,
'html' => $htmlContent
),
null,
$this->from,
$to,
$subject,
$sgHeaders = null,
$attachments = null
);
例如,如何设置一小时后发送电子邮件的参数?
答案 0 :(得分:0)
我找到了解决方案,方法是设置一个带有'sent_at'索引的数组,其时间戳值像:
SendGirdEmailService::sendEmail(array(
'text' => $htmlContent,
'html' => $htmlContent,
),
null,
$this->from,
$to,
$subject,
$sgHeaders =
array('send_at' => strtotime('+1 day', date_timestamp_get(new \DateTime()) ) ),
$attachments = null
);