Pipe Email with Attachement to PHP & Resend with new e-mail address in "to" header field - email-attachments

I am a newb trying to do something that seems simple but has turned out to be pretty complicated. I'm trying use the hostgator forwarding e-mail feature to forward emails with attachments directed to one e-mail address and forward the email, attachment and all, to another e-mail address while removing the original email address in the "to" header section and replacing it with the e-mail address to which it is being forwarded.
With the standalone feature, cpanel in hostgator allows forwarding the e-mail with the attachment but the original email is visible when forwarded to the final receiving e-mail. It's kind of like hostgator is sending it to the final recipient as a bcc.
cpanel has another feature which allows piping the email to a php script and I was able to forward the raw e-mail using the mail function and a few other lines of code to the php but it looks awful and the attachment is just a bunch of code in base64. It also takes hours to receive by the final recipient.
I've scoured the internet looking for the final solution piecing together different pieces of code and settled with this thing below, but it doesn't seem to be working so I'm hoping some of you more experienced developers can impart some wisdom upon this poor newb. My humble apologies for the long winded prompt.
#!/usr/bin/php -q
<?php
ini_set("include_path", '/home#/username/php:' . ini_get("include_path") );
require_once 'Mail/mimeDecode.php';
var $raw = '';
var $decoded;
$src = 'php://stdin';
$fd = fopen($src,'r');
while(!feof($fd)){ $this->raw .= fread($fd,1024); }
fclose($fd);
$decoder = new Mail_mimeDecode($this->raw);
$this->decoded = $decoder->decode(
Array(
'decode_headers' => TRUE,
'include_bodies' => TRUE,
'decode_bodies' => TRUE,
)
);
$this->subject = $this->decoded->headers['subject'];
$getHead[] = $this->decoded->headers['Received'];
$getHead[] = $this->decoded->headers['From'];
$getHead[] = $this->decoded->headers['Reply-To'];
$getHead[] = $this->decoded->headers['X-Mailer'];
$getHead[] = $this->decoded->headers['Date'];
$getHead[] = $this->decoded->headers['MIME-Version'];
$getHead[] = $this->decoded->headers['Content-Type'];
$getHead[] = $this->decoded->headers['Content-Transfer-Encoding'];
$getHead[] = $this->decoded->headers['Return-Path'];
$getHead[] = $this->decoded->headers['X-OriginalArrivalTime'];
$getHead[] = $this->decoded->headers['Thread-Topic'];
$getHead[] = $this->decoded->headers['Thread-Index'];
$getHead[] = $this->decoded->headers['Message-ID'];
$getHead[] = $this->decoded->headers['Accept-Language'];
$getHead[] = $this->decoded->headers['Content-Language'];
$getHead[] = $this->decoded->headers['X-MS-Has-Attach'];
$getHead[] = $this->decoded->headers['X-MS-TNEF-Correlator'];
$getHead[] = $this->decoded->headers['x-ms-exchange-transport-fromentityheader'];
$getHead[] = $this->decoded->headers['x-originating-ip'];
$getHead[] = $this->decoded->headers['Content-Disposition'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AuthSource'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AuthAs'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AuthMechanism'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-Network-Message-Id'];
$getHead[] = $this->decoded->headers['X-MS-Exchange-Organization-AVStamp-Enterprise'];
$this->body = $this->decoded->body;
$email_to = "username#domainname.com";
mail($email_to, $this->subject, $this->body, implode("\r\n", $getHead));
?>

Related

My email is not sent when use gmail smtp server?

I am using gmail smtp server on my website to send email when users register but currently I can not send emails all information is correct. How can I fix this issue? I am using php mailer
function send()
{
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'example#gmail.com';
$mail->Password = 'mypassword';
$mail->From = 'noreply#example.com';
$mail->FromName = 'example';
$mail->AddAddress($this->_tpl_vars['TO']);
if (isset($this->_tpl_vars['CC']))
{
$cc = explode(';', $this->_tpl_vars['CC']);
foreach ($cc as $c)
if (!empty($c))
$mail->AddCC($c);
}
$mail->AddReplyTo('noreply#example.com');
$i = array();
$i = 3;
$mail->IsHTML(true);
$mail->Subject = $this->_tpl_vars['SUBJECT'];
$mail->Body = $this->_tpl;
#$mail->Send();
}
I see message in my gmail account one user try login with my ip server gmail block it.
Thank you.
You can go to https://www.google.com/settings/security/lesssecureapps
and turn on "Access for less secure apps"
The proper way now: you may want to check https://developers.google.com/gmail/xoauth2_libraries for PHP examples on how to do it using Oauth2

