Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send html and plain text email simultaneously with PHP Mailer

Tags:

php

I would like to send both html and plain text simultaneously in an email. I found this pretty straightforward tutorial but it used the mail() function to send the email. How do I transform this code send an email with the PHP Mailer class?

//specify the email address you are sending to, and the email subject
$email = '[email protected]';
$subject = 'Email Subject';

//create a boundary for the email. This 
$boundary = uniqid('np');

//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: Your Name \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 text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

//Html body
$message .= "
 Hello,
This is a text email, the html version.

Regards,
Your Name";
$message .= "\r\n\r\n--" . $boundary . "--";

//invoke the PHP mail function
mail('', $subject, $message, $headers);
like image 900
erdomester Avatar asked Mar 19 '14 13:03

erdomester


1 Answers

Try this:

$mail->SMTPDebug = 2;

Debug levels

Setting the PHPMailer->SMTPDebug property to these numbers or constants (defined in the SMTP class) results in different amounts of output:

  • SMTP::DEBUG_OFF (0): Disable debugging (you can also leave this out completely, 0 is the default).
  • SMTP::DEBUG_CLIENT (1): Output messages sent by the client.
  • SMTP::DEBUG_SERVER (2): as 1, plus responses received from the server (this is the most useful setting).
  • SMTP::DEBUG_CONNECTION (3): as 2, plus more information about the initial connection - this level can help diagnose STARTTLS failures.
  • SMTP::DEBUG_LOWLEVEL (4): as 3, plus even lower-level information, very verbose, don't use for debugging SMTP, only low-level problems.

You don't need to use levels above 2 unless you're having trouble connecting at all - it will just make output more verbose and more difficult to read.

Note that you will get no output until you call send(), because no SMTP conversation takes place until you do that.

Link: https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging

like image 180
codelone Avatar answered Sep 30 '22 22:09

codelone