I want to use php mailer for sending multiple attachment in mail.
But the problem is, how to use it. Where to download it, how to install it,. I have searched for 3 days, but have got confused, two or three tutorials that i used, doesn't work and make me more confused.
I want a single file tag, that uploads multiple attachments , and send them in email.
I have done with the Email sending with one attachment successfully..
Please guide me. And please give those links that really work for the purpose.
PHPMailer can be downloaded from its SourceForge page.
Now to the code, which is mostly taken from the examples provided in the ZIPball:
<?php
require_once 'class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
This is a combination of multiple scripts and a bit of reading. I haven't added any form processing or the like, but it allows for the use of the option to attach multiple files through one input button. Hope its helpful to someone. I'm sure it breaks all kinds of standards. I do know it works in Chrome 31 and IE10.
Edit: Working with this little script, I added HTML formatting for the message and the thank you message replacement.
<?php
if(isset($_POST['Submit'])) {
$email_to = "";
$email_subject = "";
$thankyou = "thanks.html";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
function died($error) {
echo "Sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$requester_name = $_POST['requester_name']; // required
$requester_email = $_POST['requester_email']; // required
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message = "<html><body> \r\n";
$email_message .= "<table style=\"border: 1px #777 solid; font-family: Arial; font-size: 13px;\" cellpadding=\"7\"> \r\n";
$email_message .= "<tr><td style=\"background: #444; color:#fff;\"><strong>New Hire Form</strong></td><td style=\"background: #444; color:#fff;\">Requirements</td></tr>" . "\n";
$email_message .= "<tr><td style=\"background: #ccc;\"><strong>Requester Name: </strong></td><td style=\"background: #ddd;\">" .clean_string($requester_name). "</td></tr>" . "\n";
$email_message .= "<tr><td style=\"background: #ccc;\"><strong>Requester Email: </strong></td><td style=\"background: #ddd;\">".clean_string($requester_email). "</td></tr>" . "\n";
$email_message .= "</table> \r\n";
$email_message .= "</body></html>";
// multipart boundary
$email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n";
for($i=0;$i<count($_FILES['attachfile']['name']);$i++)
{
if($_FILES['attachfile']['name'][$i] != "")
{
//here you will get all files selected by user.
$name = ($_FILES['attachfile']['name'][$i]);
$tmp_name = ($_FILES['attachfile']['tmp_name'][$i]);
$type = ($_FILES['attachfile']['type'][$i]);
$size = ($_FILES['attachfile']['size'][$i]);
echo count($_Files['attachfile']) ;
echo $_FILES['attachfile']['name'][$i] ;
echo $_FILES['attachfile']['tmp_name'][$i] ;
echo $_FILES['attachfile']['type'][$i] ;
// Read the file content into a variable
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
// Close the file
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$headers .= 'From: '.$email_sender."\r\n". // Mail will be sent from your Admin ID
'Reply-To: '.$Email."\r\n" . // Reply to Sender Email
'X-Mailer: PHP/' . phpversion();
// headers for attachment
$headers .= "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
#mail($email_to, $email_subject, $email_message, $headers);
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>
Related
I'm trying to send emails with attachment from a HTML form using PHP mail().
I have found a script online that works - but with one problem:
If a file is attached to the email, the script works fine. However, if there is no attachment, the email is sent but doesn't contain any text content (blank email body).
I want the ability to add an attachment to be optional.
My question is, how can an email be sent successfully, with or without an attachment?
Thanks!
The PHP script I'm using is as follows:
<?php
$email = trim($_POST['email']);
$email_san = filter_var($email, FILTER_SANITIZE_EMAIL);
$fname = $_POST['first_name'];
$fname_san = filter_var($fname,FILTER_SANITIZE_STRING);
$lname = $_POST['last_name'];
$lname_san = filter_var($lname,FILTER_SANITIZE_STRING);
$org = trim($_POST['organisation']);
$org_san = filter_var($org,FILTER_SANITIZE_STRING);
$user_phone = $_POST['phone'];
$trim_phone = trim($user_phone);
$replace_phone = preg_replace('/[^0-9+-]/', '', $trim_phone);
$phone_san = filter_var($replace_phone,FILTER_SANITIZE_NUMBER_INT);
$message = $_POST['message'];
$fromemail = $email_san;
$subject="Inquiry";
$email_message = '<p><b>First Name:</b> '.$fname_san.'</p>
<p><b>Organisation:</b> '.$org_san.'</p>
<p><b>Email:</b> '.$email_san.'</p>
<p><b>Phone:</b> '.$phone_san.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
$semi_rand = md5(uniqid(time()));
$headers = "From: ".$fromemail;
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
if($_FILES["file"]["name"]!= ""){
$strFilesName = $_FILES["file"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: application/octet-stream;\n" .
" name=\"{$strFilesName}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$strContent .= "\n\n" .
"--{$mime_boundary}--\n";
}
$toemail="email#somedomain.com";
if(mail($toemail, $subject, $email_message, $headers)){
echo "Email sent.";
}else{
echo "Email NOT sent.";
}
?>
/*** UPDATE ***/
OK, taking advice that PHPMailer is a better method of sending emails, I have made another attempt to setup PHPMailer.
I don't have Composer so I downloaded and installed PHPMailer manually. I don't have Composer so I downloaded and installed PHPMailer manually. My server folder hierarchy is shown in this image.
I found a PHPMailer tutorial online which provided a simple script which I saved as 'mailer-test.php', uploaded and linked to my PHPMailer install:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "php-mailer/src/Exception.php";
//PHPMailer Object
$mail = new PHPMailer(true); //Argument 'true' in enables exceptions
//From email address and name
$mail->From = "me#mydomain.com";
$mail->FromName = "Full Name";
//To address and name
//$mail->addAddress("recipient#somedomain.com", "Recepient Name");
$mail->addAddress("recipient#somedomain.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("me#mydomain.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
try {
$mail->send();
echo "Message has been sent successfully";
} catch (Exception $e) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
However, when I view 'mailer-test.php' in a browser, I get the follwing server error:
This page isn't working.
yourdomain.com is currently unable to handle this request.
HTTP ERROR
What is causing this?
Something look strange with your strcontent (he miss the $ on line 39)
But to get more simple, use PhpMailer class
It makes your life easy by sending mails
Make sure you add enctype='multipart/form-data' to your form tag:
<form action='index.php' method='POST' enctype='multipart/form-data'>
...
</form>
I am using mPDF class and am successfully able to generate an email with the following code. However, my email comes out blank. I am assuming but am not sure if this has something to do with my headers. It's hard for me to tell because I am getting my emails but am not able open the pdf it generates.
include("./mpdf.php");
$mpdf->debug = true;
$html2 = '
<div style="margin-left:3%;">Attach additional photos:
<input type="file" name="file" id="file" /></div><hr />';
echo $html2;
if ( isset( $_POST['submit'] ) ) {
$file_path = "webform.php";
$file_path_type = "application/pdf";
$mpdf=new mPDF('iso-8859-2');
$mpdf->WriteHTML($html);
$file_path_name = "eval.pdf";
$headers .= 'Content-type: text/html; charset=utf-8' . "\n";
$from = "info#myemail.com";
$to = $_POST['email'];
$ccto = $_POST['youremail'];
$subject = "New Form Submitted";
$message = "*** This is an automatically generated email,
please do not reply *** Someone in your association
has completed a survey.
$headers = "From: ".$from;
$headers.= "cc: " . $ccto . " <" . $ccto . ">" . "\n" ;
$file = fopen($file_path,'rb');
$data = fread($file,$file_path);
fclose($file);
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if(#mail($to, $subject, $message, $headers)) {
echo '<script language="javascript">';
echo 'alert("Document sent successfully!")';
echo '</script>';
echo "Sent!";
} else {
echo 'Failed';
}
}
exit;
PHP mail and mpdf users any help would be appreciated.
You're learning the hard way - Don't call mail() yourself because you will do it wrong; constructing and sending email messages is horribly complicated and full of pitfalls, as you're finding. Use a library, whether PHPMailer, SwiftMailer Zend_Mail etc, to do it and it will save you a great deal of hassle. You also need to check your two operations separately - first create a PDF, write it to a file and make sure it works correctly; Then write some code that sends a message and check that works; Then get it to send the PDF. Otherwise if you find it's not working, you won't be able to tell which part is broken.
Here is how I did it with MPDF and PHPMAILER.
I also have it so you can attach another file within my form that I made. Hope this helps you along the way.
include("mpdf/mpdf.php");
if ( isset( $_POST['submit'] ) ) {
$mpdf=new mPDF('c','Letter','','','10','10','10','10','','');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML(utf8_encode($html));
$emailAttachment = $mpdf->Output('serviceagreement.pdf', 'S');
//$emailAttachment = chunk_split(base64_encode($emailAttachment));
require("php_mailer/class.phpmailer.php");
$mail = new PHPMailer(true);
try {
$mail = new PHPMailer;
$mail->AddAddress('send email');
$mail->SetFrom('support#myorganization.com');
$mail->AddReplyTo($_POST['email1']);
$mail->Subject = 'Evaluation';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML("*** Form attached! Please see the attached form (.pdf).");
$mail->AddStringAttachment($emailAttachment, $filename = 'serviceagreement.pdf',
$encoding = 'base64',
$type = 'application/pdf'); // attachment
if (isset($_FILES['attached']) &&
$_FILES['attached']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attached']['tmp_name'],
$_FILES['attached']['name']);
}
$mail->Send();
echo "<div style='margin-left:4%;'>Message Sent OK</div>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}}
?>'
I have a simple PHP contact form (Seen Here) that I want to add a file upload option to, so clients can attach an important document and mail it to me using PHP's mail function.
The form works fine on its own, but I can't seem to get the code right for uploading the attachment, storing it temporarily on the server and sending it to me as part of the e-mail. Here is the code I'm using:
<?php
if ($_POST['test'] != '') {
echo 'Unfortunately, by filling out the hidden field, you have been identified as a potential spambot and your message has been terminated.';
} else {
//Validate the name:
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
echo "You forgot to enter your name.<br>";
}
//Validate the phone:
if (!empty($_POST['phone'])) {
$phone = $_POST['phone'];
} else {
echo "You forgot to enter your phone number.<br>";
}
//Validate the e-mail:
if (!empty($_POST['email'])) {
$email = $_POST['email'];
} else {
echo "You forgot to enter your e-mail.<br>";
}
//Validate the message:
if (!empty($_POST['message'])) {
$message = $_POST['message'];
} else {
echo "You forgot to enter a message.";
}
if (!empty($_POST['name']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['message'])) {
// Obtain file upload variables:
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_name = $_FILES['attachment']['name'];
if (file($attachment)) {
// Read the file to be attached ('rb' = read binary):
$file = fopen($attachment,'rb');
$data = fread($file,filesize($attachment));
fclose($file);
// Generate a boundary string:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment:
$headers = "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message:
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// Base64 encode the file data:
$data = chunk_split(base64_encode($data));
// Add file attachment to the message:
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$attachment_type};\n" .
" name=\"{$attachment_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$attachment_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
$body = "$name\n$phone\n$email\n\n$message";
mail("*#*.com", "Starcrest Escrow, Inc. Website - Real Property Sale", $body, $headers);
header("Location: confirm.html");
}
}
?>
When I run this script presently, it forwards me to the confirmation page, but no e-mail appears to be generated at all. What am I doing wrong?
<?php
if (isset($_POST['txtEmail'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xyz#abc.com";
$email_subject = "Subject";
$email_from = "abc#xyz.com";
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['txtName']) || !isset($_POST['txtEmail']) || !isset($_POST['txtAddress']) || !isset($_POST['txtContact']) || !isset($_POST['txtUpload'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['txtName']; // required
$email = $_POST['txtEmail']; // required
$address = $_POST['txtAddress']; // required
$contact = $_POST['txtContact']; // not required
$upload = $_POST['txtUpload']; // required
$email_message = "Form Details are below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message.= "Full Name: " . clean_string($name) . "\n\n";
$email_message.= "Address: " . clean_string($address) . "\n\n";
$email_message.= "Email ID: " . clean_string($email) . "\n\n";
$email_message.= "Contact No.: " . clean_string($contact) . "\n\n";
$email_message.= "File: " . clean_string($upload) . "\n\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
I am trying to send an image attachment by mail using the code below.
I have a problam in emailClass.php file in line 22 fread(...). I know it because if I echo something before that line, it is echoed successfully, and if i echo something after this line it is not echoed which indicates to me that something is wrong with the fread statement.
The fileatt is ok: I checked it and the full path is printed. I tried with a PNG file and with a 500KB JPG. Then I tried with some text files, but still nothing.
How can I correct this problem?
index.php:
<?php
include "emailClass.php";
$testEmail = new email();
$from = 'someone#gmail.com';
$senfTo = 'someone#gmail.com';
$subject = 'email with attachment';
$bodyHead = 'welcome';
$bodyMain = 'bodyMain writings';
$bodyEnd = 'Thank you';
$filePath = '...';
$fileName = 'check.txt';
if($testEmail->emailWithAttach($from,$sendTo,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
{
echo "Email Sent Successfuly";
}
else
{
echo "Failes sending";
}
?>
emailClass.php:
<?php
class email
{
function emailWithAttach($fromaddress,$toAddress,$mailSubject,$mailMessageHead,$mailMessageMain,$mailMessageSign,$filePath,$fileName)
{
$fileatt_name = $fileName;
$fileatt = $filePath.$fileName;
$fileatt_type = "application/octet-stream";
$email_from = $fromAddress;
$email_subject = $mailSubject;
$email_message = $mailMessageHead."<br>";
$email_message .= $mailMessageMain."<br>";
$email_message .= $mailMessageSign;
$email_to = $toAddress;
$headers = "From: ".$email_from;
$file = fopen($fileatt,"rb");
echo $fileatt; //prints ok the correct pathname!!
$data = fread($file,$filesize($fileatt));
echo "check"; //not printing which means something's wrong with line 22 the fread..
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_X{$semi_rand}X";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in mime format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
if (#mail($email_to, $email_subject, $email_message, $headers))
{
return true;
}
}
}
?>
I want to encode my php sending form in my language.
What is wrong with the code? I have added the content-type in the $headers at the end... It is not the whole file, there is also HTML after the PHP, but did not let me to post it
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "gancho_lambev#abv.bg";
$email_subject = "Contact Form...";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form your submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form your submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers .= 'Content-type: text/plain; charset=windows-1251' . "\r\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers); ;}
?>
For sending mails in different language, you can just change the charset:
$headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers .= 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
And be sure the page is encoded as UTF-8 and that if a database is used, the table ( or whole db ) is in "UTF-8 unicode general"
With UTF-8 you can write the characters as they appear, don't use entities.
Do you mean something like this. Hope it helps
Try using a 3rd party library, like phpmailer:
Example: http://phpmailer.worxware.com/index.php?pg=exampleamail
Download: http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/
Don't forget to set the charset, like this:
<?php
require_once '../class.phpmailer.php';
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
$mail->CharSet = 'utf-8';
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
Try the following:
Set the collation of your database/ tables/ rows to UTF-8. UTF8_general_ci should do.
Set the connection between MySQL and PHP to UTF-8. (By executing the query SET NAMES 'utf8' after connecting or by setting the default connection encoding).
Try sending the content-type header with PHP: header("Content-Type: text/html; charset=utf-8");.