php mailer will send mail but not include content - phpmailer

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.

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.

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.

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.

$mail->addAddress() parameters

S.O.S i am trying to send a message to a selected email(posted by form)so the form will pass the student_id to the php script which gets the corresponding student_email address (referenced by the student_id) ,,,
i highly appreciate your help .
this is the error message i get every time,,
Invalid refreshed: You must provide at
least one recipient email address.
Message was not sent PHP Mailer Error:
You must provide at least one
recipient email address.
the code:
<?php require_once("connection.php"); ?>
<?php require_once("functions.php"); ?>
<?php require("class.phpmailer.php");?>
<?php
// START FORM PROCESSING
if( isset($_POST['submit'])) { // Form has been submitted.
$student = trim(mysql_prep($_POST['student']));
$re_mail =$student["email"];
$mail = new PHPMailer();
$mail->PluginDir = './';
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = "smtp.gmail.com";
$mail->IsHTML(true);
$mail->Mailer = "smtp";
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Username = "xxxxx#gmail.com";
$mail->Password = "xxxxxxxxxx";
$mail->SingleTo = true; // if you want to send mail to the users individually so that no recipients can see that who has got the same email.
$mail->From = "xxxxxx#gmail.com";
$mail->FromName = "xxxxxxxxx";
$mail->addAddress($re_mail );
$mail->Subject = "Testing PHP Mailer with localhost ";
$mail->Body = "Hi,This system is working perfectly.";
if(!$mail->Send())
echo "Message was not sent PHP Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";}
?>
<html>
<body>
<form id="form1" name="form1" method="post" action="">
<span id="spryselect1">
<label>Email:
<select name="student" id="student" tabindex="1">
<?php
$students = get_all_students();//this function works fine
while ($student = mysql_fetch_array($students)) {
echo "<option value=\"{$student["id"]}\"> {$student["first_name"]}</option> ";
}
?>
</select>
</label>
<p>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>
note:
i have this function in my function.php
function get_all_students() {
global $connection;
$query = "SELECT *
FROM student ORDER BY first_name ASC";
$student_set = mysql_query($query, $connection);
confirm_query($student_set);
return $student_set;
}
Finally i got the answer,,,thanks to everyone offered help here :)
<?php require_once("connection.php"); ?>
<?php require_once("functions.php"); ?>
<?php require("class.phpmailer.php");?>
<?php
// START FORM PROCESSING
if( isset($_POST['submit'])) { // Form has been submitted.
$student = trim(mysql_prep($_POST['student']));
$re_to_student=get_student_by_id($_POST['proposer']);
//this is another function i have in my function.php
$st_email = $re_to_student["email"];
$mail = new PHPMailer();
$mail->PluginDir = './';
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = "smtp.gmail.com";
$mail->IsHTML(true);
$mail->Mailer = "smtp";
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Username = "mymail#gmail.com";
$mail->Password = "mypassword";
$mail->SingleTo = true; // if you want to send mail to the users individually so that no recipients can see that who has got the same email.
$mail->From = "mymail#gmail.com";
$mail->FromName = "myweb.com";
$mail->addAddress($st_email);
$mail->Subject = "test";
$mail->Body = "Hi,This system is working perfectly.";
if(!$mail->Send())
echo "Message was not sent PHP Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";}
?>

Resources