WordPress examples
Sending in E-mail
Sending inquiry attached to an E-mail from WordPress
It is a very simple 1 file example. You have to adjust it regarding your form, webhooks and your application.
- Install these packages with composer:
$ composer require league/uri $ composer require league/uri-components $ composer require symfony/mailer $ composer require symfony/google-mailer
- Create a new file in your WP root directory called
justimmo_inquiry.php
with the following content:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use League\Uri\Components\Query;
use League\Uri\UriTemplate;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
// this email address is defined by Justimmo
$feedbackEmail = 'ask-it-from-the-support@muster-justimmo.at';
// this is the folder where the logic saves the generated XML files
// if there were any issues with the sending then you can send them later again
$feedbackXMLFilesFolderPath = __DIR__ . '/../openimmo_feedbacks/';
// you need to use a WP webhook to pass the data from the Contact Form to this logic
// for example: https://musterimmo.at/justimmo_inquiry.php?realtyId=999999999&salutationId=1&title=Dr.&firstName=John&lastName=Doe&...
// get the arguments from the url that you passed through your local WP webhook - in this example we use League\Uri packages
$template = 'https://musterimmo.at/justimmo_inquiry.php{?realtyOoId,salutation,title,firstName,lastName,email,message,phone,fax,mobile,street,zipCode,city,country,categories}';
$uriTemplate = new UriTemplate($template, []);
$uri = $uriTemplate->expand(getUrlQueryParameters());
$query = Query::createFromRFC3986($uri->getQuery(), '&');
$realtyOoId = $query->params('realtyOoId'); // it has to be the same like in Justimmo: OBGC2................
$salutation = $query->params('salutation');
$title = $query->params('title');
$firstName = $query->params('firstName');
$lastName = $query->params('lastName');
$email = $query->params('email');
$message = $query->params('message');
$phone = $query->params('phone');
$fax = $query->params('fax');
$mobile = $query->params('mobile');
$street = $query->params('street');
$zipCode = $query->params('zipCode');
$city = $query->params('city');
$country = $query->params('country');
//$categories = $query->params('categories');
// don't send the inquiry without these mandatory data!
if ($realtyId === null || $email === null || $message === null) {
die('invalid request');
}
// don't forget to clean up and validate the values before you add them to the XML!
// acquire the other necessary variables from your database
$senderName = 'Max Musterman Immobilien';
$openImmoAnid = 'ABGC2................'; // you can get it from Justimmo support
$date = date('Y-m-d');
$tenantId = 99999; // optional
$portalId = 99999; // optional
$providerId = '9999_OBGC2................';
$exposeUrl = 'https://musterimmo.at/immobilien/999/expose';
$description = 'Schöne Wohnung mit Potenzial';
// build the XML. It has to be a valid Openimmo feedback XML
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><openimmo_feedback/>');
$xml->addChild('version', '1.2.5');
$sender = $xml->addChild('sender');
$sender->addChild('name', $senderName);
$sender->addChild('openimmo_anid', $openImmoAnid);
$sender->addChild('datum', $date);
$sender->addChild('makler_id', $tenantId);
$object = $xml->addChild('objekt');
$object->addChild('portal_unique_id', $portalId);
$object->addChild('oobj_id', $realtyOoId);
$object->addChild('anbieter_id', $providerId);
$object->addChild('expose_url', $exposeUrl);
$object->addChild('bezeichnung', $description);
$contact = $object->addChild('interessent');
$contact->addChild('anrede', $salutation);
$contact->addChild('vorname', $firstName);
$contact->addChild('nachname', $lastName);
$contact->addChild('strasse', $street);
$contact->addChild('plz', $zipCode);
$contact->addChild('ort', $city);
$contact->addChild('email', $email);
$contact->addChild('tel', $phone);
$contact->addChild('fax', $fax);
$contact->addChild('mobil', $mobile);
$contact->addChild('anfrage', $message);
// save the XML on your server with unique naming
$feedbackFileName = date('Y-m-d-His') . '-' . $objectOpenimmoId . '.xml';
$feedbackFilePath = $feedbackXMLFilesFolderPath . $feedbackFileName;
$xml->saveXML($feedbackFilePath);
// send the feedback attached to an email
// in this example we used Symfony Mailer: https://symfony.com/doc/current/mailer.html
$email = (new Email())
->from('noreply@musterimmo.at')
->to($feedbackEmail)
//->cc('oi-feedbacks@musterimmo.at') // if you want to get a copy
->subject('Musterimmo-Anfrage an OpenImmo Objekt #' . $objectOpenimmoId)
->text(strip_tags($message))
->html($message);
// attach the XML
$email
->attachFromPath($feedbackFilePath);
// don't forget to add the 'Envelope-to' tag to the headers!
$email
->getHeaders()
->addTextHeader('Envelope-to', $feedbackEmail);
// setup the transport and the mailer
$transport = Transport::fromDsn('smtp://localhost:1025');
//$transport = Transport::fromDsn('gmail+smtp://USERNAME:PASSWORD@default');
$mailer = new Mailer($transport);
$mailer->send($email);
exit();
/**
* it is just a very simple example, and it is not secure to use it on live server!!!
*
* @return array
*/
function getUrlQueryParameters(): array
{
$uri = $_SERVER['REQUEST_URI'];
parse_str(parse_url($uri, PHP_URL_QUERY), $queryArray);
$queryArray = array_map('trim', $queryArray);
return array_filter($queryArray, fn($value) => !is_null($value) && $value !== '');
}
The XML logic above will result an output like this
<?xml version="1.0" encoding="UTF-8"?>
<openimmo_feedback>
<version>1.2.5</version>
<sender>
<name>Max Musterman Immobilien</name>
<openimmo_anid>ABGC2................</openimmo_anid>
<datum>2022-01-04</datum>
<makler_id>9999</makler_id>
</sender>
<objekt>
<portal_unique_id>9999</portal_unique_id>
<oobj_id>OBGC2................</oobj_id>
<anbieter_id>999_OBGC2................</anbieter_id>
<expose_url>https://musterimmo.at/immobilien/999/expose</expose_url>
<bezeichnung>Schöne Wohnung mit Potenzial</bezeichnung>
<interessent>
<anrede/>
<vorname>Max</vorname>
<nachname>Mustermann</nachname>
<strasse>Hauptstrasse</strasse>
<plz>1111</plz>
<ort>Wien</ort>
<email>max@musterimmo.at</email>
<tel/>
<fax/>
<mobil>4365555555555</mobil>
<anfrage>Ich will folgendes über das Objekt wissen..</anfrage>
</interessent>
</objekt>
</openimmo_feedback>
and the generated email looks like this
From: noreply@musterimmo.at
To: ask-it-from-the-support@muster-justimmo.at
Subject: Musterimmo-Anfrage an OpenImmo Objekt #OBGC2................
Envelope-to: ask-it-from-the-support@muster-justimmo.at
Message-ID: <11635acb69e2ef0b00908fffc53defef@musterimmo.at>
MIME-Version: 1.0
Date: Wed, 05 Jan 2022 18:13:51 +0100
Content-Type: multipart/mixed; boundary=KRdavuDN
--KRdavuDN
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Ich will folgendes =C3=BCber das Objekt wissen..
--KRdavuDN
Content-Type: application/xml;
name=2022-01-05-181351-OBGC2.................xml
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
name=2022-01-05-181351-OBGC2.................xml;
filename=2022-01-05-181351-OBGC2.................xml
PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPG9wZW5pbW1vX2ZlZWRiYWNr
Pjx2ZXJzaW9uPjEuMi41PC92ZXJzaW9uPjxzZW5kZXI+PG5hbWU+TWF4IE11c3Rlcm1hbiBJbW1v
YmlsaWVuPC9uYW1lPjxvcGVuaW1tb19hbmlkPkFCR0MyLi4uLi4uLi4uLi4uLi4uLjwvb3Blbmlt
bW9fYW5pZD48ZGF0dW0+MjAyMi0wMS0wNTwvZGF0dW0+PG1ha2xlcl9pZD45OTk5PC9tYWtsZXJf
aWQ+PC9zZW5kZXI+PG9iamVrdD48cG9ydGFsX3VuaXF1ZV9pZD45OTk5PC9wb3J0YWxfdW5pcXVl
X2lkPjxvb2JqX2lkPk9CR0MyLi4uLi4uLi4uLi4uLi4uLjwvb29ial9pZD48YW5iaWV0ZXJfaWQ+
OTk5X09CR0MyLi4uLi4uLi4uLi4uLi4uLjwvYW5iaWV0ZXJfaWQ+PGV4cG9zZV91cmw+aHR0cHM6
Ly9tdXN0ZXJpbW1vLmF0L2ltbW9iaWxpZW4vOTk5L2V4cG9zZTwvZXhwb3NlX3VybD48YmV6ZWlj
aG51bmc+U2Now7ZuZSBXb2hudW5nIG1pdCBQb3RlbnppYWw8L2JlemVpY2hudW5nPjxpbnRlcmVz
c2VudD48YW5yZWRlLz48dm9ybmFtZT5NYXg8L3Zvcm5hbWU+PG5hY2huYW1lPk11c3Rlcm1hbm48
L25hY2huYW1lPjxzdHJhc3NlPkhhdXB0c3RyYXNzZTwvc3RyYXNzZT48cGx6PjExMTE8L3Bsej48
b3J0PldpZW48L29ydD48ZW1haWw+bWF4QG11c3RlcmltbW8uYXQ8L2VtYWlsPjx0ZWwvPjxmYXgv
Pjxtb2JpbD40MzY1NTU1NTU1NTU1PC9tb2JpbD48YW5mcmFnZT5JY2ggd2lsbCBmb2xnZW5kZXMg
w7xiZXIgZGFzIE9iamVrdCB3aXNzZW4uLjwvYW5mcmFnZT48L2ludGVyZXNzZW50Pjwvb2JqZWt0
Pjwvb3BlbmltbW9fZmVlZGJhY2s+Cg==
--KRdavuDN--
With Justimmo PHP-SDK
Sending inquiry using Justimmo PHP-SDK through local WordPress webhook
- Install Justimmo PHP-SDK with composer:
$ composer require justimmo/php-sdk "1.0.*"
- Create a new file in your WP root directory called
justimmo_inquiry.php
with the following content
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Justimmo\Request\RealtyInquiryRequest;
use Justimmo\Model\Mapper\V1\RealtyInquiryMapper;
// get the arguments from the url that you passed through your local WP webhook
// https://musterimmo.at/justimmo_inquiry.php?realtyId=999999999&salutationId=1&title=Dr.&firstName=John&lastName=Doe&...
$realtyId = get_query_var('realtyId', null);
$salutationId = get_query_var('salutationId', '');
$title = get_query_var('title', '');
$firstName = get_query_var('firstName', '');
$lastName = get_query_var('lastName', '');
$email = get_query_var('email', null);
$message = get_query_var('message', null);
$phone = get_query_var('phone', '');
$street = get_query_var('street', '');
$zipCode = get_query_var('zipCode', '');
$city = get_query_var('city', '');
$country = get_query_var('country', '');
$categories = get_query_var('categories', '');
if ($realtyId === null || $email === null || $message === null) {
die('invalid request');
}
$api = new \Justimmo\Api\JustimmoApi('justimmo_export_feed_username', 'password');
$rq = new RealtyInquiryRequest($api, new RealtyInquiryMapper());
$rq->setRealtyId($realtyId)
->setSalutationId($salutationId)
->setTitle($title)
->setFirstName($firstName)
->setLastName($lastName)
->setEmail($email)
->setMessage($message)
->setPhone($phone)
->setStreet($street)
->setZipCode($zipCode)
->setCity($city)
->setCountry($country)
->setCategory($categories)
->send();