/**
* Function to create a mail object for futher use (uses phpMailer, smtp or sendmail depending on global config)
*
* @param  string  $from      From e-mail address
* @param  string  $fromname  From name
* @param  string  $subject   E-mail subject
* @param  string  $body      Message body
* @return CBPHPMailer        Mail object
*/
function &comprofilerCreateMail($from = '', $fromname = '', $subject, $body)
{
    global $_CB_framework;
    cbimport('phpmailer.phpmailer');
    $mail = new CBPHPMailer();
    $mail->PluginDir = $_CB_framework->getCfg('absolute_path') . '/administrator/components/com_comprofiler/library/phpmailer/';
    $mail->SetLanguage('en', $_CB_framework->getCfg('absolute_path') . '/administrator/components/com_comprofiler/library/phpmailer/language/');
    $mail->CharSet = $_CB_framework->outputCharset();
    $mail->IsMail();
    $mail->From = $from ? $from : $_CB_framework->getCfg('mailfrom');
    if ($mail->From == '' || $mail->From == 'registration@whatever') {
        $mail->From = $_CB_framework->getCfg('mailfrom');
    }
    $mail->FromName = $fromname ? $fromname : $_CB_framework->getCfg('fromname');
    $mail->Mailer = $_CB_framework->getCfg('mailer');
    if ($_CB_framework->getCfg('mailer') == 'smtp') {
        // Add smtp values:
        $mail->SMTPAuth = $_CB_framework->getCfg('smtpauth');
        $mail->Username = $_CB_framework->getCfg('smtpuser');
        $mail->Password = $_CB_framework->getCfg('smtppass');
        $mail->Host = $_CB_framework->getCfg('smtphost');
        $smtpport = (int) trim($_CB_framework->getCfg('smtpport'));
        if ($smtpport) {
            $mail->Port = $smtpport;
        }
        $smtpsecure = $_CB_framework->getCfg('smtpsecure');
        if ($smtpsecure === 'ssl' || $smtpsecure === 'tls') {
            $mail->SMTPSecure = $smtpsecure;
        }
    } elseif ($_CB_framework->getCfg('mailer') == 'sendmail') {
        // Set sendmail path:
        if ($_CB_framework->getCfg('sendmail')) {
            $mail->Sendmail = $_CB_framework->getCfg('sendmail');
        }
    }
    // If email domain matches sub-part of site domain, we can safely set the sender header to lower risk of valid registration mails being flagged as spam:
    $email_parts = explode('@', $mail->From);
    if (count($email_parts) > 1) {
        $email_domain = array_pop($email_parts);
        $urlParts = parse_url($_CB_framework->getCfg('live_site'));
        if ($email_domain && stripos($urlParts['host'], $email_domain) !== false) {
            $mail->Sender = $mail->From;
        }
    }
    $mail->Subject = $subject;
    $mail->Body = $body;
    return $mail;
}
/**
* Function to create a mail object for futher use (uses phpMailer, smtp or sendmail depending on global config)
*
* @param  string  $from      From e-mail address
* @param  string  $fromname  From name
* @param  string  $subject   E-mail subject
* @param  string  $body      Message body
* @return CBPHPMailer        Mail object
*/
function & comprofilerCreateMail( $from = '', $fromname = '', $subject, $body ) {
	global $_CB_framework;

	cbimport( 'phpmailer.phpmailer');
	$mail					=	new CBPHPMailer();

	$mail->PluginDir		=	$_CB_framework->getCfg('absolute_path') .'/administrator/components/com_comprofiler/library/phpmailer/';
	$mail->SetLanguage( 'en', $_CB_framework->getCfg('absolute_path') . '/administrator/components/com_comprofiler/library/phpmailer/language/' );
	$mail->CharSet 			=	$_CB_framework->outputCharset();
	$mail->IsMail();
	$mail->From				=	$from ? $from : $_CB_framework->getCfg( 'mailfrom' );
	if ( ( $mail->From == '' ) || ( $mail->From == 'registration@whatever' ) ) {
		$mail->From			=	$_CB_framework->getCfg( 'mailfrom' );
	}
	$mail->FromName			=	$fromname ? $fromname : $_CB_framework->getCfg( 'fromname' );
	$mail->Mailer 			=	$_CB_framework->getCfg( 'mailer' );

	if ( $_CB_framework->getCfg( 'mailer' ) == 'smtp' ) {
		// Add smtp values:
		$mail->SMTPAuth		=	$_CB_framework->getCfg( 'smtpauth' );
		$mail->Username		=	$_CB_framework->getCfg( 'smtpuser' );
		$mail->Password		=	$_CB_framework->getCfg( 'smtppass' );
		$mail->Host			=	$_CB_framework->getCfg( 'smtphost' );
		$smtpport			=	(int) trim( $_CB_framework->getCfg( 'smtpport' ) );
		if ( $smtpport ) {
			$mail->Port		=	$smtpport;
		}
		$smtpsecure			=	$_CB_framework->getCfg( 'smtpsecure' );
		if ( ( $smtpsecure === 'ssl' ) || ( $smtpsecure === 'tls' ) ) {
			$mail->SMTPSecure	=	$smtpsecure;
		}
		$mail->Sender		=	$mail->From	;
	} elseif ( $_CB_framework->getCfg( 'mailer' ) == 'sendmail' ) {
		// Set sendmail path:
		if ( $_CB_framework->getCfg( 'sendmail' ) ) {
			$mail->Sendmail	=	$_CB_framework->getCfg( 'sendmail' );
			$mail->Sender	=	$mail->From;
		}
	}

	$mail->Subject 			=	$subject;
	$mail->Body 			=	$body;

	return $mail;
}