how do i send an email through joomla

I have a system so folks can register for a class through my Joomla site (I believe it's 3.0). But from there, I would like to send folks an email filling variables from the registration. So something like:
Dear (name), thank you for registering for (class).
This is to remind you your class is tomorrow, (date), at (place).
I believe for the registration, the system uses authorize.net
How can I accomplish this?
Thanks for the help!!
You can use JFactory:getMailer like suggested in the following post. I'm copying here his code example (modified it a bit):
$subject = "Here is the subject of your message.";
$body = "Here is the body of your message.";
$user = JFactory::getUser();
$to = $user->email;
$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($body);
# 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:
# Add a blind carbon copy
$mailer->addBCC("blindcopy#yourdomain.com");
Another alternative is using JMail::sendMail: http://docs.joomla.org/API17:JMail::sendMail
Fetch the Mail Object:
`$mailer = JFactory::getMailer();`
Set a Sender
$user = JFactory::getUser();
$recipient = $user->email;
$mailer->addRecipient($recipient);
$mailer->setSender($sender);
Recipient
$user = JFactory::getUser();
$recipient = $user->email;
$mailer->addRecipient($recipient);
Create the Mail
$body = 'Body Text';
$mailer->isHtml(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
Sending the Mail
$send = $mailer->Send();
if ( $send !== true ) {
echo 'Error sending email: ';
} else {
echo 'Mail sent';
}
https://docs.joomla.org/Sending_email_from_extensions

phpmailer noname attachment

I'm using phpmailer to send email. But all of my emails are send with noname attachement. I already checked if variables are set before I use addAttachemnt function and they are. It looks like this:
$fname = $_FILES['file']['name'];
$fTmpName = $_FILES['file']['tmp_name'];
$mail = new PHPMailer();
$mail->From = "mymail#mymail.com";
$mail->FromName = "mymail.com"; //moje meno
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->Host = "smtp.mymail.com";
$mail->Subject = "Subject";
$mail->AddAddress($email);
echo $fnameZivotopis;
$mail->AddAttachment($fTmpName,$fname);
$mail->Body = $msg;
$mail->Send(); // send message
Try to use AddStringAttachment(string $att_file_data, string $att_file_name, ...).

PEAR Mail, Mail_Mime and headers() overwrite

I'm currently working on a reminder PHP Script which will be called via Cronjob once a day in order to inform customers about smth.
Therefore I'm using the PEAR Mail function, combined with Mail_Mime. Firstly the script searches for users in a mysql database. If $num_rows > 0, it's creating a new Mail object and a new Mail_mime object (the code encluded in this posts starts at this point). The problem now appears in the while-loop.
To be exact: The problem is
$mime->headers($headers, true);
As the doc. states, the second argument should overwrite the old headers. However all outgoing mails are sent with the header ($header['To']) from the first user.
I'm really going crazy about this thing... any suggestions?
(Note: However it's sending the correct headers when calling $mime = new Mail_mime() for each user - but it should work with calling it only once and then overwriting the old headers)
Code:
// sql query and if num_rows > 0 ....
require_once('/usr/local/lib/php/Mail.php');
require_once('/usr/local/lib/php/Mail/mime.php');
ob_start();
require_once($inclPath.'/email/head.php');
$head = ob_get_clean();
ob_start();
require_once($inclPath.'/email/foot.php');
$foot = ob_get_clean();
$XY['mail']['params']['driver'] = 'smtp';
$XY['mail']['params']['host'] = 'smtp.XY.at';
$XY['mail']['params']['port'] = 25;
$mail =& Mail::factory('smtp', $XY['mail']['params']);
$headers = array();
$headers['From'] = 'XY <service#XY.at>';
$headers['Subject'] = '=?UTF-8?B?'.base64_encode('Subject').'?=';
$headers['Reply-To'] = 'XY <service#XY.at>';
ob_start();
require_once($inclPath.'/email/templates/files.mail.require-review.php');
$template = ob_get_clean();
$crfl = "\n";
$mime = new Mail_mime($crfl);
while($row = $r->fetch_assoc()){
$html = $head . $template . $foot;
$mime->setHTMLBody($html);
#$to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <'.$row['email'].'>'; // for testing purpose i'm sending all mails to webmaster#XY.at
$to = '=?UTF-8?B?'.base64_encode($row['firstname'].' '.$row['lastname']).'?= <webmaster#XY.at>';
$headers['To'] = $to; // Sets to in headers to a new
$body = $mime->get(array('head_charset' => 'UTF-8', 'text_charset' => 'UTF-8', 'html_charset' => 'UTF-8'));
$hdrs = $mime->headers($headers, true); // although the second parameters says true, the second, thrid, ... mail still includes the To-header form the first user
$sent = $mail->send($to, $hdrs, $body);
if (PEAR::isError($sent)) {
errlog('error while sending to user_id: '.$row['id']); // personal error function
} else {
// Write log file
}
}
There is no reason to keep the old object and not creating a new one.
Use OOP properly and create new objects - you do not know how they work internally.

Get email from Drupal CCK field and send mail using drupal_mail

Hi I'm using the Jobsearch module to build a recruitment site in Drupal 6. By default it sends applications to the email address of the user who posted the job. My problem is all jobs will be posted by a site admin - I need the applications to be sent to BOTH this admin and an email address specified in a CCK field (it's a CCK Email field to be precise).
Trying to extract the CCK field's value and use it in addition to the job poster's (admin's) email and send using drupal_mail but failing - email not sent to the custom CCK email field.
This is what I have attempted (and permutations of), from the Jobsearch module job.module file:
/**
* Implementation of hook_mail().
*/
function job_mail($key, &$message, $params) {
$result = theme('job_mail', $params['job_node'], $params['job_user'], $params['resume_node'], $params['resume_user']);
$message['subject'] = $result['subject'];
$message['body'] = $result['body'];
}
function job_send_email($job_nid, $resume_nid) {
global $user;
$params['job_node'] = $job_node = node_load(array('nid' => $job_nid));
$params['job_user'] = $job_user = user_load(array('uid' => $job_node->uid));
$params['resume_node'] = $resume_node = node_load(array('nid' => $resume_nid));
$params['resume_user'] = $resume_user = user_load(array('uid' => $resume_node->uid));
$from = $resume_user->mail;
$language = user_preferred_language($user);
$contactEmail = node_load($field_contact_email[0][nid]);
$to = "$job_user->mail, $contactEmail";
drupal_mail('job', 'job_apply', $to, $language, $params, $from);
watchdog('job', t("%name applied for job $job_node->nid.",
array('%name' => theme('placeholder', $resume_user->name . " <$from>"))));
}
It seems like it should be a simple thing to do, but I'm struggling!
Cracked it I think :) This sends to both the poster/user's email and one specified in my CCK email field.
function job_send_email($job_nid, $resume_nid) {
global $user;
$params['job_node'] = $job_node = node_load(array('nid' => $job_nid));
$params['job_user'] = $job_user = user_load(array('uid' => $job_node->uid));
$params['resume_node'] = $resume_node = node_load(array('nid' => $resume_nid));
$params['resume_user'] = $resume_user = user_load(array('uid' => $resume_node->uid));
$contactEmail = $job_node->field_contact_email[0]['email'];
$from = $resume_user->mail;
$language = user_preferred_language($user);
$to = "$job_user->mail, $contactEmail";
drupal_mail('job', 'job_apply', $to, $language, $params, $from);
watchdog('job', t("%name applied for job $job_node->nid.",
array('%name' => theme('placeholder', $resume_user->name . " <$from>"))));
}