I am using PHPMailer to send HTML emails. I first set the HTML body of the email using HTML and PHP variables after first calling ob_start.
Here's my code:
<?php
ob_start();
?>
..... a bunch of VALID HTML
<?
$body = ob_get_contents();
require_once('class.phpmailer.php');
// Send to Me
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport
try {
$mail->AddReplyTo('info#example.com', 'Company Name');
$mail->AddAddress('me#example.com', 'My Name');
$mail->SetFrom('info#example.com', 'Company Name');
$mail->Subject = "Contact Form Confirmation";
$mail->AltBody = "This is a contact form submitted through the main website.";
$mail->WordWrap = 50; // set word wrap
$mail->Body = $body;
$mail->IsHTML(true); // send as HTML
$mail->Send(); // Try to send email
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// end try
For some reason, this line:
$mail->Body = $body;
causes the email not to send. If I replace it with:
$mail->Body = "This is a test email.";
the email sends perfectly.
If my HTML contained within $body is just a valid HTML page with CSS in the head, and no Javascript or anything, why won't the email send?
Is there another way to do this? Please help!!
Turns out, for some reason, the emails were not sending if they contained a fax number. Totally strange. I tested my entire markup, and after ONLY removing a 10 digit phone number, the emails finally went through. I'm not a newbie - literally that's the only text I removed and the emails sent fine.
HAS ANYONE SEEN THIS BEFORE??
Related
Am sending raw code to gmail via phpmailer. when I tried to run the code, the code is formatted in the gmail instead of appearing as raw codes.
I want the code in the message body to appear the way it is in the email as per below
. In order words, how do i make the code to appear as raw codes in gmail
<html>
<script>
alert('This is your Code');
</script>
<body><p>
<b>Display as raw codes.</b>
</p>
</body></html>
below is the code
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email_subject='my codes';
$recipient_email='reciever#gmail.com';
$recipient_name='Ann';
$sender_name='john';
$mycode = "<html>
<script>
alert('This is your Code');
</script>
<body><p>
<b>Display as raw codes.</b>
</p>
</body></html>";
// Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings goes here
$mail->setFrom('nancy#gmail.com', "$sender_name");
$mail->addAddress($recipient_email, $recipient_name); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email_subject;
$mail->Body = $mycode;
//$mail->AltBody = ""; // for Plain text for non-HTML mails
$mail->send();
echo 'Message successfully sent';
} catch (Exception $e) {
echo "Message not sent. Error: {$mail->ErrorInfo}";
}
?>
$mail->isHTML(true); // Set email format to HTML
Don't do that.
If you want the message to be displayed as plain text, then don't tell the client that it should be displayed as HTML.
$mail->isHTML(false);
I am using PHPMailer to send mail in my Php program. Email is working fine but it showing mailed by address in from area.How can i hide these mailed by in PHPMailer.and also via details from Email from area.
When i use php mail() function like below its removing mailed by details.But how can i do it in PHPMailer
mail('info#example.com', 'Subj', "Message", $headers, '-freturn#yourdomain.com')
Here is php mailer code
<?php
require_once 'phpmailer/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
$body = "Heloooo";
try {
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('to#example.com', 'John Doe');
$mail->SetFrom('info#example.ae', 'Info');
$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($body);
$mail->Send();
echo "Message Sent OK<p></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!
}
?>
You can just remove the below two lines.
$mail->SetFrom('info#example.ae', 'Info');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
Then the mail details will show from as "Root User root#localhost".
But it will show from which server you are sending it.
Even you use the SMTP with phpmailer, you can do the same.
I am using phpmailer to send email from my online website.but when i sending it showing default server address in from address
<?php
require_once 'phpmailer/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->AddAddress('to#domain.com', 'Jo');
$mail->SetFrom('info#mydomain.com', 'Info');
$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('Helooo');
$mail->Send();
echo "Message Sent OK<p></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!
}
?>
now it showing like
i want to replace red marked address to info#mydomain.com....how can i do that...this red marked address is default server address i think. i tried to set address using phpmailer from tag, but no change
try this to change from address
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: info#domain.com";
mail($to,$subject,$txt,$headers);
?>
It's very common for hosted email services (e.g. gmail) to not let you change the From address, or restrict you to pre-set aliases, not allowing arbitrary addresses.
It also looks like you've based your code on an obsolete example, and you're probably using an old version of PHPMailer, so get the latest version.
I am creating a email service which send different image to different person using phpmail.I can send mail and attachment as i require but when it comes to dynamically adding embedded image in body of mail.
I wont be able to achieve success.
<?php
require_once('class.phpmailer.php');
// multiple recipients
$to = $arr['Contact.Email']; // note the comma
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try{
$mail->SetFrom($arr['Contact.Email'], 'First Last');
$mail->AddAddress($arr['Contact.Email'], 'John Doe');
$mail->Subject = 'PHPMailer Test';
$mail->AddEmbeddedImage($arr['ContactId'].'.png', 'my-attach');
$mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src=cid:my-attach\'> Here is an image!';
$mail->AddAttachment($arr['ContactId'].".png"); // this is a regular attachment (Not inline)
$mail->Send();
echo "Message Sent OK<p></p>\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
I can embed one image but when i try to send different image for different user i get an error.I searched everywhere but i did not get any satisfactorily answer.
any help will be appreciated.
You have an error at:
$mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src=cid:my-attach\'> Here is an image!';
You need to skip the \' after attach.
You also need to set the message to html format by adding:
$mail->IsHTML(true);
Read more about embedding images in the docs here: http://phpmailer.worxware.com/?pg=tutorial
I have a problem sending plain text emails using PHPMailer.
I have text that I read from a text file and mail it to mail recipient via PHPMailer
When the recipient gets the actual email, the formatting of the mail is not like in the text file, everything is in one line, no new lines and tabs are included in the email that I send. Text wrapping is totally off.
Code:
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "test#test.com";
$mail->AddAddress($address, "John Doe");
$mail->SetFrom(EMAIL_TEST_FROM);
$mail->AddReplyTo(EMAIL_TEST_REPLY);
$mail->Subject = $action." REGISTRATION ".$formName.$tld;
$mail->From = EMAIL_TEST;
$mail->MsgHTML(file_get_contents($newFile));
if($mail->Send()){
return true;
}
You are setting $mail->MsgHTML() to a plain text message, and since whitespace formatting is ignored in HTML, you're getting an inline text.
I haven't used PHPMailer for a while, but from memory try:
$mail->Body = file_get_contents($newFile);
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "test#test.com";
$mail->AddAddress($address, "John Doe");
$mail->SetFrom(EMAIL_TEST_FROM);
$mail->AddReplyTo(EMAIL_TEST_REPLY);
$mail->Subject = $action." REGISTRATION ".$formName.$tld;
$mail->From = EMAIL_TEST;
// Very important: don't have lines for MsgHTML and AltBody
$mail->Body = file_get_contents($mailBodyTextFile);
// $mail->Body = $_POST["msg"]; //If using web mail form, use this line instead.
if($mail->Send()){
return true;
}
Try below code which works fine:
try {
$mail->AddAddress('jitpal#domain.com', 'Jit Pal');
$mail->SetFrom('testuser#domain.com', 'Test User');
$mail->Subject = "All machine's tests.";
$mail->Body = "All machine's tests working fine.";
$mail->Send();
echo "<br/>Message sent successfully...<br/><br/>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}