Not able to send attachment with my code? - phpmailer

I am using phpMailer i do not know whats wrong with my code, it return fails after execution?
my function which call the phpmailer.php page and takes parameters:
function sendEmail($to,$from,$sender_name="",$subject,$body,$attachement_path){
require("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$host="mail.mysite.com";
$userName="info";
$password="password!";
$mail->IsSMTP(); // set mailer to use SMTP i.e. smtp1.example.com;smtp2.example.com
$mail->Host = $host; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $userName; // SMTP username i.e email id of an email address
$mail->Password = $password; // SMTP password for the specified email address
$mail->From = $from;
$mail->FromName = $sender_name;
$mail->AddAddress($to); //mail,name
$mail->AddAddress("myBCCMailAddress#gmail.com"); // name is optional
$mail->AddReplyTo($to);//to, name
$mail->WordWrap = 50;
$mail->AddAttachment($attachement_path);
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
return false;
}
else{
return true;
}
}
Here is the calling method of my above function:
if(sendEmail($to,"info#mysite.com","My Company Name",$subject,$body,$path)){
echo 'mail sent';
}
else{
echo('mail failed to send '.$to);
}

Here is the script i use.
// phpmailer is required earlier
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->isHtml(true);
$mail->AddAddress($customer_email);
$mail->From = 'billing#example.com';
$mail->FromName = 'MyName';
$mail->Sender = 'mailman#example.com';
$mail->AddBcc('accounting#example.com);
$mail->Subject = 'Invoince;
$mail->Body = 'Some exiting text explaining the customer about his invoice';
$mail->AddAttachment('/var/invoices/'.$invoce_nr.'.pdf', 'Faktura.pdf');
$mail->Body = iconv("UTF-8", "ISO-8859-1",$mail->Body);
if($mail->send()){
return true;
} else {
return false;
}
Also make sure you can send regular emails and then make sure the file you are trying to send is readable.

Related

Google_Service_Gmail mail not send

I am using PHPMailer to send mails with Google_Service_Gmail,but mails are not getting delivered although status is showing SENT.I have created a valid OAth Credentails for Gmail api,which did deliver 5 mails,but then stopped sending mails,although status is showing as SENT
Using Google_Service_Gmail with PHPMailer
try {
$mail = new PHPMailer();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->CharSet = "UTF-8";
$mail->From = $sender;
$mail->FromName = $alias;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $messageText;
$mail->IsHTML(true);
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);
$client = getClient();
$service = new Google_Service_Gmail($client);
$message = $service->users_messages->send($sender, $message);
if ($message->labelIds[0] == "SENT") echo 'Message with ID: ' .$message->getId()."-".$message->labelIds[0];
else echo "Something went wrong...";
return $message;
} catch (Exception $e) {
print "Error" . $e->getMessage();
}
// return null;
}
Returns
Message with ID: 1845b21052f00d8e-SENT
but the e-mail is not delivered.

phpmailer : blank subject if subject contents accent

When I send an email whose subject contains accents in subject, email arrives with blank subject:
$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
$mail->IsSMTP();
$mail->Host = SMTPHost;
$mail->SMTPDebug = SMTPDebug;
$mail->SMTPAuth = SMTPAuth;
$mail->Port = SMTPPort;
$mail->Username = SMTPUsername;
$mail->Password = SMTPPassword;
$mail->SMTPSecure = "ssl";
$mail->IsHTML(true);
$mail->AddAddress($to, $to);
$mail->SetFrom($from, $from);
$mail->AddReplyTo($from, $from);
$subject = "oubliƩ ?"; // if "oublie": it works fine !
$mail->Subject = $subject;
$mail->Send();

Email not send in function codeigniter php mailer

