Given an Email as Raw Text, How to Send It Using PHP

Given an email as raw text, how can I send it using PHP?

I had the same problem but found a solution that seams to work. Open a socket in PHP and "telnetting" the raw emaildata. Something like this:

  $lSmtpTalk = array(
array('220', 'HELO my.hostname.com'.chr(10)),
array('250', 'MAIL FROM: me@hostname.com'.chr(10)),
array('250', 'RCPT TO: you@anotherhost.com'.chr(10)),
array('250', 'DATA'.chr(10)),
array('354', $lTheRawEmailStringWithHeadersAndBody.chr(10).'.'.chr(10)),
array('250', 'QUIT'.chr(10)),
array('221', ''));
$lConnection = fsockopen('mail.anotherhost.dk', 25, $errno, $errstr, 1);
if (!$lConnection) abort('Cant relay, no connnection');
for ($i=0;$i<count($lSmtpTalk);$i++) {
$lRes = fgets($lConnection, 256);
if (substr($lRes, 0, 3) !== $lSmtpTalk[$i][0])
abort('Got '.$lRes.' - expected: '.$lSmtpTalk[$i][0]);
if ($lSmtpTalk[$i][1] !== '')
fputs($lConnection, $lSmtpTalk[$i][1]);
}
fclose($lConnection);

You might need to lookup the mx-host if you dont know it. Google has an answer to that i'm sure.

Phpmailer sending email as plain text to emails

The issue was solved by enabling smtp login details

HTML emails sends as plain text with PHP

It seems from that somehow there are extra line breaks between each two lines of your headers. How it manages to happen, I'm not sure, as your code doesn't put them there. It's possible that the receiving mail server is messed up somehow and interprets \r\n as two line breaks instead of one. To start with, I would remove \r leaving only \n in and see what happens. I know this is not proper per RFC - but start with that.

To make you feel better, I am using a very similar code - and the email gets delivered correctly.

Sending Plain text emails using PHPMailer

You are setting $mail->MsgHTML() to a plain text message, and since whitespace formatting is ignored in HTML, you're getting an inline text.

I haven't used PHPMailer for a while, but from memory try:

$mail->Body = file_get_contents($newFile); 

Send HTML in email via PHP

It is pretty simple. Leave the images on the server and send the PHP + CSS to them...

$to = 'bob@example.com';

$subject = 'Website Change Request';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';

mail($to, $subject, $message, $headers);

It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

Here is the link I got the information from... (link)

You will need security though...

How can I send an email using PHP?

It's possible using PHP's mail() function. Remember the mail function will not work on a local server.

<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Reference:

  • mail

PHP - Plain text email

Removing the HTML should do the trick but you'll probably want to change the content-type to text/plain as well:

."Content-Type: text/plain; charset=\"utf-8\"\r\n"

(I would have let a comment suffice but I can't post comments yet :))



Related Topics



Leave a reply



Submit