How to save mail body as html - php

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.

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

EasyPeasyICS is not sending ics file with my email

In codeigniter, working with phpMailer, I am trying to use EasyPeasyICS to help build an email with an ics file and this is a portion of my code:
$invite = new EasyPeasyICS();
$invite->addEvent(strtotime($appointment_data['start_datetime']),strtotime($appointment_data['end_datetime']),
$service_data['name'],$message." ".$appointment_link,$appointment_link);
$mail = new PHPMailer();
$mail->From = $company_settings['company_email'];
$mail->FromName = $company_settings['company_name'];
$mail->AddAddress($receiver_address); // "Name" argument crushes the phpmailer class.
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $title;
$mail->Body = $email_html;
$mail->Ical = $invite->render(false);
if (!$mail->Send()) {
throw new Exception('Email could not been sent. Mailer Error (Line '
. __LINE__ . '): ' . $mail->ErrorInfo);
}
return TRUE;
}
So, what I get is my HTML email and no ICS file. If I modify $mail->Ical = $invite->render(true) then the ics file downloads to me and I see the ICS file wrapped around my HTML as it is supposed to be and it looks great. Why is it not sending out with the email?
Even with the latest version (5.2.22), you must set AltBody if you want to parse Ical. Because alternative (alt) message type is required to Ical to be loaded.
But if you specify Ical, your email WILL BE an event, and will be considered as it by email clients.

php code for sending a mail using smtp [duplicate]

This question already has answers here:
Reintroduce $HTTP_POST_VARS in PHP 5.3
(4 answers)
Closed 8 years ago.
this is my code am not receiving any mail through this code.
$email = $HTTP_POST_VARS[email];
$mailto ="nr.shubha#gmail.com";
$mailsubj = "Form submission";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Email form the web site form:\n";
echo $HTTP_POST_VARS;
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
$mailbody .= "$key : $val\n";
}
if (!eregi("\n",$HTTP_POST_VARS[email])) {
mail($mailto, $mailsubj, $mailbody, $mailhead);
}
print_r($HTTP_POST_VARS);
I strongly suggest you to use premade classes like https://github.com/PHPMailer/PHPMailer
It's much "spam safe" than using plain PHP code, and helps you with image embedding, attachments, etc...
I recommend you to use PHPMailer Library. Works great.
Here is a sample of my work
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
//This part is for authentication
$mail->Host = "mail.example.com"; // specify main and backup server
$mail->SMTPAuth = "SMTPAuth"; // turn on SMTP authentication
$mail->Username = "user#example.com"; // SMTP username
$mail->Password = "****"; // SMTP password
//the receiver will see that the sender address is this
$mail->From = "ihavesentit#example.com";
$mail->FromName = "The Sender";
//You can use AddAdress() and AddCC() functions several times for different receivers
$mail->AddAddress("receiver#example.com");
$mail->AddCC('another_receiver#example.com');
$mail->AddReplyTo("reply-to-me#example.com");
$mail->WordWrap = 50; // set word wrap to 50 characters (arbitrary)
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "mail subject";
$mail->Body = "<p>mail content will be typed here</p>";
$mail->AltBody = "will be used if the receiver does not accept HTML content e-mails";
//final and the most important action
$mail->Send();
This is Work only on the live server when it will host. It will not work on the localhost.
Because the SMTP only work on the live hosted server not on the "localhost".

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.

Can I send a php variable as an attachment

i'm Using livedoc to create a pdf. I've suceeded in doing so without saving the file to the server, and thats the objective here.
Can I attach the file I've created which is on a variable via phpmailer and email it.
require("php_mailer/phpmailer.inc.php");
$mail = new phpmailer;
$mail->From = "noreply#credireto.com";
$mail->FromName = "Crédireto";
$mail->Host = "smtp1.site.com;smtp2.site.com"; // specify main and backup server
$mail->AddAddress("$email"); // name is optional
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("$data","Simulação.pdf"); // add attachments
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Simulação Crédireto";
$mail->Body = filegetcontents("emailagradecimento");
$mail->Send(); // send message
The variable $data contains the file I want to send.
I Solved It. Created a temp file and sent the attachment. Let me know if you need any help with this.

Categories