Custom Email Settings

From NeuCart Documentation
Jump to: navigation, search

The Custom Email Settings feature is a custom hook that is used to add server-specific parameters to the email object. Typically, this hook is used when SMTP authorization is required; this file allows the username and password to be safely added to your code.

Source code project 1171.svg
This article discusses
a custom hook that is
provided by the system.
Binary-icon.png
This article discusses a
technical topic that may not be
intended for all readers.

Code

<?php
function configure_email_params( $mail ) { // don't remove this line or the one above it.
	/*
		// some documentation from https://github.com/PHPMailer/PHPMailer
		$mail->isSMTP();                                      // Set mailer to use SMTP
		$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
		$mail->SMTPAuth = true;                               // Enable SMTP authentication
		$mail->Username = 'jswan';                            // SMTP username
		$mail->Password = 'secret';                           // SMTP password
		$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

	---------------------------------------------------------------------------------------------
	-- STORE OWNER: if you're using SMTP auth stuff, remove or comment the word "return" below --
	---------------------------------------------------------------------------------------------
	*/
	return;
	
	// map the email to a username and password
	$email_map = array();

	/* 
	--------------------------------------------------------------------------------------------
	-- STORE OWNER: list the username/password, per address, in the bob@example format below. --
	-- Or if the same authentication is used for all email addresses, create a star like this
	   $email_map[ '*' ] = array( 'smtp_username', 'smtp_password' );
	--------------------------------------------------------------------------------------------
	*/
	$email_map[ 'bob@example.com' ] = array( 'smtp_username', 'smtp_password' );
	
	// neucart logic below.
	$from = $mail->From;
	
	if ( isset( $email_map[ $from ] ) || isset( $email_map[ '*' ] ) ) {
		$arr = isset( $email_map[ '*' ] ) ? $email_map[ '*' ] : $email_map[ $from ];
		
		if ( !is_array( $arr ) || sizeof( $arr ) != 2 )
			return; // don't know what to do.
	
		$mail->isSMTP();
		$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
		$mail->Port = 465;
		$mail->SMTPAuth = true;
		$mail->Username = $arr[ 0 ];
		$mail->Password = $arr[ 1 ];	
		$mail->SMTPSecure = 'ssl';	
	}
	
	// any other logic below, for example the host.
	
} // don't remove this line or the one below it.
?>

Usage

Unlike most custom hooks provided by NeuCart, this function is intended to be used mostly as-is, where the store administrator simply adds code to the appropriate areas. (Most custom hooks provide a blank function.) In the example above, the blue text is relevant to the discussion.

The idea behind this script is to match the "from" address of the mail object to the appropriate SMTP username and password. Within NeuCart, every email address is configurable; for example, a store owner can configure the email address that sends notifications to the store, the email address that sends confirmation emails, and more.

Within this custom hook, a store administrator would add a list of email addresses configured with the username and password of that user. For example:

$email_map[ 'bob@example.com' ]                = array( 'smtp_username', 'smtp_password' );
$email_map[ 'order_notification@example.com' ] = array( 'OrderNotifier', 'somePassword123' );
$email_map[ 'store_notifier@example.com' ]     = array( 'StoreNotifier', 'anotherAbcPw' );

Notes about the other important lines highlighted above:

  • If using this hook, the "return" line should be removed or commented. This line exists because all email functions call this custom hook, but this return line prevents any of the logic from executing if the script has not been modified.
  • The main and backup hosts should be configured as desired.

What values should be used? When is this script needed?

These are questions only your web host or system administrator can answer. NeuCart has simply provided a way to create the necessary configuration.

See also

Need a question answered about the Custom Email Settings article? Want to offer a suggestion or correction? Click here to discuss this page.