How To Send Email from Your Joomla Extension
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
= "[email protected]"
;
$from
= array
(
"[email protected]"
, "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
(
"[email protected]"
)
;
# Add a blind carbon copy
$mailer
->addBCC
(
"[email protected]"
)
;
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
게시 페이지 마무리index.php index.php body 태그의 내용을 <body> -> <body <?php body_class(); ?>>라는 식으로 재기록함으로써 class가 부여된다. 전후를 비교해 보자. 다음에 다시 쓴...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.