如何在joomla中的自定义模块中发送电子邮件?

时间:2017-02-05 01:21:11

标签: php joomla

我在joomla中创建了一个自定义模块,我想使用带有gmail凭据的SMTP从模块发送电子邮件。 我在下面使用了这段代码但是我收到错误“无法实例化邮件功能”。问题可能在哪里,我该如何解决?

<?php
if(isset($_POST['submit'])){
   $mailer = JFactory::getMailer();
   $config = JFactory::getConfig();
   $sender = array(
       $config->get( 'mailfrom' ),
       $config->get( 'fromname' )
   );

  $mailer->SMTPSecure = "tls";
  $mailer->SMTPDebug = 4;
  $mailer->Username = "elviskc091@gmail.com";
  $mailer->Password = "mypassword";
  $mailer->SMTPAuth = true;
  $mailer->Port = 587;

  $mailer->setSender($sender);

  $user = JFactory::getUser();
  $recipient = "elvis091@gmail.com";

  $mailer->addRecipient($recipient);

  $body   = '<h2>Our mail</h2>'
    . '<div>A message to our dear readers'
    . '<img src="cid:logo_id" alt="logo"/></div>';
  $mailer->isHtml(true);
  $mailer->Encoding = 'base64';
  $mailer->setBody($body);
  $mailer->AddEmbeddedImage( JPATH_COMPONENT.'/assets/logo128.jpg',     'logo_id', 'logo.jpg', 'base64', 'image/jpeg' );

 $send = $mailer->Send();
 if ( $send !== true ) {
    echo 'Error sending email: '.$send;
 } else {
    echo 'Mail sent';
 }
}
?>

0 个答案:

没有答案