PHPMailer - adding attachment breaks charset - php

I've got a simple e-mail being sent from my script using PHPMailer:
include_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsMail();
$mail->IsHTML(true);
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->Body = $content;
the above is sent perfectly, even if both subject and body contain foreign characters.
However if I add an attachment:
$mail->AddAddress($email);
$mail->AddAttachment("file.pdf");
$mail->Subject = $subject;
the characters are replaced by ÅÄ
I've been using PHP Mailer for years but it's the first time I have to use both attachments and foreign characters in the message. Strangely the subject is fine, just the body of the e-mail is broken.
Has anybody experienced same/similar problem?

Related

Send multiple email address using phpmailer function

I have a problem sending multiple email addresses functions. If I send a single email address, I can work. If I send multiple emails to the receiver, they cannot receive my message. I am using the PHPMailer function to do the email function with XAMPP. I am using the PHP array function to put the receiver's address in the array and use the while function to loop the receiver address to send it.
Below is my coding:
$address = array('st9overfindsolution#gmail','st7overfindsolution#gmail');
require 'class/class.phpmailer.php';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
// $mail->SMTPDebug = 1;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = '1233aqqq';
$mail->SMTPSecure = 'ssl';
$mail->From = $_POST["email"];
$mail->FromName = $_POST["name"];
$mail->AddCC($_POST["email"], $_POST["name"]);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
while(list ($key, $val) = each($address)){
$mail->AddAddress($val);
}
What I've tried?
1.I put all receiver email addresses in the array $address = array('st9overfindsolution#gmail','st7overfindsolution#gmail'); and use below while function code, but cannot work.
2.If send single email address without using while function, just can work, like below coding:
Hope someone can guide me on how to solve this problem. Thanks.
i have created new function named "sendmyemail" and inserted the code inside this function, so you can sent multiple emails by calling this function with email address.
<?php
/**
* This example shows sending a message using a local sendmail binary.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
function sendmyemail($whotoEmail){
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($whotoEmail);
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to
embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent to '.$whotoEmail.'!';
}
}
sendmyemail('abcduser#gmail.com');
sendmyemail('efguser#gmail.com');
sendmyemail('hijkuser#gmail.com');
sendmyemail('lmnouser#gmail.com');

Sending e-mail with attachment to gmail never gets received, using phpmailer

Something weird is going on when i'm trying to send an e-mail with attachment to gmail, using phpmailer.
Sometimes the email received after a delay of 5 minutes and sometimes it is never received. From phpmailer debugger i get that the email is actually sended, so i assume it has something to do with the attachment getting blocked by gmail.
The attachment is a zip file containing only .jpg and .pdf files.
If i remove the addAttachment('path/to/file') everything works fine.
The weird thing is that sometimes, even with the attachment, the email received immediately and other times the email never gets received.
Here is the code
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'my_host';
$mail->Port = 'my_port';
$mail->SMTPAuth = true;
$mail->Username = 'my_username#domain.com';
$mail->Password = 'my_password';
$mail->setFrom('my_username#domain.com', 'my_username#domain.com');
$mail->addAddress('test#gmail.com', 'test#gmail.com');
$mail->Subject = $subject;
$mail->msgHTML($msg);
$mail->AltBody = $subject;
$mail->addAttachment($zipfilename);
if($mail->send()){
echo "ok";
}else{
echo "error";
}
I don't have any clue why this is happening so any help will be appreciated.
EDIT:
I found out that with two changes everything works fine.
The changes are the setting
$mail->SMTPSecure = 'tls'
and i had a path like this ./path/to/file and changed it to 'path/to/file'.

Send mail with plaintext alternative using phpmailer

I have the following php code for sending email with phpmailer
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->SetFrom($from, $sender_name);
$mail->Subject = $subject;
$mail->AddAddress($to, "test");
$mail->Body = $body; //$body is an html content
$mail->Send();
This will send an email.I need to send plain text alternative with this,for an email client does not have HTML support.
Is possible?
Is possible to do with php mail() function?
PHPMailer has a property AltBody, so you could add some plain text like this:
$mail->AltBody = strip_tags($body);
See the documentation.

PHP mailer Character encoding issue

I m facing some character encoding issues like this:
D\\\'Huison-Longueville Référence
Original Text was :
D'Huison-Longueville Référence
Here is my PHP Mailer script:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsMail();
$mail->Subject = $subject;
$mail->IsHTML(true);
$mail->Body = $email_body;
$mail->From='support#xyz.com';
$mail->FromName= "Support Team";
$mail->AddAddress($toEmail);
$mail->Send();
I had the same problem. The charset was not being changed although I was specifying the new one like you with:
$mail->CharSet = 'UTF-8';
Since you use "IsMail" you have to go to the folder where class.phpmailer.php resides (generally in the same folder as the main phpmailer.php file) and edit it. There you will see the default values for the mail and you will see that it is set to "ISO-8859-1".
Change
public $CharSet = 'ISO-8859-1';
to
public $CharSet = 'UTF-8';
Change it to "UTF-8" or any other charset you want.

How to save mail body as html

I am looking for a way to save my template file which I use as a certificate snf sttsch it to my my.
Below is the code to replace fields in my template with one I have queried.
require("class.phpmailer.php");
//placeholders array
$pholders = array('replace_name', 'replace_number', 'replace_title');
//Certificate replace values array
$failedValues = array($FullName, $TestNo, $Title);
$mail_body = file_get_contents("failed.html");
$mail_body = str_replace($pholders,$failedValues,$mail_body);
//mailer
$mail = new phpmailer;
$mail->IsSMTP(); // set mailer to use SMTP
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Results";
$mail->Body = $mail_body;
$mail->AddAttachment("cpd.html");
I have omitted code for the smtp setting. The file cpd.html has to be generated and attached. I am looking for a way on how to generate it and save it for attaching it to the mail.

Categories