Send mail with plaintext alternative using phpmailer - php

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.

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');

PHP Mailer in Codeigniter Setfrom is not sending email to gmail,hotmail,ymail but setfrom is sending other custom business email to e.g abc#ab.com

mail is only sent when I used business email like(abc#abc.com) but when use gmail,Hotmail or yahoo mail it gives below error.
Controller
$this->load->library("mailer");
$mail = $this->mailer->load();
$mail->isHTML(true);
$mail->IsMAIL();
$mail->setFrom('ballers999#gmail.com', 'Ballers');
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
error message could cot instantiate mail function

Unable to send email from godaddy server to godaddy or zoho mail

I am trying to send email from my website to my godaddy mail and zoho mail but its not working.
I tried it on my gmail account and its working fine.
I am using phpmailer.
MY CODE-
require_once "PHPMailerAutoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "test#deltware.com";
$mail->FromName = "Himanshu Mishra";
$mail->addAddress("my godaddy webmail"); //Recipient name is optional
//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";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Please help!!!!!
Your From syntax is wrong
Instead of
$mail->From = "test#deltware.com";
$mail->FromName = "Himanshu Mishra";
It should be
$mail->setFrom('test#deltware.com', 'Himanshu Mishra');
Check this link https://github.com/PHPMailer/PHPMailer

PHPMailer - adding attachment breaks charset

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?

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