How to set the from name in php mailer? - php

I can actually send the emails but I want to change the email from name. When I send emails, it comes into the receiver mailbox with my mail id. I want to change that to some other names. Kindly help me to do that. This is my code
<?php
$mailto = $_POST['mail_to'];
$mailSub = $_POST['mail_sub'];
$mailMsg = $_POST['mail_msg'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 0;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "smtp.gmail.com";
$mail ->Port = 465; // or 587
$mail ->IsHTML(true);
$mail ->Username = "dkdev006#gmail.com";
$mail ->Password = "password";
$mail ->SetFrom("Kreatz.in");
$mail ->Subject = $mailSub;
$mail ->Body = $mailMsg;
$mail ->AddAddress($mailto);
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo "Mail Sent";
}

Look at the docs on this method.
You have $mail ->SetFrom("Kreatz.in");, which is neither a "nice" name nor an email address, so I don't know what you're trying to do there. You probably want to do this:
$mail ->setFrom('dkdev006#gmail.com', 'Kreatz.in');
Bear in mind that you're using gmail to send through, so you are not allowed to set arbitrary from addresses, though you can create fixed aliases in gmail prefs.
You're also using an old version of PHPMailer, and have based your code on an obsolete example, so get the latest version.

Related

I want to send a qrCode to an email using PHPMailer

This is my qr_code.php file:
require_once 'phpqrcode/qrlib.php';
function create_QrCode($name){
$path ='images/';
$file=$path.uniqid().".png";
$text=$name;
QRcode::png($text, $file);
}
?>
This is my mail.php file:
require('qr_code.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\xampp\htdocs\contact_form\vendor\autoload.php';
#Initialize PHPMailer
function Send_email($mailFrom,$User_name,$phone,$city,$facebook,$qrCode){
$mail= new PHPMailer();
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail ->isSMTP();
$mail ->SMTPAuth =true;
$mail ->SMTPDebug =3;
$mail ->SMTPSecure="tls";
$mail ->Port=587;
#Connection settings
$mail ->Host= "smtp.gmail.com";
$mail ->Username="email#gmail.com";
$mail ->Password="password";
$mailTo ='email#gmail.com';
$mail-> IsHTML(true);
$mail ->SetFrom($mailFrom,$User_name);
$mail ->addAddress($mailTo);
$mail->AddReplyTo($mailFrom, $User_name);
#Create email
$mail->Subject ='I would like to come at the party ! ';
$mail->Body ="Name: $User_name<br>
Phone No: $phone<br>
City: $city<br>
Facebook: $facebook<br>
Email id: $mailFrom<br>
QrCode: $qrCode";
return $mail->Send();
}
This is my functionalities.php file (where I want to send the email):
$qrCode=create_QrCode($name);
if(!Send_email($email,$name,$phone,$city,$facebook,$qrCode))
{
echo "Mail NOT SENT";
}
else
{
echo"Mail Sent !";
}
header('Location:redirecting_page.html');
}
I have tried different modes, but I don't know how to figure it out. I know in the QRCode function, I am generating it to a file, but this is what I succesfully created until now. I know the QRCode function is the problem, but I don't know how to fix it. Can somebody please help me?

PhpMailer can't seem to send mail to Outlook

I've been trying to make a small E-mail function, so it's possible to send an E-mail to my Outlook from my website. But I'm running into some problems..
Do I have to change anything in my php.ini (xampp)? and username and password should be my outlook usn/pass right?
When I run this .php it says: echo "Mail Not Sent";
Can't really see what I'm doing wrong.
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 0;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "smtp.office365.com";
$mail ->Port = 587; // or 587
$mail ->IsHTML(true);
$mail ->Username = "mhoegstrup#company.com";
$mail ->Password = "XXXX";
$mail ->SetFrom("mhoegstrup#company.com");
$mail ->Subject = "Test";
$mail ->Body = "HEJJ";
$mail ->AddAddress("mhoegstrup#company.com");
if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo "Mail Sent";
}
The file PHPMailer is from: https://drive.google.com/file/d/0BzlVPBUP5IM8dmpDZ2tEZjdRaEU/view
Thanks for the help!
I checked documentation and settings of outlook service:
I realized you just need to set SMTPSecure to PHPMailer::ENCRYPTION_STARTTLS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

PHP Mailer returns SMTP ERROR: Failed to connect to server

