This is my index.php file
Please suggest what is wrong with the below code that i am not receiving mails or if there is any other easy way of sending emails.
<?php
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth=false;
$mail->SMTPSecure='tls';
$mail->Host='smtp.gmail.com';
$mail->port='587';
$mail->isHTML();
$mail->Username='demoXXXXX#gmail.com';
$mail->Password='XXXXXX';
$mail->SetFrom('no-reply#gmail.com');
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->addAddress("parkingston#gmail.com");
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
Include class.phpmailer.php & classes/class.smtp.php first .
<?php
require "autoload.php";
include "class.phpmailer.php"; // include the class name
include "class.smtp.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth=false;
$mail->SMTPSecure='tls';
$mail->Host='smtp.gmail.com';
$mail->port='587';
$mail->isHTML();
$mail->Username='demoXXXXX#gmail.com';
$mail->Password='XXXXXX';
$mail->SetFrom('no-reply#gmail.com');
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->addAddress("parkingston#gmail.com");
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
You can easily download this both file if you do not have it. Hope it will help you.
Related
I am trying to send emails using phpmailer and getting the error as mentioned in the title. I am using XAMPP for apache server.
I have searched almost every link on google and everywhere but nothing is working for me.
<?php
ini_set('display_errors', 1);
require 'autoload.php';
require 'class.phpmailer.php';
//require 'class.smtp.php';
$mail = new PHPMailer;
$mail->IsSMTP();
//$mail->SetLanguage("en", 'language');
$mail->Mailer = "smtp";
//$mail->SMTPSecure = "tls";
$mail->Port = 465;
$mail->SMTPDebug = 2;
$mail->SMTPAuth=False;
$mail->Host="smtp.gmail.com";
$mail->Username = "email#gmail.com";
$mail->Password = "password";
//$mail->setFrom("email#gmail.com");
$mail->From = "email#gmail.com";
$mail->addAddress('email#gmail.com','Rishabh Goel');
$mail->isHTML(false);
$mail->Subject = 'First Subject';
$mail->Body = 'Mail body in HTML';
$mail->AltBody = 'This is the plain text version of the email content';
/*try{
$mail->Send();
echo "Thanks. Bug report successfully sent.
We will get in touch if we have any more questions!";
} catch(Exception $e){
//Something went bad
echo "Mailer Error: - " . $mail->ErrorInfo;
}*/
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
?>
Im using phpMailer to send an image like attachment and a message in HTML. But the page show me that is not working.
include 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer();
$mail->From = 'someone#some.com';
$mail->FromName = 'someone';
$mail->AddAddress( 'someone#some2.com' );
$mail->Subject = 'Message Subject';
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);
$file_to_attach = '../images/logo.png';
$mail->AddAttachment( $file_to_attach , 'logo.png"' );
if(!$mail->Send())
echo "Error sending: " . $mail->ErrorInfo;;
else
echo "Letter is sent";
Using this code works perfectly to me send a message with HTML and an attachment.
<?php
require_once 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer(true);
try {
$mail->AddAddress('someone#some.com', 'a name');
$mail->SetFrom('someone2#some2.com', 'another name');
$mail->AddReplyTo('someone3#some3.com', 'another name');
$mail->Subject = 'PHPMailer Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->IsHTML(true);
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AddAttachment('../images/logo.png');
$mail->Send(); echo "Message Sent OK\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
You are using $email variable to new object PHPMailer but in line 8,9 and 14,15 you're using $mail.
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);
if(!$mail->Send())
echo "Error sending: " . $mail->ErrorInfo;
just use $email.
I see you are using
$file_to_attach = '../images/logo.png';
to get the name for the image file. However the file name is already set in:
$email->AddAttachment( $file_to_attach , 'logo.png"' );
Try putting the whole path in $email->AddAttachment(); like this $email->AddAttachment('../images/Logo.pgn');
I have this code where sending body in UTF-8 is not working:
<?php
require '../phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->setFrom('from#example.com', 'First Last');
$mail->addAddress('whoto#example.com', 'John Doe');
$mail->Subject = 'PHPMailer mail() test';
$mail->AltBody = 'This is a plain-text message body';
$mail->AddEmbeddedImage('img_00/Mailing_febrero2015_02.jpg', 'fondo', 'fondo.jpg');
$mail->Body = 'árbol';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
But if I remove the "AltBody" or "AddEmbeddedImage" it sends correctly in UTF-8. Why this occurs (I have the last PHPMailer version)?
There is another correct way to add an alternative body and embedded image at the same time?
I'am newbie in php. I'am trying to send email using php but I don't know what's wrong in my code. I googled a lot but nothing has worked yet. Here is my php code. I'am using class.phpmailer.php.
<?php
require("phpmailer-master/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemail#googlemail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$webmaster_email = "recipient#googlemail.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="myname"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I could finally send a mail using php. Here is the code:
<?php
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'sender#mail.com';
$mail->Password = 'sender_password';
$mail->SMTPAuth = true;
$mail->From = 'sender#mail.com';
$mail->FromName = 'sender';
$mail->AddAddress("sender#mail.com");
$mail->AddReplyTo("sender#mail.com", 'Information');
$mail->IsHTML(true);
$mail->Subject = "Sample exmple to check proper working of mail function";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello ";
$path = $_POST['upload'];
$mail->AddAttachment($path); // attachment
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
?>
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
// Comment out this line here it is wrong
// IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email = "username#domain.com"; // Recipients email ID
$name = "name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Webmaster");
$mail->WordWrap = 50; // set word wrap
// i would also comment out these lines, get it working without attachments first
// then add then back in after (if you want attachments)
// $mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
// $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
// at the end of the pasted code above, you have these lines (below here) doubled up.
// remove them
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
EDIT:
The basic idea for sending email with PHPmailer was perfect. But I'm having issue attaching files (in different) format.
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.domain.com"; // SMTP server
$mail->From = "frommail#domain.com";
$mail->FromName="fromname";
$mail->AddAddress("admin#domain.com");
$mail->IsHTML(true);
//for file attachments
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']);
$mail->Subject = "First PHPMailer Message";
$mail->Body = "<b>Hi! <br><br> This is my first e-mail sent through PHPMailer.</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
I tried to display the sequence but not a help. Much appreciated for your time.