this controller code.
i found problem that "Failed to send password, please try again!" .
What should I do?
$mail_message='Dear '.$row[0]['name'].','. "\r\n";
$mail_message.='Your <b>Password</b> is <b>'.$passwordplain.'</b>'."\r\n";
$mail_message.='<br>Please Update your password.';
$mail_message.='<br>Thanks & Regards';
$mail_message.='<br>IT Ardiles';
$mail = new PHPMailer;
$mail->IsSendmail();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = '';
$mail->Port = 587;
$mail->Host = "mail.xxxxx.co.id";
$subject = 'Forgot Password';
$mail->AddAddress(xxx#xxxxx.co.id);
$mail->IsMail();
$mail->From = 'it#xxxxx.co.id';
$mail->Password = 'xxxxx';
$mail->FromName = 'IT';
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $mail_message;
$mail->Send();

SMTP ERROR: Failed to connect to server: With PHPMAILER in Dreamhost

I can not send mail using the PHPMailer lib. My site is hosted on dreamhost,
with SMTP gmail I can send more properly to the configuration provided by the hosting the only return is:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP connect() failed.
My code:
<?php
date_default_timezone_set('Etc/UTC');
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = trim(stripslashes($_POST['name']));
$from = trim(stripslashes($_POST['email']));
$subject = trim(stripslashes($_POST['subject']));
$message = trim(stripslashes($_POST['message']));
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'mail.example.com.br';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'e-mail#example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Debugoutput = 'html';
$mail->isHTML(true);
$mail->setFrom($from, $name);
$mail->addReplyTo($from, $name);
$mail->addAddress('email#example.com');
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
`
I managed to solve the problem, first replaces the e-mail sent to an e-mail sender#example.com own client, then tried some documentation of dreamhost, made the necessary changes, the code:
date_default_timezone_set('Etc/UTC');
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$subject = trim(stripslashes($_POST['subject']));
$name = trim(stripslashes($_POST['name']));
$from = trim(stripslashes($_POST['email']));
$body = trim(stripslashes($_POST['message']));
$mail = new PHPMailer;
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 1;
$mail->Host = 'mail.example.com.br';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'sender#example.com.br';
$mail->Password = 'password';
$mail->Debugoutput = 'html';
$mail->isHTML(true);
$mail->setFrom('sender#dexample.com.br', 'DontAnswer');
$mail->addReplyTo($from, $name);
$mail->addAddress('contact#example.com.br');
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
In dreamhost documentation says it should be repeated sending e-mail in setFrom () method; after these changes the form worked perfectly.

Unable to send email to multiple user

Right now I am doing a system. This system will send notification if educators have update a quiz. I want to send email to multiple user. I have refer this question 17860172 and Its solution but Its not works.
This is my code:
$result = mysql_query("SELECT * FROM student WHERE className='$classname'");
while ($rec= mysql_fetch_array($result))
{
$title = "Quiz Information";
$body = "Your quiz have been updated";
$email = $rec['stuEmail'];
}
include_once("mailer/mailer.php");
okey, for the mailer.php :
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "***";
$mail->Password = "***";
$mail->From = "iii#gmail.com";
$mail->FromName = "i System";
$mail->AddAddress($email);
$mail->Subject = $title;
$mail->MsgHTML($body);
if (file_exists($path)){
$mail->AddAttachment($path);
}
$mail->WordWrap = 50;
$mail->Send();
I want to send to all email in database based on className but when I click submit button the email doesn't send at all.
You can try this :
$result = mysql_query("SELECT * FROM student WHERE className='$classname'");
while ($rec= mysql_fetch_array($result))
{
$title = "Quiz Information";
$body = "Your quiz have been updated";
$email = $rec['stuEmail'];
mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "***";
$mail->Password = "***";
$mail->From = "iii#gmail.com";
$mail->FromName = "i System";
$mail->AddAddress($email);
$mail->Subject = $title;
$mail->MsgHTML($body);
if (file_exists($path)){
$mail->AddAttachment($path);
}
$mail->WordWrap = 50;
$mail->Send();
}

Resources