I am developing a PHP application that needs to retrieve arbitrary emails from an email server. Then, the message is completely parsed and stored in a database.
Of course, I have to do a lot of tests as this task is not really trivial with all that different mail formats under the sun. Therefore I started to "collect" emails from certain clients and with different contents.
I would like to have a script so that I can send out those emails automatically to my application to test the mail handling.
Therefore, I need a way to send the raw emails - so that the structure is exactly the same as they would come from the respective client. I have the emails stored as .eml files.
Does somebody know how to send emails by supplying the raw body?
Edit:
To be more specific: I am searching for a way to send out multipart emails by using their source code. For example I would like to be able to use something like that (an email with plain and HTML part, HTML part has one inline attachment).
--Apple-Mail-159-396126150
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
The plain text email!
--=20
=20
=20
--Apple-Mail-159-396126150
Content-Type: multipart/related;
type="text/html";
boundary=Apple-Mail-160-396126150
--Apple-Mail-160-396126150
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
charset=iso-8859-1
<html><head>
<title>Daisies</title>=20
</head><body style=3D"background-attachment: initial; background-origin: =
initial; background-image: =
url(cid:4BFF075A-09D1-4118-9AE5-2DA8295BDF33/bg_pattern.jpg); =
background-position: 50% 0px; ">
[ - snip - the html email content ]
</body></html>=
--Apple-Mail-160-396126150
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename=bg_pattern.jpg
Content-Type: image/jpg;
x-apple-mail-type=stationery;
name="bg_pattern.jpg"
Content-Id: <4BFF075A-09D1-4118-9AE5-2DA8295BDF33/tbg.jpg>
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAASAAA/+IFOElDQ19QUk9GSUxFAAEB
[ - snip - the image content ]
nU4IGsoTr47IczxmCMvPypi6XZOWKYz/AB42mcaD/9k=
--Apple-Mail-159-396126150--
Using PHPMailer, you can set the body of a message directly:
$mail->Body = 'the contents of one of your .eml files here'
If your mails contain any mime attachments, this will most likely not work properly, as some of the MIME stuff has to go into the mail's headers. You'd have to massage the .eml to extract those particular headers and add them to the PHPMailer mail as a customheader
You could just use the telnet program to send those emails:
$ telnet <host> <port> // execute telnet
HELO my.domain.com // enter HELO command
MAIL FROM: sender#address.com // enter MAIL FROM command
RCPT TO: recipient#address.com // enter RCPT TO command
<past here, without adding a newline> // enter the raw content of the message
[ctrl]+d // hit [ctrl] and d simultaneously to end the message
If you really want to do this in PHP, you can use fsockopen() or stream_socket_client() family. Basically you do the same thing: talking to the mailserver directly.
// open connection
$stream = #stream_socket_client($host . ':' . $port);
// write HELO command
fwrite($stream, "HELO my.domain.com\r\n");
// read response
$data = '';
while (!feof($stream)) {
$data += fgets($stream, 1024);
}
// repeat for other steps
// ...
// close connection
fclose($stream);
You can just use the build in PHP function mail for it. The body part doesnt have to be just text, it can also contain mixed part data.
Keep in mind that this is a proof of concept. The sendEmlFile function could use some more checking, like "Does the file exists" and "Does it have a boundry set". As you mentioned it is for testing/development, I have not included it.
<?php
function sendmail($body,$subject,$to, $boundry='') {
define ("CRLF", "\r\n");
//basic settings
$from = "Example mail<info#example.com>";
//define headers
$sHeaders = "From: ".$from.CRLF;
$sHeaders .= "X-Mailer: PHP/".phpversion().CRLF;
$sHeaders .= "MIME-Version: 1.0".CRLF;
//if you supply a boundry, it will be send with your own data
//else it will be send as regular html email
if (strlen($boundry)>0)
$sHeaders .= "Content-Type: multipart/mixed; boundary=\"".$boundry."\"".CRLF;
else
{
$sHeaders .= "Content-type: text/html;".CRLF."\tcharset=\"iso-8859-1\"".CRLF;
$sHeaders .= "Content-Transfer-Encoding: 7bit".CRLF."Content-Disposition: inline";
}
mail($to,$subject,$body,$sHeaders);
}
function sendEmlFile($subject, $to, $filename) {
$body = file_get_contents($filename);
//get first line "--Apple-Mail-159-396126150"
$boundry = $str = strtok($body, "\n");
sendmail($body,$subject,$to, $boundry);
}
?>
Update:
After some more testing I found that all .eml files are different. There might be a standard, but I had tons of options when exporting to .eml. I had to use a seperate tool to create the file, because you cannot save to .eml by default using outlook.
You can download an example of the mail script. It contains two versions.
The simple version has two files, one is the index.php file that sends the test.eml file. This is just a file where i pasted in the example code you posted in your question.
The advanced version sends an email using an actual .eml file I created. it will get the required headers from the file it self. Keep in mind that this also sets the To and From part of the mail, so change it to match your own/server settings.
The advanced code works like this:
<?php
function sendEmlFile($filename) {
//define a clear line
define ("CRLF", "\r\n");
//eml content to array.
$file = file($filename);
//var to store the headers
$headers = "";
$to = "";
$subject = "";
//loop trough each line
//the first part are the headers, until you reach a white line
while(true) {
//get the first line and remove it from the file
$line = array_shift($file);
if (strlen(trim($line))==0) {
//headers are complete
break;
}
//is it the To header
if (substr(strtolower($line), 0,3)=="to:") {
$to = trim(substr($line, 3));
continue;
}
//Is it the subject header
if (substr(strtolower($line), 0,8)=="subject:") {
$subject = trim(substr($line, 8));
continue;
}
$headers .= $line . CRLF;
}
//implode the remaining content into the body and trim it, incase the headers where seperated with multiple white lines
$body = trim(implode('', $file));
//echo content for debugging
/*
echo $headers;
echo '<hr>';
echo $to;
echo '<hr>';
echo $subject;
echo '<hr>';
echo $body;
*/
//send the email
mail($to,$subject,$body,$headers);
}
//initiate a test with the test file
sendEmlFile("Test.eml");
?>
You could start here
http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/
I have no idea how good that code is, but it would make a starting point.
What you are doing is connecting direct to port 25 on the remote machine, as you would with telnet, and issuing smtp commands. See eg http://www.yuki-onna.co.uk/email/smtp.html for what's going on (or see Jasper N. Brouwer's answer).
Just make a quick shell script which processes a directory and call it when you want e.g. using at crontab etc
for I in ls /mydir/ do cat I | awk .. | sendmail -options
http://www.manpagez.com/man/1/awk/
You could also just talk to the mail server using the script to send the emls with a templated body..
Edit: I have added the code to Github, for ease of use by other people. https://github.com/xrobau/smtphack
I realise I am somewhat necro-answering this question, but it wasn't answered and I needed to do this myself. Here's the code!
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class SMTPHack
{
private $phpmailer;
private $smtp;
private $from;
private $to;
/**
* #param string $from
* #param string $to
* #param string $smtphost
* #return void
*/
public function __construct(string $from, string $to, string $smtphost = 'mailrx')
{
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPAutoTLS = false;
$mail->Host = $smtphost;
$this->phpmailer = $mail;
$this->from = $from;
$this->to = $to;
}
/**
* #param string $helo
* #return SMTP
*/
public function getSmtp(string $helo = ''): SMTP
{
if (!$this->smtp) {
if ($helo) {
$this->phpmailer->Helo = $helo;
}
$this->phpmailer->smtpConnect();
$this->smtp = $this->phpmailer->getSMTPInstance();
$this->smtp->mail($this->from);
$this->smtp->recipient($this->to);
}
return $this->smtp;
}
/**
* #param string $data
* #param string $helo
* #param boolean $quiet
* #return void
* #throws \PHPMailer\PHPMailer\Exception
*/
public function data(string $data, string $helo = '', bool $quiet = true)
{
$smtp = $this->getSmtp($helo);
$prev = $smtp->do_debug;
if ($quiet) {
$smtp->do_debug = 0;
}
$smtp->data($data);
$smtp->do_debug = $prev;
}
}
Using that, you can simply beat PHPMailer into submission with a few simple commands:
$from = 'xrobau#example.com';
$to = 'fred#example.com';
$hack = new SMTPHack($from, $to);
$smtp = $hack->getSmtp('helo.hostname');
$errors = $smtp->getError();
// Assuming this is running in a phpunit test...
$this->assertEmpty($errors['error']);
$testemail = file_get_contents(__DIR__ . '/TestEmail.eml');
$hack->data($testemail);
Related
I'm trying to get a script to poll a pop3 mailbox and extract some contents of the email body which is in MIME. The below script does the job however it only reads the first message because the $message variable is manually defined to look at the first message only. I need this script to loop and get all the messages in the inbox. I don't do programming for living so please excuse my ignorance if this is a simple task.
Below code was extracted from http://www.damnsemicolon.com/php/parse-emails-in-php-with-email-piping-part-2#.WnGfF2995EZ
require('mime_parser.php');
require('rfc822_addresses.php');
require('pop3.php');
stream_wrapper_register('mlpop3', 'pop3_stream'); /* Register the pop3 stream handler class */
$user=UrlEncode("someuserid");
$password=UrlEncode("somepassword");
$realm=UrlEncode(""); /* Authentication realm or domain */
$workstation=UrlEncode(""); /* Workstation for NTLM authentication */
$host = 'someip';
$apop=0; /* Use APOP authentication */
$authentication_mechanism=UrlEncode("USER"); /* SASL authentication mechanism */
$debug=0; /* Output debug information */
$html_debug=1; /* Debug information is in HTML */
$message=1;
$message_file='mlpop3://'.$user.':'.$password.'#'.$host.'/'.$message.
'?debug='.$debug.'&html_debug='.$html_debug.'&realm='.$realm.'&workstation='.$workstation.
'&apop='.$apop.'&authentication_mechanism='.$authentication_mechanism;
$mime=new mime_parser_class;
$mime->decode_bodies = 1;
$parameters=array(
'File'=>$message_file,
/* Read a message from a string instead of a file */
/* 'Data'=>'My message data string', */
/* Save the message body parts to a directory */
/* 'SaveBody'=>'/tmp', */
/* Do not retrieve or save message body parts */
);
$success=$mime->Decode($parameters, $decoded);
if(!$success)
echo '<h2>MIME message decoding error: '.HtmlSpecialChars($mime->error)."</h2>\n";
else
{
if($mime->Analyze($decoded[0], $results))
{
//for($line=0;$line<count($decoded);$line++)
$fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name'];
$fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address'];
//get the subject
$subject = $decoded[0]['Headers']['subject:'];
echo '<pre>';
//get the message body
if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body'])){
$body = $decoded[0]['Body'];
} elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body'])) {
$body = $decoded[0]['Parts'][0]['Body'];
} elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body'])) {
$body = $decoded[0]['Parts'][0]['Parts'][0]['Body'];
}
echo "\nFrom: $fromName \n\nSubject: $subject \n\nAffected Sites: $searchmsg ";
echo '</pre>';
}
else
echo 'MIME message analyse error: '.$mime->error."\n";
}
I have a script that is reading mails from a mail account and writing the content into files. This is working well besides there are some mails, that can not be read.
Here is some of the code
$mails = imap_search($imap,'UNSEEN');
foreach($mails as $mail)
{
$content_type = imap_fetchmime($imap,$mail,1);
$message = imap_fetchbody($imap,$mail,1);
//$message = imap_fetchbody($imap,$mail,1.1);
//$message = imap_fetchbody($imap,$mail,2);
$structure = imap_fetchstructure($imap, $mail);
if(isset($structure->parts[1]))
{
$part = $structure->parts[1];
}
$encoding = $part->encoding;
}
The problem is $message is a long cryptic string, that I am not able to decrypt, and all other variables are empty ($content_type, $structure, $encoding). I have tried all decryption methods I know, but had no success. When I change the option of imap_fetchbody to 1.1 or 2 I get nothing back.
I want to automatically process E-Mails which I get from Paypal for donations to my E-Mail-Adress. (via cronjob and php script)
So far, so good. I made a test donation and for further testing, I copy this mail with Outlook to INBOX several times.
Everything worked fine with the copied Mail (I'm checking a String in E-Mail Body)
but now I've made another test donation and it wasn't working.
It turned out that the E-Mail-Body is not parsed correctly.
I've tried already with Ararat Synapse in Delphi but same result.
The correct E-Mail Body from copied Mail looks like this:
Guten Tag Peter Meier ! Diese E-Mail bestätigt den Erhalt einer Spende über €1,50 EUR von Peter Meier (peter.meier#gmx.de ). Sie können die Transaktionsdetails online abrufen . Spendendetails Gesamtbetrag: €1,50 EUR
But the original Mail from Paypal is parsed as
PGh0bWwgPgogICA8aGVhZD4KICAgCQk8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LVR5cGUi IGNvbnRlbnQ9InRleHQvaHRtbDsgY2hhcnNldD1VVEYtOCI+CiAgIAkJPG1ldGEgbmFtZT0i dmlld3BvcnQiIGNvbnRlbnQ9ImluaXRpYWwtc2NhbGU9MS4wLG1pbmltdW0tc2NhbGU9MS4w LG1heGltdW0tc2NhbGU9MS4wLHdpZHRoPWRldmljZS13aWR0aCxoZWlnaHQ9ZGV2aWNlLWhl aWdodCx0YXJnZXQtZGVuc2l0eWRwaT1kZXZpY2UtZHBpLHVzZX...
And so on, copiedmail.html is 3KB, original is 28KB.
My Code:
$connection = imap_open("{imap.gmx.net:993/imap/ssl}", "peter.meier#gmx.de", "password"); //connect
$count = imap_num_msg($connection); //get E-Mail count in INBOX
for($msgno = 1; $msgno <= $count; $msgno++) //walk through INBOX mails
{
$headers = imap_headerinfo($connection, $msgno); //read E-Mail header
$subject = $headers->subject; //read subject
//decode subject
$elements = imap_mime_header_decode($subject);
$decodedsubject="";
for ($i=0; $i<count($elements); $i++)
{
$decodedsubject = $decodedsubject.$elements[$i]->text;
}
$body = imap_fetchbody ($connection, $msgno, 1); //read body
echo $body; <--- Here I get long cryptic Text in original Mail
}
Thanks to NineBerry,
I found out that paypal uses base64 encoding for their mails.
So the body was parsed correctly by this code
$body = imap_fetchbody ($connection, $msgno, 1); //read E-Mail-Body
$body = imap_base64($body);
echo $body;
To check encoding as suggested, you could do:
$structure = imap_fetchstructure($connection, $msgno);
$encoding = $structure->encoding;
$body = imap_fetchbody ($connection, $msgno, 1);
$body=decodebody($encoding, $body);
echo $body;
function decodebody($encoding, $body)
{
switch ($encoding)
{
# 7BIT
case ENC7BIT:
echo "7BIT<br>";//
return $body;
break;
# 8BIT
case ENC8BIT:
echo "8BIT<br>";//
return quoted_printable_decode(imap_8bit($body));
break;
# BINARY
case ENCBINARY:
echo "BINARY<br>";//
return imap_binary($body);
break;
# BASE64
case ENCBASE64:
echo "BASE64<br>";//
return imap_base64($body);
break;
# QUOTED-PRINTABLE
case ENCQUOTEDPRINTABLE:
echo "QUOTED<br>";//
return quoted_printable_decode($body);
break;
# OTHER
case ENCOTHER:
echo "OTHER<br>";//
return $body;
break;
# UNKNOWN
default:
echo "UNKNOWN<br>";//
return $body;
break;
}
}
Of course echo in function is only for you to check.
BTW, I pasted the snippet from my Original Question to this online decoder:
https://www.base64decode.org and it also worked :)
I noticed that my server has been returning this error when trying to send email to an invalid domain:
Standard Message: Failed to set sender: user#invaliddomain.coom [SMTP: Invalid response code received from server (code: 553, response: 5.1.8 ... Domain of sender address user#invaliddomain.coom does not exist)]
Standard Code: 10004
DBMS/User Message:
DBMS/Debug Message:
Is there a way to check the domain first before attempting to send the email? I have a feeling I could also handle this on the SMTP server end by squelching this error, but I like the idea of being able to test an email domain first before sending it. Thanks for your ideas!
Here is the pertinent code just for reference (variables are filtered in from a form):
$headers['To'] = $to_address;
$headers['From'] = $from;
$headers['Reply-To'] = $from;
$headers['Subject'] = $subject;
$this->setHTMLBody($body);
$body = $this->get(array('text_charset' => 'utf-8'));
$headers = $this->headers($headers, true);
$message =& Mail::factory('smtp');
$mail = $message->send($to_address,$headers,$body);
You could use Net_DNS2 to determine if the domain exists and if so, send the email on it's merry way.
include "Net/DNS2.php";
$r = new Net_DNS2_Resolver();
try {
$result = $r->query($domain, 'MX');
} catch(Net_DNS2_Exception $e) {
$result = null;
}
if ($result !== null) {
// send email...
}
Naturally, I'd suggest some level of caching so you aren't repeating lookups.
I'm trying to add an attachment to emails that I draft but cannot seem to get it to work. I've tried to follow the examples here and here but without success.
Here's what I'm able to do so far:
Connect to the Exchange server
Open the mailbox
Draft an html email
Append the email to the mailbox and have it render the html correctly. (when using text/html as the content-type). Using anything else displays the html as plaintext.
As an additional note, after being drafted, the emails are appended to a mailbox on an Exchange 2010 server and are viewed then sent via Outlook 2010.
Below is my mailer class and the code to draft the email.
mailer.php
<?php
class mailer
{
const USER = 'user';
const PASSWORD = 'pass';
const MAILBOX = '{conn}DRAFTS';
// STRING ORDER: $content-type . $from . $to . $cc . $subject . "\r\n\r\n" . $message
public $message;
public $imap_conn;
public $boundary;
function __construct()
{
// Open the message property so we can start appending strings.
$this->message = '';
// Open the IMAP connection
$this->imap_conn = imap_open(self::MAILBOX,self::USER,self::PASSWORD);
}
public function content_type($string_type)
{
$this->message .= "Content-type:{$string_type}\r\n";
}
public function from($string_from)
{
$this->message .= "From:{$string_from}\r\n";
}
public function to($string_to)
{
$this->message .= "To:{$string_to}\r\n";
}
public function cc($string_cc)
{
$this->message .= "Cc:{$string_cc}\r\n";
}
public function mime($float_mime_version)
{
$this->message .= "MIME-Version:{$float_mime_version}\r\n";
}
public function subject($string_subject)
{
$this->message .= "Subject:{$string_subject}\r\n\r\n";
}
public function message($string_message)
{
$this->message .= "{$string_message}\r\n";
}
public function set_boundary($string_boundary)
{
$this->boundary = $string_boundary;
}
public function append()
{
imap_append($this->imap_conn,self::MAILBOX,$this->message,"\\Draft");
imap_close($this->imap_conn);
}
}
?>
Draft code
// A random hash used for the boundary
$rh = md5(date('c',time()));
$data = chunk_split(base64_encode('Testing'));
$m = new mailer;
$m->set_boundary('--PHP-mixed-' . $rh);
$m->content_type('multipart/mixed; boundary="' . $m->boundary . '"');
$m->mime('1.0');
$m->from('from#mail.com');
$m->to('to#mail.com');
$m->cc('cc1#mail.com,cc2#mail.com');
$m->subject('A new email');
$m->message("
{$m->boundary}
Content-Type: text/html; charset=\"utf-8\"
Content-Transfer-Encoding: base64
Testing my <b>HTML</b>
</br>
{$m->boundary}
Content-Type: application/octet-stream; name=\"test.txt\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"test.txt\"
{$data}
{$m->boundary}--
"));
$m->append();
The message before its appended
Content-type:multipart/mixed; boundary="--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f"
MIME-Version:1.0
From:from#mail.com
To:to#mail.com
Cc:cc1#mail.com
Subject:New Message
--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: "base64"
My <b>html</b> content
--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"
--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f--
Question: How do I add an attachment to an email drafted with PHP via IMAP?
Answer: In short, the issue was in two places.
Improper placement of newlines (\r\n). A single newline should be after each header and two newlines after each ending header of a section. See Figure 1.
Improper boundaries. Boundaries should start with -- followed by a unique string. The ending boundary of the message should begin and end with -- See Figure 2
Figure 1
Content-Type: text/html\r\n
Content-Encoding: base64\r\n\r\n
"Message text here."\r\n
Figure 2
--unique
Content-Type: text/html\r\n
Content-Encoding: base64\r\n\r\n
HTML stuff here.\r\n
--unique
Content-Type: application/octet-stream\r\n
Content-Disposition: attachment; filename=\"logs.csv\"\r\n
Content-Transfer-Encoding: base64\r\n\r\n
Attachment(s)\r\n
--unique--
I plan on doing a more in depth write-up on this as it took me a very long time to get it right.
It would be helpful to have some more information on your debugging so far. Is the file being read successfully? Does anything make it to the mailbox? It would also help me to see an output of the full message before your final append.
I do not know if this will solve it, but from my successful code and your second example, your message should have an extra filename property, and the values wrapped in quotes.
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"