How To Send Email from Your Joomla Extension

本文介绍如何利用Joomla!内置的JMail类轻松发送电子邮件,包括设置邮件主题、内容、收件人等基本操作,并提供了一些高级功能如添加抄送、密送和附件。此外,还介绍了如何验证邮件地址的有效性和清理邮件内容,确保邮件的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Because of it ubiquitous nature, automatic emailing is something that many clients expect. People want to be notified immediately of changes on their sites -- when a new article has been submitted, or a blog comment has been posted. Joomla! already provides some of this functionality out of the box by notifying administrators when a user has registered on their site. You, however, may find yourself needing to implement emailing in your own components. As you may have already guessed, Joomla! provides a very helpful class for this: JMail .

With just a few lines of code, you can send your first email:

# Set some variables for the email message


$subject

 = "You have a new message"

;
$body

 = "Here is the body of your message."

;
$to

 = "someone@yourdomain.com"

;
$from

 = array



(

"me@mydomain.com"

, "Brian Edgerton"

)

;
 
# Invoke JMail Class


$mailer

 = JFactory::getMailer

(

)

;
 
# Set sender array so that my name will show up neatly in your inbox


$mailer

->setSender

(

$from

)

;
 
# Add a recipient -- this can be a single address (string) or an array of addresses


$mailer

->addRecipient

(

$to

)

;
 
$mailer

->setSubject

(

$subject

)

;
$mailer

->setBody

(

$subject

)

;
 
# If you would like to send as HTML, include this line; otherwise, leave it out


$mailer

->isHTML

(

)

;
 
# Send once you have set all of your options


$mailer

->send

(

)

;
 

That's all there is to it for sending a simple email. If you would like to add carbon copy recipients, include the following before sending the email:

$mailer

->addCC

(

"carboncopy@yourdomain.com"

)

;
 
# Add a blind carbon copy


$mailer

->addBCC

(

"blindcopy@yourdomain.com"

)

;
 

Need to add an attachment? No problem:

 

$file

 = JPATH_SITE."/path/to/file.doc"

;
$mailer

->addAttachment

(

$file

)

;
 

As you can see, it really can't get much simpler or straightforward for sending an email. There are several more methods available in JMail . You should also check out JMailHelper . It provides several functions to help you secure input from users before passing it along in an email. Consider the following:

# Import JMailHelper


jimport(

'joomla.mail.helper'

)

;
 
$to

 = JRequest::getVar

(

'to'

, ''

, 'post'

)

;
$subject

 = JRequest::getVar

(

'subject'

, ''

, 'post'

)

;
$body

 = JRequest::getVar

(

'body'

, ''

, 'post'

)

;
$from

 = JRequest::getVar

(

'from'

, ''

, 'post'

)

;
 
if

 (

!JMailHelper::isEmailAddress

(

$to

)

 || !JMailHelper::isEmailAddress

(

$from

)

)

 :
    return

 false

;
endif

;
 
if

 (

!JMailHelper::cleanAddress

(

$to

)

 || !JMailHelper::cleanAddress

(

$from

)

)

 :
    return

 false

;
endif

;
 
$subject

 = JMailHelper::cleanSubject

(

$subject

)

;
$body

 = JMailHelper::cleanText

(

$body

)

; 
 

The above code has checked to make sure that the "to" and "from" email addresses are valid and that the addresses, subject, and body are clean (they do not have any extra headers injected). These checks are important if the emails being send back and forth will be heavily user-controlled.

Next time your client requests email alerts when someone fills out your custom form, don't panic. JMail has done all the hard work for you. Figure what information is going to be sent, and use these few lines of code send it via email.

http://docs.joomla.org/How_to_send_email_from_components

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值