How to send mail in extbase typo3 || How to send mail using swiftmailer in extbase typo3


Here i am going to show you for how to send mail in Extbase fluid – typo3

/**
 * send a mail with build-in swiftmailer
 * @param \mixed $to array(key1 => array('email' => 'jainish@domain.com', 'name' => 'Jainish'), key2 => array('email' => 'info@domain.com', 'name' => 'Admin')) or just a string with the email-address
 * @param \string $subject
 * @param \string $html
 * @param \string $plain
 * @param \string $fromEmail
 * @param \string $fromName
 * @param \string $replyToEmail
 * @param \string $replyToName
 * @param \string $returnPath
 * @param \array $attachements array('path/to/file-1.suffix' => 'Name Of File-1', 'path/to/file-2.suffix' => 'Name Of File-2', 'path/to/filen.suffix' => 'Name Of File N')
 * @return \boolean true, if mail should be send - false, if parameter errors are given
 */
 
public static function sendMail($to, $subject, $html, $plain, $fromEmail = '', $fromName = '', $replyToEmail = '', $replyToName = '', $returnPath = '', $attachements = array()) {

  // Make instance of swiftmailer
  $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
   
  // From
  if ($fromEmail) {
	$message->setFrom(array($fromEmail => $fromName));
  }
 
  // To
  $recipients = array();
  if (is_array($to)) {
	foreach ($to as $pair) {
	  if (\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($pair['email'], false)) {
		if (trim($pair['name'])) {
		  $recipients[$pair['email']] = $pair['name'];
		} else {
		  $recipients[] = $pair['email'];
		}
	  }
	}
  } else {
	$recipients[] = $to;
  }
 
  if (!count($recipients)) {
	return false;
  }
 
  $message->setTo($recipients);
 
  // Subject
  $message->setSubject($subject);
 
  // HTML
  $message->setBody($html, 'text/html', 'utf-8');
 
  // Plain
  if ($plain) {
	$message->addPart($plain, 'text/plain', 'utf-8');
  }
 
  // Return Path
  if (trim($returnPath)) {
	$message->setReturnPath($returnPath);
  }
 
  // Reply To
  if (trim($replyToEmail) && \TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($replyToEmail)) {
	if (trim($replyToName)) {
	  $message->setReplyTo(array($replyToEmail => $replyToName));
	} else {
	  $message->setReplyTo(array($replyToEmail));
	}
  }
 
  // Attachements
  if (count($attachements)) {
	foreach ($attachements as $file => $name) {
	  if (file_exists($file)) {
		if (trim($name)) {
		  $message->attach(\Swift_Attachment::fromPath($file)->setFilename($name));
		} else {
		  $message->attach(Swift_Attachment::fromPath($file));
		}
	  }
	}
  }
 
  // Mail Send
  $message->send();
 
  return true;
}

If mail successfully sent then it will return 1 otherwise its return 0

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Advertisements

One thought on “How to send mail in extbase typo3 || How to send mail using swiftmailer in extbase typo3

  1. Pingback: How to call mail template in extbase typo3 || How to call template in extbase extension of typo | Typo3 and PHP Techie Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s