菜单

typo3 mail

2010年04月8日 - typo3

1.sendnotifyemail (前台)

$this->cObj->sendNotifyEmail($msg, $recipients, $cc, $email_from, $email_fromName=”, $replyTo=”)

typo3.org/fileadmin/typo3api-4.2.6/df/d65/classtslib__cObj.html#bf556a717be8d450ebd66870e9d13f50

2.sendHTMLMail (前后台)

require_once PATH_tslib.’media/scripts/fe_adminLib.inc’;

user_feAdmin::sendHTMLMail($content,$recipient,$dummy,$fromEmail,$fromName,$replyTo=”);

typo3.org/fileadmin/typo3api-4.2.6/da/d94/fe__adminLib_8inc-source.html#l01536

Emailconf example


$confemail = array ('###SITENAME###' => $GLOBALS['TSFE']->TYPO3_CONF_VARS['SYS']['sitename']);
foreach ($GLOBALS['TSFE']->fe_user->user as $key1=>$val1){
$confemail['###'.t3lib_div::strtoupper($key1).'###'] = $val1;
}
/** * Application for alteration of personal information via e-mail
* @return success/failed message
*/
function sendNotifyEmail($confemail = array()) {
//get email content
$emailContent = $this->lConf ['emailtitle'] . chr(10) . $this->lConf ['emailcontent'];
//send email
if (! empty ( $confemail )) {
foreach ( $confemail as $key => $val ) {
$emailContent = str_replace ( $key, $val, $emailContent );
}
}
if ($this->cObj->sendNotifyEmail (
$emailContent, $this->lConf ['emailto'], '', $this->lConf ['emailfrom'], $this->lConf ['emailfromname'] )) {
return $this->cObj->wrap($this->pi_getLL ( 'emailsuccess' ),'|

‘); } else {
return $this->cObj->wrap($this->pi_getLL ( ’emailerror’ ),’|

‘); } }

3. t3lib_htmlmail(前后台)
####6.2####
$this->objectManager->get(‘TYPO3\\CMS\\Core\\Mail\\MailMessage’);

require_once(PATH_t3lib.’class.t3lib_htmlmail.php’);

// Prepare mailer and send the mail
$mailer = t3lib_div::makeInstance(‘t3lib_htmlmail’);
$mailer->start();
$mailer->useBase64();
$mailer->from_email = ”;
$mailer->from_name = ”;
$mailer->charset = ‘iso-8859-1′;
//$mailer->replyto_email = $this->reply_email;
//$mailer->replyto_name = ‘TEST-TASK’;
$mailer->subject = ”;
//$mailer->addPlain($mailBody);
$mailer->addHTML($url); //支持图片CID
$mailer->setRecipient(recipient@email.com);
$mailer->setHeaders();
$mailer->setContent();
$mailer->sendtheMail();

4. t3lib_mail_message (前后台)

$mail = t3lib_div::makeInstance(‘t3lib_mail_Message’); 
$mail->setFrom(array($email => $name)); 
$mail->setTo(array($email => $name)); 
$mail->setReplyTo(array(test_replyto@test.com => ‘name2′)); 
$mail->setSubject($subject); 
$mail->setBody($body, ‘text/html’, ‘iso-8859-1′); 
$mail->send();

相关:http://buzz.typo3.org/teams/core/article/your-first-blog/

Practical samples
The best way to explain is to show a few examples

Regular syntax

$mail = t3lib_div::makeInstance(‘t3lib_mail_Message’);
$mail->setFrom(array($email => $name));
$mail->setTo(array($email => $name));
$mail->setSubject($subject);
$mail->setBody($body);
$mail->send();
Concatenated syntax

Because most methods return the message object the calls can also be concatenated:

$mail = t3lib_div::makeInstance(‘t3lib_mail_Message’);
$mail->setFrom(array($email => $name))
->setTo(array($email => $name))
->setSubject($subject)
->setBody($body)
->send();
Extra options

The class t3lib_mail_Message extends the class Swift_Message, so all the magic of Swift Mailer is available in the t3lib_mail_Message. One of the things added by t3lib_mail_Message is the send() method. This takes care of all the ‘transport’ and ‘mailer’ magic. It will use the settings from the Install Tool, so all mails from TYPO3 will use the same transport method.

HTML and plain text mails

It’s easy to add alternative body parts. Swift Mailer will add the appropriate boundaries and MIME-part headers:

$mail->setBody($bodyHtml, ‘text/html’);
$mail->addPart($bodyText, ‘text/plain’);
Attachments

Again Swift Mailer will take care of the encoding, MIME part headers, etc.

$mail->attach(Swift_Attachment::fromPath($theFile));
Want to set the filename of the attachment?

$mail->attach(Swift_Attachment::fromPath($theFile)->setFilename($theName));
Inline images

If you want to embed images in HTML text Swift Mailer can help you too

$cid = $mail->embed(Swift_Image::fromPath($theFile);
The $cid contains the id you can use in the HTML content in the src attribute of the img tag.

 

使用cid:标记,表明图形是放在一个叫123的附件中的 然后加入一个附件,指定为 content-type: image/gif content-id: <123> 即上面那个123 content-disposition: inline; filename=sdfsdf filename是随便指定的 content-transfer-encodinf: base64 这里是附件内容要求用base64编码。

Legacy code
Existing extensions will use various mail functions in the current TYPO3 API. To make sure that all mails sent from the TYPO3 installation use the new transport mechanisms an adapter is included which catches all calls to the ‘old’ functions and forwards them to the Swift Mailer functions. This adapter is not 100% perfect, so in case of problems you can disable this adapter:

$TYPO3_CONF_VARS[‘MAIL’][‘substituteOldMailAPI’] = 0

以下是cid demo from FMOT:

$mail = t3lib_div::makeInstance('t3lib_mail_Message');
            $mail->setFrom(array($sender => $sender_name));
            $mail->setReplyTo($reply_to);
            $mail->setTo($row['email']);
            $mail->setSubject($subject);

            $body = t3lib_div::getUrl($url = $GLOBALS['TSFE']->baseUrl.$this->getLinkUrl('', '&action=newwindow&query_id='.$row['uid']));

            preg_match_all("/\embed(Swift_Image::fromPath($src));
                    if($cid){
                        $body = str_replace($src, $cid, $body);
                    }
                }
            }
            $mail->setBody($body, 'text/html', 'utf-8');
            return $mail->send();

发表评论

电子邮件地址不会被公开。 必填项已用*标注