Multi-part emails displaying incorrectly in eM Client - php

I have a multi-part email script, that takes a POSTed email address and sends a simple email to them in HTML and/or Plain Text. It displays correctly in Gmail and Outlook, but not eM (and doesn't even get through to a Communigate server). The code:
<?php
$email_address = addslashes($_POST['email_address']);
if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
header("Location: ./?error=invalid-email");
exit();
}
$subject_line = "This is a test multi-part email";
$boundary = uniqid();
$headers = "MIME-Version:1.0\r\n";
$headers .= "From: Maggie Multipart <web#ukipme.com>\r\n";
$headers .= "To: " . $email_address . "\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain;charset=utf-8\r\n\r\n";
$message .= "Hello,\nThis is a test email, the text/plain version.\n\nRegards\nMaggie Multipart";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html;charset=utf-8\r\n\r\n";
$message .= "<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>";
$message .= "\r\n\r\n--" . $boundary . "--";
mail("", $subject_line, $message, $headers);
header("Location: ./?success=email-sent");
exit();
// var_dump($_POST);
?>
The message is received in eM as follows:
Content-Type: text/plain;charset=utf-8
Hello,
This is a test email, the text/plain version.
Regards
Maggie Multipart
However, eM is set up to receive HTML emails (and does so frequently). Can someone please help me fix this problem? Am I missing any headers?

My general advice for creating emails: Don't do it yourself (with some string concat functions/operators anyway). My weapon of choice is swiftmailer but there are other feasible libraries on the web, too.
<?php
require_once('autoload.php'); // swiftmailer was installed via Composer
$message = Swift_Message::newInstance('This is a test multi-part email')
->setBody(
"Hello,\nThis is a test email, the text/plain version.\n\nRegards\nMaggie Multipart",
'text/plain',
'utf-8'
)
->addPart(
"<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>",
'text/html',
'utf-8'
)
->setFrom(array('...#...' => '...'))
->setTo(array('...#...' => '...'));
$transport = Swift_SmtpTransport::newInstance('MSERV', 25, 'tls')
->setUsername('...')
->setPassword('...');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

Related

PHP mail - Spam Problems

I am trying to generate an email on signing up to website, I am using PHP to create and send the email. My domain has both SPF and DKIM running on it. However when i spam check my email i get this:
[SPF] srv61.hosting24.com does not allow your server 31.220.105.111 to use face159#srv61.hosting24.com
[Sender ID] srv61.hosting24.com does not allow your server 31.220.105.111 to use face159#srv61.hosting24.com
Your message is not signed with DKIM
I have contacted my hosting and they confirm that all the authentication systems are working on there end, so i must be doing something wrong in the PHP code but for the life of me i can't find what here is how i generate the email:
//Emails link to voucher
//create a boundary for the email. This
$boundary = uniqid('np');
$subject = "The Forum - Little Thank You";
//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: TheForum#freewifisystems.co.uk" . "\r\n";
$headers .= "To: " . $email . "\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
//Plain Email
$content2 = "plain text email here";
//Plain text body
$message .= "Hello,\nThis is a text email as HMTL is dissable, Please enable HTML to continue. \n\nRegards,\nThe Forum";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
//Html body
$message .= $content3;
$message .= "\r\n\r\n--" . $boundary . "--";
//invoke the PHP mail function
mail($email, $subject, $message, $headers);
?>
$email and $content3 are set via SQL and are setting correctly the email forms and is well formatted.

PHP - Not receiving email from contact form

