The isHTML is already set to True. But it doesnt work in the email i receive. I just get the sample html from a tutorial.
<?php
require("phpmailertest/class.phpmailer.php");
$x=$_SESSION['items'];
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
// $mail->SMTPDebug = 2;
$mail->From = "benedictpayot#gmail.com";
$mail->FromName = "BravoTech Solutions";
$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected
$mail->SMTPAuth = true;
$mail->Username = "benedictpayot#gmail.com"; // SMTP username
$mail->Password = "Ichthys030313!"; // SMTP password
$mail->AddAddress($_SESSION['email_address']);
$mail->IsHTML(true);
$mail->Subject = "Mail Test";
$mail->Body = '<html><body>';
$mail->Body = '<table style="border-color: #eee;"';
$mail->Body = '<tr>
<td>JALOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
<td>OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
<tr>
<td>BENEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDICT
<td>PAYOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT
</table>';
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
session_destroy();
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
You can set Body (and AltBody) manually to any value you like. msgHTML() is a convenience function to set moth of them and optionally apply an html to text conversion to generate your plain text version. It also sets isHTML, rewrites image URLs and various other things - but you don't have to use it.
In your code you're saying:
$mail->Body = '<html><body>';
$mail->Body = '<table style="border-color: #eee;"';
$mail->Body = '<tr>...
Which should be:
$mail->Body = '<html><body>';
$mail->Body .= '<table style="border-color: #eee;"';
$mail->Body .= '<tr>...
otherwise you're just overwriting the contents of Body each time.
You should base your code on the gmail example bundled with PHPMailer - it looks like you are using an old one from somewhere else.
I'm not sure (correct me if I'm wrong) but I was always setting body like this:
$mail->MsgHTML($body);
Related
I'm beginner on php. and now I want to set up $contact_name display in send email ? Below my code and please fix my code. email receive always show "$contact_name" not "my name"
<?php
if(isset($_POST['send_email']))
{
require_once('../src/PHPMailer.php');
require_once('../src/SMTP.php');
require_once('../src/Exception.php');
require_once('../vendor/autoload.php');
$mail = new phpmailer\phpmailer\PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$contact_name = $_POST['contact_name']; // required
$mail->From = 'mygmail#gmail.com';
$mail->FromName = 'No Reply';
$mail->AddAddress('togmail#gmail.com'); // Add a recipient
$mail->AddAddress; // Name is optional
$mail->IsHTML(false); // Set email format to HTML
$mail->Subject = 'Add Contact';
$mail->Body = 'Dear All, ($contact_name) with $contact_ext has been addeds.';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
}
?>
Change
$mail->Body = 'Dear All, ($contact_name) with $contact_ext has been addeds.';
To
$mail->Body = "Dear All, ($contact_name) with $contact_ext has been addeds.";
See the double quotes instead of single quotes.
Using double quotes makes php replace the variable name by its value. Single quotes makes php treat the string between the quotes as literal.
Change:
$mail->body
with
$mail->MsgHTML($message);
It Works Now. Thanks
I need to send form data to mail in tabular format. for this i'm using using PHPMailer. when I am trying to display values that are selected by user it only display as array. can somebody help me to find what is wrong in this code?
Below is the part of HTML
<input type="checkbox" name="Favorite_Drink[]" value="Vodka">Vodka
<input type="checkbox" name="Favorite_Drink[]" value="Vodka">Vodka
<input type="checkbox" name="Favorite_Drink[]" value="Bear">Bear
Here is PHP part
$Favorite_Drink = $_POST['Favorite_Drink'];
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'd11288#gmail.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('a11288#gmail.com', 'User');
$mail->addAddress('al#gmail.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('a11288#gmail.com', 'Information');
$mail->addCC('a11288#gmail.com');
//$mail->addBCC('bcc#arshad.com');
$mail->Subject = 'Here is the subject';
//$mail->Body = "Name of patient is ";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);
$mail->Subject = ' Test';
$mail->Body = <<<EOF
<html><body>
<br>
<table rules="all" style="border-color: #666;" cellpadding="10">
<tr style='background: #eee;'>
<td>Favorite_Drink </td>
<td> $Favorite_Drink</td>
</tr>
</table>
</body>
</html>
EOF;
//Altbody is used to display plain text message for those email viewers which are not HTML compatible
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
$Favorite_Drink = $_POST['Favorite_Drink']; - this variable gets array of data from form.
<td> $Favorite_Drink</td> - if you want print array as text you should, use something like this implode(', ', $Favorite_Drink)
I've been using the default mail function of PHP before but decided to use another service for emailing features. I used PHPMailer but it is not working.
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Host = gethostbyname('smtp.gmail.com');
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "xxx#gmail.com";
$mail->Password = "xxx";
$mail->setFrom('xxx#gmail.com', 'Name');
$mail->addReplyTo('xxx#gmail.com', 'no-reply');
$mail->addAddress('xxx#gmail.com', 'Name');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->AltBody = 'This is a plain-text message body';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The code also doesn't echo any errors.
It does not echo any errors, but I think it also doesn't echo 'Message sent!'
Try the following
Append the brackets
$mail = new PHPMailer();
Set debug to messages AND ERRORS for testing purpose
$mail->SMTPDebug = 1;
To send HTML Body, either after setting $mail->Body call
$mail->IsHTML(true);
Or use the Method to set HTML body which will combine both of them:
$mail->MsgHTML('This is the HTML message body <b>in bold!</b>');
Try this and see if it works
EDIT:
Also look that the casing is as suggested (I am not sure if this may give errors, but just to be sure)
$mail->SetFrom
$mail->AddReplyTo
$mail->AddAddress
I don't know if my code is the issue or if I had configured Zoho's SMTP's settings incorrectly.
Basically I want the ability to dynamically send emails with a simple php function like for example
phpMail("to#example.com", $subject, $body, "from#example.com", "replyto#example.com");
This is my PHPMailer.php script (it sees the function and its settings)
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**REMOVED**'; // SMTP username
$mail->Password = '**REMOVED**'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true); // Set email format to HTML
// SYTAX: phpMail($from, $reply, $to, $subject, $body);
function phpMail($to, $subject, $body, $from = "from#example.com", $reply = "replyto#example.com") {
if (isset($from))
$mail->From = $from;
$mail->FromName = "testing";
if (isset($to))
$mail->addAddress($to);
if (isset($reply))
$mail->addReplyTo($reply);
if (isset($subject))
$mail->Subject = $subject;
if (isset($body))
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Now the issue with this is that, I'm not receiving emails nor am I receiving any errors / messages but it's also not sending me an email.
When you make a PHP function, make sure your objects aren't outside of your function.
My error wasn't that though, it was basically my own stupidity which had nothing to do with the code but more of my "form post data".
The following configuration works for me using zoho mail.
Give it a try:
$mail=new JPhpMailer;
$mail->IsSMTP();
$mail->Host='smtp.zoho.com';
// Enable this option to see deep debug info
// $mail->SMTPDebug = 4;
$mail->SMTPSecure = 'ssl';
$mail->Port='465';
$mail->SMTPAuth=true;
$mail->Username='your_email_address';
$mail->Password='your_eamil_address_password';
$mail->isHTML(true);
$mail->SetFrom('your_email_address','Your Name');
$mail->Subject='PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody='To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress('destination_email_address','John Doe');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I'm using PHPMailer to automatically send an order acknowledgement as an HTML formatted email. Everything is working as expected except that the formatting of the acknowledgement isn't correct. I included my style sheet with an 'AddAttachment' line which seems to have fixed the header of the acknowledgement form, but the rest of the form still isn't right. Has anyone run into this situation before and know what I need to do to fix it? My edited program code follows in case it'll help!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Port = 25; // set the SMTP port
$mail->Host = "<<smtp server>>"; // SMTP server
$mail->Username = "<<username>>";
$mail->Password = "<<password>>";
$mail->From = "<<email address>>";
$mail->FromName = "<<name>>";
$mail->AddAddress("<<email address>>");
$mail->Subject = "Acknowledgement Form";
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->IsHTML(true);
$mail->Body = file_get_contents('<<acknowledgement form page>>');
$mail->AddAttachment('printer.css'); // attach style sheet
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
You should'nt work with external CSS files in emails... Not many of the email-clients can handle that and not all of your customers use email-clients like Outlook or Thunderbird. Instead, you should disgn you html-code "90s style" :-P
e.g. <p style="width: 200px; text-align: center;">