Send emails from your Magento’s custom extensions

Home Magento TopicsSend emails from your Magento’s custom extensions
mage+php

Send emails from your Magento’s custom extensions

1 Comment

Send emails from your Magento’s custom extensions.

In this post we show you how to send an email from Magento, using several functions that you can insert into your extensions. They could be very useful if for example, you want to send a notification when ending a process or when a particular action is executed.

We hope that it will be helpful!

<strong><span style="text-decoration: underline;">Functions to send and Email from Magento:</span></strong>
 
<span style="color: orange;">//*** Function "SendNotificationEMail" is used to create the new message and send the email.</span>
<span style="color: green;">public</span> <span style="color: blue;">function</span> SendNotificationEMail()
 
{
 
<span style="color: blue;"> $toEmail =</span> ‘Email of Message Receptor’;
<span style="color: blue;"> $fromEmail =</span> ‘Email of Message Sender’;
<span style="color: blue;"> $fromName =</span> ‘Name of Message Sender’;
<span style="color: blue;"> $subject =</span> ‘Email Subject’;
<span style="color: blue;"> $body = $this-&gt;</span>createBody(‘Text of Body Message’);
 
<span style="color: blue;"> $attachments =</span> <span style="color: green;">array</span>();
<span style="color: orange;"> //*** On the field $attachments you can include uploaded files, for example, files attached from a contact form,etc.</span>
 
<span style="color: blue;"> $this-&gt;</span>sendMail(<span style="color: blue;">$toEmail, $fromEmail, $fromName, $subject, $body, $attachments</span>);
 
}
 
 
 
<span style="color: orange;">//*** Function "createbody" is used to create the body of message.</span>
<span style="color: blue;">private</span> <span style="color: blue;">function</span> createBody(<span style="color: blue;">$body_content</span>)
 
{
 
<span style="color: orange;"> //*** You can include any text between tags DATA or variables as we show in this example.</span>
 
<span style="color: blue;">$body = &lt;&lt;&lt;</span>DATA
{<span style="color: blue;">$ body_content</span> }
Date: {<span style="color: blue;">$currentDate</span>}
DATA;
 
 
 
<span style="color: green;"> return</span> <span style="color: blue;">$body</span>;
}
 
 
 
<span style="color: orange;">//*** Function "sendMail" is called for send the email using an instance of Zend_Mail. In this example, we use the UTF-8 codification. </span>
<span style="color: green;">private</span> <span style="color: blue;">function</span> sendMail(<span style="color: blue;">$toEmail, $fromEmail, $fromName, $subject, $body,</span> <span style="color: green;">array</span> <span style="color: blue;">$attachments =</span> <span style="color: green;">array</span>())
{
<span style="color: blue;"> $mail = <span style="color: green;">new</span> Zend_Mail('UTF-8');</span>
 
<span style="color: blue;"> $mail-&gt;</span>addTo(<span style="color: blue;">$toEmail</span>)
<span style="color: blue;"> -&gt;</span>setFrom(<span style="color: blue;">$fromEmail, $fromName</span>)
<span style="color: blue;"> -&gt;</span>setSubject(<span style="color: blue;">$subject</span>)
<span style="color: blue;"> -&gt;</span>setBodyText(<span style="color: blue;">$body</span>, null, Zend_Mime::ENCODING_8BIT);
 
<span style="color: green;"> foreach</span> (<span style="color: blue;">$attachments</span> <span style="color: green;">as</span> <span style="color: blue;">$attachment</span>) {
<span style="color: blue;"> $mail-&gt;</span>addAttachment(<span style="color: blue;">$attachment</span>);
}
 
<span style="color: blue;"> $mail-&gt;</span>send();
}
 
 
[bws_google_captcha]

Leave a Reply

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