Simple SugarCRM E-mail Wrapper

This is a little helper function I wrote to send an email using Sugar CRM’s built in mail class.

/**
	 * A wrapper for sugar's mail class
	 * Sends emails to everyon in $tos array.
	 * $tos['user name'] = 'username@domain.com';
	 *
	 * @example : 	$tos['Some User'] = 'SomeUser@somewhere.com';
	 * 				$tos['Some User 2'] = 'SomeUser2@somewhere.com';
	 * 				sendSugarPHPMail($tos, 'hi', 'hello fellas');
	 *
	 * @param associative array
	 * @param string $subject
	 * @param string $body
	 * @return boolean
	 */
	function sendSugarPHPMail($tos, $subject, $body){

		require_once('include/SugarPHPMailer.php');
		require_once('modules/Administration/Administration.php');

		$mail = new SugarPHPMailer();
		$admin = new Administration();
		$admin->retrieveSettings();

		if ($admin->settings['mail_sendtype'] == "SMTP") {
			$mail->Host = $admin->settings['mail_smtpserver'];
			$mail->Port = $admin->settings['mail_smtpport'];

			if ($admin->settings['mail_smtpauth_req']) {
				$mail->SMTPAuth = TRUE;
				$mail->Username = $admin->settings['mail_smtpuser'];
				$mail->Password = $admin->settings['mail_smtppass'];
			}

			$mail->Mailer   = "smtp";
			$mail->SMTPKeepAlive = true;

		}else{
			$mail->mailer = 'sendmail';
		}

		$mail->From     = $admin->settings['notify_fromaddress'];
		$mail->FromName = $admin->settings['notify_fromname'];
		$mail->ContentType = "text/html"; //"text/plain"

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

		foreach ($tos as $name => $address){
			$mail->AddAddress("{$address}", "{$name}");
		}

		if (!$mail->send()) {
			$GLOBALS['log']->info("sendSugarPHPMail - Mailer error: " . $mail->ErrorInfo);
			return false;
 		}else{
 			return true;
 		}
	}

4 Responses to “Simple SugarCRM E-mail Wrapper”

  1. Jon Hermiz says:

    Any sugar developers this code will not work for several reasons. First off change this:

    foreach ($tos as $name => $address){
    $mail->AddAddress(“{$address}”, “{$name}”);
    }

    to this:

    foreach ($to as $name => $address){
    $mail->AddAddress(“{$address}”, “{$name}”);
    }

    (Get rid of the s in $tos make it $to. Also get rid of the required_once functions, they are not needed. The logic hook file is being included in the controller where all needed common files are already included. Once you do this it works fine. I know this post was fairly old but I needed to do something similiar and found this. Otherwise EXCELLENT job by the author…

    Thanks!

  2. Ashwin Surajbali says:

    Thanks, however $tos is the array of email addresses being passed in. The requires are there because it was written as a standalone outside of sugar…but you’re right. Thanks for the input :)

  3. Anonymous says:

    Hello,

    Great job. But I’m new in using & developping with sugarcrm. By adding this code in mail class do you mean in the modules/email/email.php”?

    Thx a lot!

  4. Anonymous says:

    You can add this function where ever you please. It’s just a function…just include it in your code somewhere.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>