I'm looking for a way to capture and manage email data using PHP. Basically, what I want to do is capture all the data in an email and then manipulate this data to my specification.
For example, say, I send an email containing a .zip file attachment to myemail#myproject.com, I want to be able to:
Get the attachment and place it in a specific folder on my site
Get the text content of the email
Get the subject of the email
Get the sender's info i.e. email address
Anyone know how I can get this done efficiently with PHP. I'm using LAMP by the way.
Thanks.
Start with PEAR Mail_mimeDecode. What you are looking to do is ambitious but can be done.
Basically what you will be doing is:
Instructing your MTA to deliver mail from an address to a pipe into your PHP script. Postfix and Sendmail can handle this with an alias like:
myemail: "|/path/to/your/parsingscript.php"
Parsing out the parts of the MIME email message
Locating and storing attachments after decoding them from base64 (or other encoding)
Parsing the headers.
Your PHP script will likely read the email message from STDIN and then pass the string to mimeDecode, which creates an object containing all the MIME parts.
Assuming your message was received into $str from STDIN, something like this gets you started:
$mime = Mail_mimeDecode::decode(array('include_bodies'=>TRUE, 'decode_headers'=>TRUE, 'decode_bodies'=>TRUE, 'input'=>$str));
// get the recipient To address:
$to = $mime->headers['to'];
Related
I have a simple email pipe script. But I need a copy of the incoming e-mail going to another e-mail address. Unfortunately I do not receive the e-mail as I wanted.
The code;
#!/usr/bin/php -q
<?php
$email_msg = ''; // the content of the email that is being piped
$email_addr = 'sales#flensmuts.nl'; // where the email will be sent
$subject = 'Piped:'; // the subject of the email being sent
// open a handle to the email
$fh = fopen("php://stdin", "r");
// read through the email until the end
while (!feof($fh)){
$email_msg .= fread($fh, 1024);
}
fclose($fh);
// send a copy of the email to your account
mail($email_addr, $subject, "Piped Email: ".$email_msg);
?>
Your mail server logs will probably say something about this. My guess is that it might be failing because your message is malformed because of the prefix you're adding. Try sending the message untouched, like this:
mail($email_addr, $subject, $email_msg);
Separately, for simple forwarding like this, you can probably set up your mail server to do this directly without having to go via a script.
There is no error checking in your script. There is no instrumentation in your script to see if it is even being run. You have not said why you believe that the script is being triggered. You have not said how mail() works on your test site.
Decompose the issues. Replace this script with one which dumps stein to a file to find out if there is an issue with the input integration.
Write and run a script which sends an email when using mail() when manually invoked. If you control the MTA then read your logs - even if these experiments are successful.
Then go back to your original script. Add some logging. Check the value returned by mail(). Check that the delivery path accepts forwarded mail with a broken envelope.
I need to send a link within HTML email using CodeIgniter.
first trial:-
send an email to using Gmail server, then the link will be sent successfully without any corruption.
Second trial:
I am using Microsoft Exchange Server.
when sending the link within the HTML email, the link will be received corruptly!!
I don't know why? Then the problem occurred when using only Exchange server.
correct link
http://000.00.0.00/s/admin/r?i=7&e=hana#dom.edu.com
corrupted link
http://000.00.0.00/s/admin/r?i=7&e=hana#do=.edu.com
If I put it within a href tag
correct link
http://000.00.0.00/s/admin/r?i=7&e=2
corrected link
http://000.00.0.00/s/admin/r=i=7&e=2
My IP= 000.00.0.00, it is not the actual but it is just an example
You have to change the crlf setting to use Exchange as an SMTP server.
Add $this->email->set_crlf( "\r\n" );to your controller or add $config['crlf'] = "\r\n"; to your email config file!
The equal signs getting substituted into your email message are a dead giveaway that the end of line character is the issue.
I'm trying to send a file via Zend Framework's (1.10.7) Mail library.
$mail = new Zend_Mail();
$mail->setSubject('Test');
$mail->setFrom('hello#ex.com');
$mail->setBodyText ( "" );
$at = $mail->createAttachment($txtFile->toString(),
'text/plain',
Zend_Mime::DISPOSITION_ATTACHMENT,
Zend_Mime::ENCODING_8BIT);
$mail->addTo ( "hi#you.com" );
$mail->send();
The file is a simple text file.
It works with Outllok, I receive a proper attachment but not with Gmail and Lotus Notes.
With Gmail I have this message :
This is a message in Mime Format. If you see this, your mail reader does not support this format.
Lotus Notes says this :
MIME content for this item is stored in attchment $RFC822.eml. Parsing MIME content failed: Incorrect format in MIME data..
What's wrong with Zend's Mail attachments ?
The docs say that the first parameter should be a binary string of data that is being sent.
Some of the comments suggest using file_get_contents() for sending an image, this of course does return a string, but it is a string of binary data that represent the image.
What does your toString() method return for the $txtFile? A simple string like
return "This is some plain text";
or does it return binary data?
You might want to try creating a plain text file with some content, then using file_get_contents() on that file and using the return value as the first parameter for sending the attachment, rather than just sending it a plain string.
Hope that helps.
Apparently it was our anti-spam software that was messing with the email's headers (in output).
I'm trying to handle bounced message and send to a responsible System Administrator.
I use CakePHP Email Component to send the message. On server side, I use postfix to transport the message.
function sendAsEmail($data) {
$Email->sendAs = 'html';
$Email->from = $user['Sender']['username'] . '#example.com';
$Email->return = Configure::read('App.systemAdminEmail');
$Email->bcc = array($data['Message']['recipient_text']);
$content = 'Some content';
$Email->send($content);
}
As you can see above, I set the $Email->return to sysadmin's email which it will send all the bounced message.
On postfix configuration, I tried creating a bounce.cf template and set bounce_template_file. http://www.howtoforge.com/configure-custom-postfix-bounce-messages
How do I get the bounced message and send it to System Administrator?
I think what you'll need to do is to use an SMTP (or I suppose POP3) connector for PHP. Then you'll basically have to create your own PHP email client that will login to the server, ask for the messages that have been bounced, and parse them appropriately.
I would think there would be a CakePHP component for this, but I can't find one.
I would recommend that you use an Envelope Header in your email. Otherwise you'll be stuck trying to parse the recipient server bounce, and those are very very inconsistent. If you use the VERP (variable envelope return protocol?) header, you can encode a unique hash into the email address which should be really easy to parse out in your PHPEmailClient.
More info on VERP: http://en.wikipedia.org/wiki/Variable_envelope_return_path
Cake-specific VERP stuff: http://www.mainelydesign.com/blog/view/setting-envelope-from-header-cakephp-email-component
I also highly recommend that you look into using SwiftMailer. It has a lot of plugins; you might find a base PHP SMTP client that you can easily modify to do what you need. http://swiftmailer.org/
I've been struggling with low level mail in PHP and I know I should be using a library for this, but that's not an option right now.
When doing mail in PHP, you can manually set additional headers, like From, Cc and Bcc, but you can also set Subject, To and a Body. When you call the function you pass the headers along to the mail() function, but that function also "asks for" a Subject, body and To.
My question then is: how does PHP handle the double intention in this? If you manually set the header to have Subject : foo, but then in the call to mail pass 'foo' along as the subject...?
I can't read C, so opening up PHP source probably won't help me here.
Thanks!
Well, no need to read C, just test it: it's a one liner :)
If you specify a subject in both places PHP does nothing special: you get an e-mail message with two subject headers. Which one gets displayed in your e-mail client is something I don't know; perhaps it's defined in e-mail protocols, perhaps it's a per-client choice.
About the "To" header, PHP sets one from the $to parameter when you don't specify a header manually; if you set one, your header prevails.
It's worth noting that the "From" and "To" headers have no effect in who sends and receives the message: they're purely informative. Mail server software requires senders and recipients to be specified implicitly; headers are not parsed for this purpose.
The question here seems to be if you specify a 'To' or a 'Subject' in the additional headers, what appears in the email produced.
If so, then the simple answer is to test it:
$add_to ='to: "added" <user#example.com>"; // substituting your email address
$param_to ='"param" <user#example.com>";
$add_subj ='subject: Subject in header';
$param_subj='Subject in param';
$add_hdr=$add_to . "\n" . $add_subj;
mail($param_to, $param_subj, "body - test", $add_hdr);
Then have a look at the message you get back.
C.
Right way - don't use Subject, To and a Body in headers. You can remove it from heders with regular expression, if you take headers as is. Other way - you can use PEAR library - http://pear.php.net/manual/en/package.mail.mail-mime.example.php