I am not receiving emails from this contact form, but the message seems to sending okay and it also redirects me to the sent page.
I don't have access to the server only via FTP.
PHP
<?php
$to = 'test#gmail.com';
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$body = <<<EMAIL
<html>
<p><h3>Email Submited From Website.</h3></p>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Subject:</strong> $subject</p>
<p><strong>Message:</strong> $comment</p>
</html>
EMAIL;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
//$headers .= 'Cc: noreply#example.com' . "\r\n";
//$headers .= 'Bcc: noreply#example.com' . "\r\n";
if ($_POST['submit']){
mail($to, $subject, $body, $headers);
header ("Location: message-sent.php");
die();
} else {
header ("Location: message-failed.php");
die();
}
?>
Check if mail is actually being sent:
if (mail($to, $subject, $body, $headers)===false) {
echo "Not sent!";
} else {
echo "Sent!";
}
Change this:
$headers .= 'To: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
To this:
$headers .= "To: $to <test email>\r\n";
$headers .= "From: website#mt.co.uk <website#mt.co.uk>\r\n";
Also, you need to sanitize the subject and body of the email so that the email arrives, but this will usually be reflected in the results after email() reports a success, in that case the email will bounce, go to the spambox, or simply be refused.
If your hosting provider doesn't have an email server, you could try to use a free email server and phpMailer. https://github.com/PHPMailer/PHPMailer

PHP: Sending an html plus plain text email I got mail with empty body

This is php code
$boundary = uniqid("np");
$headers = "MIME-Version: 1.0\n" ;
$headers .= "From: $from\n";
$headers .= "To: $to\n";
if (!is_null($reply_to)) $headers .= "Reply-To: $reply_to \n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\n\n--" . $boundary . "\n";
$message .= "Content-type: text/plain;charset=utf-8\n\n";
//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\n\n--" . $boundary . "\n";
$message .= "Content-type: text/html;charset=utf-8\n\n";
$message .= "Hello, <BR> This is a text email, the <bold>html version</bold>.";
$message .= "<BR>Regards";
$message .= "<BR>Your Name";
$message .= "\n\n--" . $boundary . "--";
$sendmail = mail($to, $title, $message, $headers);
$headers variable contains:
MIME-Version: 1.0
From: Italia S.p.a. <root#domain.it>
To: My Name <my_name#domain.it>
Reply-To: My Alt Name <my_alt_name#domain.it>
Content-Type: multipart/alternative;boundary=np536b88fc27327
$message variable contains:
This is a MIME encoded message.
--np536b88fc27327
Content-type: text/plain;charset=utf-8
Hello,
This is a text email, the text/plain version.
Regards,
Your Name
--np536b88fc27327
Content-type: text/html;charset=utf-8
Hello, <BR> This is a text email,
the <bold>html version</bold>.<BR>Regards<BR>Your Name
--np536b88fc27327--
Problem is: when sending using mail($from, $to, $message, $headers) mail is delivered, but it's empty
Header should be..
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Check PHP mail() function.
Email headers must be separated by \r\n.

Sending an email with pdf attachment in php

I m new to php. I need to send the email with the pdf attachment.I m able to send an email with the attachment. But unable to open the pdf. I get the some error like this
Store information to the database(done),
Sends the staff an email with the new customer information (done), and
Send the customer a "thank you message" email with a pdf file attachment (not working).
I mean, the customer does receive an email, but when he/she opens the pdf file, I get the following error message:
"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."
If someone could help me resolve this problem, that would be great. Thanks!
Here is my code:
$to = 'form#kronova.in, ' . $Email;
$subject = 'ABC :: Admission Form Details';
$repEmail = 'form#kronova.in';
$fileName = 'ABC-Admission.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Principal abc <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "Thanks for filling online application form. Your online admission registration number is E0000". mysql_insert_id() . "." .$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
echo "Email sent";
}
else {
echo "Email failed";
}

Sending multipart email from php, only download shows not the actual message

I'm struggling with sending my multipart email with plain text version as well as with HTML. Unfortunately when I receive it in Gmail it shows as downloadable message only as shown below:
The code is pasted below and although I went over like 100 questions here on SO I'm still stuck.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: donotreply#someaddress.com\r\n";
$headers .= "Subject: Test mail\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
// plain text version
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// html version here
$message .= $htmlMessage;
$message .= "\r\n\r\n--" . $boundary . "--";
return #mail($to, $subject, $message, $headers);

Categories