I tried to change port number and 'ssl' to 'tls'. I also activated extension=php_openssl.dll in php.ini, restarted the server and allowed the low security applications on Gmail (https://www.google.com/settings/security/lesssecureapps)
These solutions seem to work for everyone but not me! can you help? here is my code:
<?php
session_start();
include 'dbConnector.php';
$toAddress = $_POST['toEmail'];
$emailSubject = $_POST['subject'];
$emailContent = $_POST['content'];
$fromPassword = $_POST['frompassword'];
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 1;
$mail -> SMTPAuth = true;
$mail -> SMTPSecure = 'ssl';
$mail -> HOST = "smtp.gmail.com";
$mail -> Port = 465; //587
$mail -> IsHTML(true);
$mail -> Username = $_SESSION['loggedUserEmail'];
$mail -> Password = $fromPassword;
$mail -> SetFrom($_SESSION['loggedUserEmail']);
$mail -> Subject = $emailSubject;
$mail -> Body = $emailContent;
$mail -> AddAddress($toAddress);
if(!$mail -> Send ())
{
echo "Your mail has not been sent!";
}
else
{
echo "Your email has been sent successfully!";
}
?>

Godaddy phpmailer smpt configuration

We have a site on godaddy server. We have a contact form. When user filled form, first we should send mail to our info#oursite, after we should send mail to user's mail from our info#oursite. My tried code below:
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->port = 25;
$mail->Host = 'localhost';
$mail->Username = 'username#example.com';
$mail->Password = 'password';
$mail->addAddress($email, $name);
$mail->subject = 'Subject';
$mail->MsgHTML('mail içeriği');
if ($mail->Send()){
this outputs below:
it seems mail delivered, but it never delivered the message.
I tried many things;
$mail->SMTPAuth = true;
port 25,80,3535,465(with ssl),
tls, ssl authentication,
what should I do? what should I try more, any ideas?
First of all, you're using a very old version of PHPMailer; get the latest.
I don't know where you got your code, but you've got the case of some vital properties wrong - I suggest you base your code on the examples provided with PHPMailer. In particular, subject should be Subject and port should be Port. When sending to localhost you don't need a username or password, so you don't need to set them. Similarly, Port defaults to 25, so you don't need to set it.
You're not specifying a From address, and it looks like whatever address you're passing to addAddress is invalid, so you're sending a message from nobody to nobody - and not surprisingly it's not going anywhere!
It looks like your message body is in Turkish (?), which will not work in the default ISO-8859-1 character set, so I suggest you switch to UTF-8 by setting $mail->CharSet = 'UTF-8';.
Check your return values when calling addAddress to make sure the submitted value is valid before trying to send to it.
With these things fixed:
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer;
if (!$mail->addAddress($email, $name)) {
die('Invalid email address');
}
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 2;
$mail->Host = 'localhost';
$mail->Subject = 'Subject';
$mail->msgHTML('mail içeriği');
$mail->setFrom('me#example.com', 'My Name');
if ($mail->send()) {
echo 'Sent';
} else {
echo 'Error: '.$mail->Errorinfo;
}

PHP Mailer - User email getting sent to admin

I am trying to send an email to user and admin using php-mailer.But when I send the mail, the user Acknowledgement mail is also getting sent to the admin. The e mails getting sent are in proper format. What is the issue here?
My Code is Here:
$name = "User Name";
$email = "user#gmail.com";
$mobile = 'User Phone No.';
$body = "<div><div>Name: <b>$name</b></div> <div>Email: <b>$email</b></div> <div>Mobile: <b>$mobile</b></div></div>";
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail -> isSMTP();
function sendEmail($mail, $from, $password, $to, $body, $subject) {
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail -> SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail -> Debugoutput = 'html';
//Set the hostname of the mail server
$mail -> Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail -> Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail -> SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail -> SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail -> Username = $from;
//Password to use for SMTP authentication
$mail -> Password = $password;
//Set who the message is to be sent from
$mail -> setFrom($from, $from);
//Set who the message is to be sent to
$mail -> addAddress($to, $to);
//Set the subject line
$mail -> Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail -> msgHTML($body);
//Replace the plain text body with one created manually
$mail -> AltBody = 'This is a plain-text message body';
$sucess = $mail -> send();
// //send the message, check for errors
// if (!$sucess) {
// echo "Mailer Error: " . $mail -> ErrorInfo;
// } else {
// echo "Message sent!";
// }
}
$from = "noreplyadmin#gmail.com";
$maiAdmin = "admin#gmail.com";
$password = "password";
$subject = "Enquiry";
// Send Mail to admin
sendEmail($mail, $from, $password, $maiAdmin, $body, $subject);
// Send Mail to User
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($mail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
You are using the same instance and have not cleared the previous out of the PHPMailer $mail instance. You can either clear the old data out of the PHPMailer Instance or do this:
$adminmail = new PHPMailer();
$adminmail -> isSMTP();
$usermail = new PHPMailer();
$usermail -> isSMTP();
sendEmail($adminmail, $from, $password, $maiAdmin, $body, $subject);
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($usermail, $from, $password, $email, $userMessage, 'Acknowlwdgement');
Whatever floats your boat I suppose.

Categories