Send Mail from raw body for testing purposes

7.5k views Asked by At

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--
6

There are 6 answers

1
Marc B On

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

0
xrobau On

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 = '[email protected]';
    $to = '[email protected]';
    $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);
2
Jay On

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..

0
Chris Lear On

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).

6
Hugo Delsing On

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<[email protected]>";

  //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");
?>
2
Jasper N. Brouwer On

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: [email protected]           // enter MAIL FROM command
RCPT TO: [email protected]          // 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);