Here below you can find some examples showing how to send an email using SMTP Relay (SMTP+) from your client application.
The code examples below are "as is", without warranty of any kind. MailUp shall not be held liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or other related to the example code. Please contact us if you are interested in submitting examples for further programming languages.
Basic sample
This is a PHP snippet that requires PHPMailer and the credentials of a SMTP+ user.
<?php //######################################################## //Include the PHPmailer class. // //PHPmailer allows you to create and transport an email an email message. //The PHPmailer class is available at this address: //https://github.com/PHPMailer/PHPMailer/ //######################################################## require_once "class.phpmailer.php"; $EmailMessage = new PHPmailer(); $EmailMessage->SetLanguage('en','language/'); //Define the language //********************************************** //SMTP configuration //********************************************** $EmailMessage->IsSMTP(); //Specify usage of SMTP Server $EmailMessage->Host = "in.smtpok.com"; //SMTP+ Server address $EmailMessage->Port="25"; //SET the SMTP Server port $EmailMessage->Username = "smtpplus_username"; //SMTP+ authentication: username $EmailMessage->Password = "smtpplus_password"; //SMTP+ authentication: password $EmailMessage->SMTPAuth = true; //Authentication required //********************************************** //Email data //********************************************** $EmailMessage->IsHTML(true); //Set the email format $EmailMessage->FromName= "myname"; //From: display name $EmailMessage->From="sender_address@domain.com"; //From: email address $EmailMessage->AddAddress("recipient_address@domail.com"); //Add one or more recipients $EmailMessage->Subject="My first email sent via SMTP+"; //Set the email subject $EmailMessage->Body="<b>Hello,<b><br/>This is my first email."; //Set the email body $EmailMessage->AltBody="Hello, This is my first email.."; //Set the email text part //********************************************** //Send the email //********************************************** if(!$EmailMessage->Send()) { echo "<h1>Error sending the email.</h1>"; //Email was not sent } else { echo "Email has been sent"; //Mail sent } ?>
Advanced topics
You can take advantage of more powerful features by using "fast.smtp.ok" header and "X-SMTPAPI" custom header. X-SMTPAPI custom header allows you to specify additional parameters through a JSON object.
Go faster
Use fast.smtpok.com instead of in.smtpok.com
// switching to fast.smtpok.com leads to a terrific reduction of latency time $EmailMessage->Host = "fast.smtpok.com"; //SMTP+ Server address
Use dynamic fields
Add placeholders (always lowercase between square brackets) referring to personal data fields that are defined for your MailUp account. Substitution occurs for subscribed recipients and when recipients' personal fields are valued, otherwise empty values will be displayed in place of the placeholder.
// Placeholder are replaced at sending time by empty values when no value was previously added for the recipient. // No action occurs when the specified placeholder does not correspond to a personal data field for that account. $EmailMessage->Subject="Hello [firstname], your order has been placed"; $EmailMessage->Body="<b>Hello [firstname], your order [latestorderid] has been sent on [latestshippedorderdate].<b><br/> Please find enclosed your invoice <b><br/>. Kind regards";
Use merge tags
Merge tags allow you to reuse a message template (i.e. the same subject and body) and customize it with key-value pairs that are specified through message custom headers.
// Placeholder are replaced at sending time by empty values when no value was previously added for the recipient. // No action occurs when the specified placeholder does not correspond to a personal data field for that account. $EmailMessage->Subject="Recommended books for [customername]"; //Set the email subject $EmailMessage->Body="<b>Hello [customername], we recommend you [title1] by [author1]. Buy now at [price1].<b><br/> Please find enclosed your invoice <b><br/>. Kind regards"; //Set the email body $EmailMessage->AddCustomHeader("X-SMTPAPI: {\"DynamicFields\":[{\"N\":\"customername\",\"V\":\"John\"},{\"N\":\"title1\",\"V\":\"Become an email guru\"},{\"N\":\"price1\",\"V\":\"$10.99\"},{\"N\":\"author1\",\"V\":\"Martha G.\"}]}");
Apply custom aggregation using Campaign Code
"CampaignCode" parameter allows client application to customize aggregation when none of the available options suits the desired behaviour.
Example: if you aggregated either by subject and monthly or by a specific keyword that is inside the message subject, there would be no way to meet your needs with aggregations that can be selected on the admin console.
An effective workaround solution consists of letting the user to specify a code within message header and force aggregation based on that code (e.g. all the emails with CampaignCode="Code001" inside the custom header
X-SMTPAPI will be handled as different sending of the same message). An additional parameter called "CampaignName" can be used to specify a name to be displayed on admin console as message name instead of "CampaignCode" value ("CampaignName" is ignored if "CampaignCode" is not specified)
// All the emails with CampaignCode = "Code001" will be treated as different sending of the same message // Message name ("subject" field) on admin console is set by the CampaignName of the first call with CampaignCode = "Code001". CampaignName of further calls with the same Campaign code is ignored. $EmailMessage->AddCustomHeader("X-SMTPAPI: {\"CampaignName\":\"Custom Aggregation 001\",\"CampaignCode\":\"Code001\"}");
Schedule a deferred sending
You can specify date and time (with time zone) of sending. Email is sent immediately when specified date and time occurs in the past
// Schedule sending on Feb 26th 2015 at 15:55 UTC+2 $EmailMessage->AddCustomHeader("X-SMTPAPI: {\"Schedule\":\"2015-02-26T15:55+02:00\"}");
Combine more options
Use commas as separator for the X-SMTPAPI parameters that you want to set.
// Schedule sending + Set Campaign Name & code $EmailMessage->AddCustomHeader("X-SMTPAPI: {\"CampaignName\":\"Custom Aggregation 001\",\"CampaignCode\":\"Code001\",\"Schedule\":\"2015-02-26T15:55+02:00\"}");
Here below you can find an example of how you can send an email using SMTP Relay (SMTP+) from your client application.
It is a PHP snippet that requires PHPMailer and the credentials of a SMTP+ user.
<?php //######################################################## //Include the PHPmailer class. // //PHPmailer allows you to create and transport an email an email message. //The PHPmailer class is available at this address: //https://github.com/PHPMailer/PHPMailer/ //######################################################## require_once "class.phpmailer.php"; $EmailMessage = new PHPmailer(); $EmailMessage->SetLanguage('en','language/'); //Define the language //********************************************** //SMTP configuration //********************************************** $EmailMessage->IsSMTP(); //Specify usage of SMTP Server $EmailMessage->Host = "in.smtpok.com"; //SMTP+ Server address $EmailMessage->Port="25"; //SET the SMTP Server port $EmailMessage->Username = "smtpplus_username"; //SMTP+ authentication: username $EmailMessage->Password = "smtpplus_password"; //SMTP+ authentication: password $EmailMessage->SMTPAuth = true; //Authentication required //********************************************** //Email data //********************************************** $EmailMessage->IsHTML(true); //Set the email format $EmailMessage->FromName= "myname"; //From: display name $EmailMessage->From="sender_address@domain.com"; //From: email address $EmailMessage->AddAddress("recipient_address@domail.com"); //Add one or more recipients $EmailMessage->Subject="My first email sent via SMTP+"; //Set the email subject $EmailMessage->Body="<b>Hello,<b><br/>This is my first email."; //Set the email body $EmailMessage->AltBody="Hello, This is my first email.."; //Set the email text part //********************************************** //Send the email //********************************************** if(!$EmailMessage->Send()) { echo "<h1>Error sending the email.</h1>"; //Email was not sent } else { echo "Email has been sent"; //Mail sent } ?>