* You have to set the smtp username ,password,host in the admin settings page
<?php
/*
* This function send mail with attachement
*
*/
function drupal_mail_wrapper_attachment($mailkey, $to, $subject, $body, $from, $header,$attachment="") {
$mail = new phpmailer(); //Create a new phpmailer object.
$username = variable_get('smtp_username', '');
$password = variable_get('smtp_password', '');
//Set the default name emails should be from. If value is not defined in settings use site_name.
if (variable_get('smtp_fromname', '') != ''){
$from_name = variable_get('smtp_fromname', '');
}
else{
$from_name = variable_get('site_name', '');
}
//If from email address is blank use smtp_from config option
if ($from == '' && variable_get('smtp_from', '') != ''){
$from = variable_get('smtp_from', '');
}
//Decide whether to use SMTP Authorization. Do so if username and password are given.
if ($username != '' and $password != '') {
$auth = TRUE;
} else {
$auth = FALSE;
}
//Take care of the email headers.
foreach ($header as $key => $value) {
//watchdog('error', 'Key: ' . $key . ' Value: ' . $value);
if (strtolower($key) == 'from') {
if ($from == NULL or $from == '') {
$from = $value; //If a from value was already given then set based on header.
}
}
else if (strtolower($key) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {
$mail->IsHTML(TRUE);
}
else if (strtolower($key) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {
//$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
$mail->AddCustomHeader($key . ": " . $value);
$mail->message_type = "pre";
}
else if (strtolower($key) == 'reply-to') {
$mail->AddReplyTo = $value;
}
else if (strtolower($key) == 'return-path') {
if (trim($value) != '') {
$mail->Sender = $value;
}
}
else if (strtolower($key) == 'content-transfer-encoding') {
$mail->Encoding = $value;
}
else if (strtolower($key) == 'mime-version') {
// just ommit MIME-Version it since it will be set by PHP-Mailer
}
else if (strtolower($key) == 'bcc') {
$bccrecipients = split(",", $value);
foreach ($bccrecipients as $bccrecipient) {
if ( strpos($bccrecipient, '<') !== false ) {
$bccparts = explode(" <", $bccrecipient);
$bccname = $bccparts[0];
$bccaddr = rtrim($bccparts[1], ">");
}
else {
$bccname = "";
$bccaddr = $bccrecipient;
}
$mail->AddBCC($bccaddr, $bccname);
}
}
else { //Else the header key is not special, just add it.
$mail->AddCustomHeader($key . ": " . $value); //Add header line.
}
}
//If no from address has been set than use a default.
if ($from == '' or $from == NULL){
$from = variable_get('site_mail', 'test@example.com'); //If no from can be found use site_mail variable.
}
//Set the correct protocol prefix to append to the smtp host.
switch(variable_get('smtp_protocol', 'standard')){
case "ssl":
$mail->Protocol = 'ssl://';
break;
case "tls":
$mail->Protocol = 'tls://';
break;
case "standard":
$mail->Protocol = '';
}
$mail->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
$mail->Port = variable_get('smtp_port', '25');
$mail->Mailer = "smtp";
$mail->SMTPAuth = $auth;
$mail->Username = $username;
$mail->Password = $password;
$mail->CharSet = 'utf-8';
$mail->From = $from;
$mail->FromName = $from_name;
$torecipients = split(",", $to);
foreach ($torecipients as $torecipient) {
if (strpos($torecipient, '<') !== false) {
$toparts = explode(" <", $torecipient);
$toname = $toparts[0];
$toaddr = rtrim($toparts[1], ">");
}
else {
$toname = "";
$toaddr = $torecipient;
}
$mail->AddAddress($toaddr, $toname);
}
$mail->Subject = $subject;
$mail->Body = $body;
if(count($attachment)) {
foreach ($attachment as $attachments) {
//$target_path_pdf = file_directory_path().'/bill/new/meha.pdf';
$mail->AddAttachment($attachments); // attachment
}
}
watchdog('smtp', "Sending mail to: $to");
//Try to send email, if it fails set watchdog entry.
if(!$mail->Send()) {
watchdog("smtp", "Error sending email: '". $mail->ErrorInfo ."' From: '$from' To: '$to'", WATCHDOG_ERROR);
return false;
}
$mail->SmtpClose();
return true;
}
?>
No comments:
Post a Comment