Google_Service_Gmail mail not send - phpmailer

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.

Related

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();
}

php mailer will send mail but not include content

This mailer works with go daddy but I cant get it too work with a home server. It will send the mail and shows up in the inbox but not the content. I think it has to do with using $body tag but I am not sure any suggestions?
<?php
// PHPmailer settings
$mail = new PHPMailer(); // create a new object
$mail->Issmtp(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "email"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = $email;
$mail->FromName = $contact_name;
$mail->SetFrom($email, $contact_name);
$mail->AddAddress('email');
$mail->Priority = 1;
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Gray Gables maintenance request";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
// gets info from form
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$ip = $_SERVER['REMOTE_ADDR'];
// defines how message looks in email
$mail->Body="
Name: $name<br>
Telephone: $phone<br>
Email: $email<br>
IP address: $ip <br>
-------------------<br>
Message:<br>
$message";
// looks good in your inbox
$mail->From = "$email";
$mail->FromName = "$name";
$mail->AddReplyTo("$email","$name");
// gives success or error
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo '
<meta http-equiv="refresh" content="3;url=http/">
<center><font size="5px">
Message has been sent!!<br>
you will be redirected to our home page in 3 seconds...<br>
click here to return now.
</font>
</center>
';
?>
<?php
echo ' Client IP: ';
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
echo '' . $_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' ';
}
?>
I think you are looking for this...
$body = file_get_contents('yourfile.html');
You can attach any HTML content in 'yourfile.html', which displays in your body of the mail.

phpmailer received email but not attachment

I am using the below script to allow user to upload images and email it to me. I did received the email but the attachment is not included in the email. It also show the from email as root user instead of the user's email address. please help.
<?php
ob_start();
require("class.phpmailer.php");
$salutation = $_POST['salutation'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$photo = $_FILES['photo'];
isset($_POST['submit']);
$active_keys = array();
foreach($_FILES[$photo]['name'] as $key => $filename)
{
if(!empty($filename))
{
$active_keys[] = $key;
} }
foreach($active_keys as $key)
{ switch(exif_imagetype($_FILES[$photo]['tmp_name'][$key])) {
case IMAGETYPE_JPEG:
case IMAGETYPE_PNG:
break;
default:
echo "{";
echo "error: 'This is no photo..'\n";
echo "}";
exit(0);
} }
$message = "Photo submitted by: $salutation $firstname $lastname. <br>Comments: $comments.<br>terms:$terms.";
$mail = new PHPMailer();
$mail->From = ($email);
$mail->AddAddress('info#domain.com');
$mail->Subject = "Submitted Photos";
$mail->Body = $message;
$mail->WordWrap = 50;
foreach($FILES['photo']['tmp_name'] as $photo) {
if(!empty($photo)) {
$mail->AddAttachment($photo);
}}
$mail->Send();
header("Location: thankyou.php");
exit();
?>
change
foreach($FILES['photo']['tmp_name'] as $photo) {
to
foreach($_FILES['photo']['tmp_name'] as $photo)

Not able to send attachment with my code?

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.

Resources