AddStringAttachment with PDF in PHPMailer not work - php

I had used phpmailer for send email. Now i have to attach pdf file in email. It attached pdf but that pdf could not open. and is shows there is problem in format. Is AddStringAttachment doesn't work for pdf? What should i do?
require_once('././php_mailer/class.phpmailer.php');
$this->load->helper('mail_html');
$body2 = "You are receiving this email because the Transfer Application submitted for transferring to g is missing required documentation.
Please see the note below for details. The transfer application will not be reviewed until all of the necessary materials are received by the UHSAA.
";
$body = 'test';
$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->Host = 'smtp.gmail.com'; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com'; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'nabina.smartmobe#gmail.com'; // SMTP account username
$mail->Password = '*******'; // SMTP account password
$mail->AddAddress('nabina.shahi#gmail.com','nabina');
$mail->SetFrom('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->AddReplyTo('no-reply#nayacinema.com.np', '');
$mail->IsHTML(true);
$mail->Subject ='NayaCinema: Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->AddStringAttachment($body2, 'Filename.pdf', 'base64', 'application/pdf');
if($mail->Send()){
echo 'sent'; die;
return true;
}else{
echo ' not sent'; die;
return false;
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thank you

You can create a .pdf with another PHP class TCPDF. You can see how it goes in examples. And if you dont want to save the PDF in the server you can send like you did, you only need to change the output line of the TCPDF $pdf->... codes, like this:
$attachdata = $pdf->Output('foo.pdf','S'); // return the document as a string (name is ignored)
then you can send it:
$mail->AddStringAttachment($attachdata, 'Filename.pdf');

Related

Issue with Moving phpmailer Page from Localhost to Server

I have been developing a page that will automatically send emails from a Gmail account to mail accounts of various origin for specific users of my site. I have this working perfectly on Localhost. The mails are received by the users and also deposited in the "Sent Items" of the Gmail account.
However, as soon as I migrate this to the server we are using it no longer works. The code is as follows :-
$mail = new PHPMailer();
try
{
$mail->CharSet = "UTF-8";
// telling the class to use SMTP
$mail->IsSMTP();
// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPDebug = 0;
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the servier
$mail->SMTPSecure = "tls";
// sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";
// set the SMTP port for the GMAIL server
$mail->Port = 587;
// GMAIL username
$mail->Username = "xxxxxxx#gmail.com";
// GMAIL password
$mail->Password = "P4ssw0rd";
//Set reply-to email this is your own email, not the gmail account
//used for sending emails
$mail->SetFrom($from);
$mail->FromName = "XXX";
$mail->AddReplyTo('xxxxxxx#gmail.com' , 'XXX');
// Mail Subject
$mail->Subject = $subject;
//Main message
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
$mail->Send();
}
catch (phpmailerException $e)
{
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
echo $e->getMessage(); //Boring error messages from anything else!
}
Setting the Debug to 1 on localhost produces the expected errors if something goes wrong. However, nothing seems to happen on the Server. Please help.

How can I remove mailed-by and signed-by tag from email

I am using php-mailer to send email.
The mail goes succesfully however in each email I get "mailed-by: gamil.com" and signed-by: "gmail.com" in mail header.
How can I remove mailed-by and signed-by tag from email ?
Here is my code
require_once('././php_mailer/class.phpmailer.php');
$body = html($msg);
$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->Host = 'smtp.gmail.com'; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com'; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'nabina.smartmobe#gmail.com'; // SMTP account username
$mail->Password = '******'; // SMTP account password
$mail->AddAddress($to, 'Nabina');
$mail->SetFrom('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->AddReplyTo('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->IsHTML(true);
$mail->Subject ='NayaCinema: '.$subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
if($attachment){
$mail->AddAttachment($attachment);
}
if($mail->Send()){
//echo 'sent'; die;
return true;
}else{
//echo ' not sent'; die;
return false;
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thank You

PHPMailer not sending CC or BCC

I have been testing the following code for hours. The email will send to the addresses added through $mail->AddAddress() and in the received email it states the cc but the person cced does not receive the email. I have looked everywhere and can not find a solution to why this is happening. I have run tests and all variables are being submitted to this code properly.
My server is running Linux Red Hat
My Code:
require_once('../smtp/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$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->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server 465 or 587
$mail->Username = $username; // GMAIL username
$mail->Password = $password; // GMAIL password
// Add each email address
foreach($emailTo as $email){ $mail->AddAddress(trim($email)); }
if($cc!=''){ foreach($cc as $email){ $mail->AddCC(trim($email)); } }
if($bcc!=''){ foreach($bcc as $email){ $mail->AddBCC(trim($email)); } }
$mail->SetFrom($emailFrom, $emailName);
$mail->AddReplyTo($emailFrom, $emailName);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($content);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo'1';exit();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
$address = "xxxxx#gmail.com";
$mail->AddAddress($address, "technical support");
$address = "yyyyyy#gmail.com";
$mail->AddAddress($address, "other");
$addressCC = "zzzzzz#gmail.com";
$mail->AddCC($addressCC, 'cc account');
$addressCC = "bcc#gmail.com";
$mail->AddBCC($addressCC, 'bcc account');

Sending Emails With phpmailer From A php Project?

i want to send emails with phpmailer.
for doing that i searched google and i found the blow link :
http://phpmailer.worxware.com/
so i download their libraries from here (by download link in their site , php 4-5):
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/
after that i refer to this example : (mean advanced using smtp)
http://phpmailer.worxware.com/index.php?pg=exampleasmtp
<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$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->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$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(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
but i could n't find class.phpmailer.php file for this line -> require_once('../class.phpmailer.php'); from dowloaded file.
did i miss something?
would u plz help me for that?
best regards.
The "../" means "one directory level higher". That means that it should look for the file class.phpmailer.php in the directory one higher than that where this script is executing from. If that's not where you saved class.phpmailer.php relative to your code, then adjust the path.
Try this one.
<?php
include "/home/tnehme/public_html/smtpmail/classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // 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->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$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(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
I'm using PHPMailer with the 5.5.10 and its working really well.
It was very easy to get up and running. Copy files into a directory (alongside your PHP files)
'include' it in your script... and use the example code they give you as a basis for your email.
To get the files visit:
https://github.com/PHPMailer/PHPMailer
good luck.
I also here good things about SwiftMailer.

how to use php mail on fedora

I'm running fedora core 9 with php 5.2.6. I want to send a simple email from a form using the php mail command.
Do I need to configure my system to be an smtp server? If so do I have to load something like pear?
I've read that pear is installed by default. however when I go to /usr/lib/php/pear , the folder is empty.
When I go to install pear with yum install php-pear none of the mirrors are available.
Does anyone have any suggesions on what mail service to use? If it's installed by default and where I can figure out how to configure.
Thanks!
Joe
This the code I'm using:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = "587"; // set the SMTP port for the GMAIL server
$mail->Username = "joestestemail#gmail.com"; // GMAIL username
$mail->Password = "joeiscool"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('joestestemail#gmail.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('joe12345#mailcatch.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$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!
}
?>
I would strongly recommend using a library like PHPMailer to send emails.
You can use it with the server's own mail service or with any other server on the internet (your ISPs, Gmail, etc).
For a better idea, check this example from their website:
require_once('../class.phpmailer.php');
$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->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$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(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$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!
}
Edit:
Following up on your comment..
For a simple GMail example, try this:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('name#yourdomain.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$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!
}
Edit2:
The reason why you're getting this error is because you have an extra ' on your string:
Replace
$mail->Host = "'smtp.gmail.com";
with:
$mail->Host = "smtp.gmail.com";
The easiest way to get mail sending up and running is to install and configure postfix. You can use:
yum install postfix
And check out the documentation here:
http://www.postfix.org/docs.html
You can use the LAMPStack from BitNami. It comes with an easy installer that installs everything you need and you'll be running in minutes.

Categories