由于Cognos发送邮件功能的限制,无法同时在正文和附件中发送不同格式的报表数据,特采用php+Mashup Service的方式来变通实现。本文是实现笔记。
首先尝试在php上实现邮件发送。
利用开源的php类PHPMailer实现发送功能,通过https://github.com/Synchro/PHPMailer下载。下载解压放到htdocs目录下。
在编写php代码时参考以下代码:
<?php
require_once(“PHPMailer/PHPMailerAutoload.php”);
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘smtp.163.com’; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘cognosplus@163.com’; // SMTP username
$mail->Password = ‘Password’; // SMTP password
//$mail->SMTPSecure = ‘tls’; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->CharSet = “gbk”; // 这里指定字符集!
$mail->Encoding = “base64″;
$mail->From = ‘cognosplus@163.com’;
$mail->FromName = ‘CognosPlus’;
$mail->addAddress(‘hupingzhi@163.com’, ‘Hupz’); // Add a recipient
//$mail->addAddress(‘ellen@example.com’); // Name is optional
//$mail->addReplyTo(‘info@example.com’, ‘Information’);
//$mail->addCC(‘cc@example.com’);
//$mail->addBCC(‘bcc@example.com’);
//$mail->addAttachment(‘/var/tmp/file.tar.gz’); // Add attachments
//$mail->addAttachment(‘/tmp/image.jpg’, ‘new.jpg’); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = ‘Here is the subject’;
$mail->Body = ‘
<html><head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gbk”>
</head>
<body>
看看能否显示中文?
</body>
</html>
‘;
$mail->AltBody = ‘This is the body in plain text for non-HTML mail clients’;
date_default_timezone_set(“Asia/Shanghai”);
if(!$mail->send()) {
echo ‘Message could not be sent.’;
echo ‘Mailer Error: ‘ . $mail->ErrorInfo;
} else {
echo ‘Message has been sent’.date(“Y-m-d H:i:s”);
}
?>
运行测试结果,可以正常发送。