Custom headers with Swiftmail


(OscarFabian) #1

Hi there.

I’m using yii 2 basic application and I’m doing a console function that will be executed by a cron.

I get this error when try send mails

PHP Fatal error: Uncaught Error: Call to undefined method Swift_Mime_Headers_UnstructuredHeader::getParameter() in /var/www/html/Projects/Fabrica/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/SimpleMimeEntity.php:606

Stack trace:

#0 /var/www/html/Projects/Fabrica/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/MimePart.php(77): Swift_Mime_SimpleMimeEntity->_getHeaderParameter(‘Content-Type’, ‘charset’)
#1 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2-swiftmailer/Message.php(229): Swift_Mime_MimePart->getCharset()
#2 /var/www/html/Projects/Fabrica/vendor/yiisoft/yii2-swiftmailer/Message.php(213): yii\swiftmailer\Message->setBody(NULL, ‘text/html’)
#3 /var/www/html/Projects/Fabrica/components/MyComponent.php(211): yii\swiftmailer\Message->setHtmlBody(NULL)
#4 /var/www/html/Projects/Fabrica/components/MyComponent.php(509): app\components\MyComponent->sendMail(‘smtp.gmail.com’, ‘info@fabrica.com’, ‘test02’, ‘’, Array, ‘usuariotest…’ in /var/www/html/Projects/Fabrica/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/SimpleMimeEntity.php on line 606

My code

$headers = array(
        "MIME-Version" => "1.0",
        "From" => $adminEmail,
        "Reply-To" => $adminEmail,
        "Content-Type" => "multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"",
     );

$messages = \Yii::$app->mailer->compose()
        ->setFrom($from)
        ->setTo($to)
        ->setSubject($subject)
        ->setHeaders($header)
        ->setHtmlBody($text_html)
        ->setTextBody($text_mail)
        ->send();