PHPMailer dynamic mail recipient insertion - php

We all know how to implement PHPMailer within a project and its use is great, but I need something more, after entering the necessary parameters in order to make everything work.
I need a form to change the email address of the recipient who will receive the email, how can I do?
code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); //Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx#gmail.com'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('xxx', 'xxx'); // Sender email and name
$mail->addAddress('xxx'); // Reciver email
// if you want to send email to multiple users, then add the email addresses you which you want to send.
//$mail->addAddress('reciver2#gmail.com');
//$mail->addAddress('reciver3#gmail.com');
//For Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // You can specify the file name
//Content
$mail->isHTML(true);// Set email format to HTML
$mail->Subject = 'Oggetto dell email'; // Subject of the email
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

Related

PHPMailer script sending email twice when CC added

I'm trying to send an email that has a attachment generated using dompdf to a list of recipients. Using array_shift() for taking the primary email and remove it from the array and use foreach for taking every other email as CC. When I'm adding CC, email is sent twice. If there is no CC, it will be sent only once. Even if CC is blank , it is sent twice. If I remove that line AddCC(), it will be sent only once.
Any idea or help I can seek?
require_once 'dompdf/autoload.inc.php';
require_once 'phpqrcode/qrlib.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$recipients = array() // array of emails to be sent
try {
ob_start();
include "pdf.php";
$htmldata = ob_get_clean();
$options = new Dompdf\Options();
$options->getChroot($_SERVER['DOCUMENT_ROOT']);
$options->setisRemoteEnabled(true);
$options->setIsHtml5ParserEnabled(true);
$options->setTempDir('temp'); // temp folder with write permission
$dompdf = new Dompdf\Dompdf($options);
$dompdf->loadHtml($htmldata);
$customPaper = array(0,0,600,600);
$dompdf->set_paper($customPaper);
QRcode::png($data, "qr.png", "L", 4, 4);
$dompdf->render();
$output = $dompdf->output();
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('', '');
$mail->addAddress(array_shift($recipients)); // Add first email as the recipient and remove the email from array
$mail->addReplyTo('');
foreach($recipients as $ccEmail)
{
$mail->AddCC($ccEmail);
}
$mail->addStringAttachment($output, 'attachment.pdf');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject'.rand();
$mail->Body = 'Content';
$mail->AltBody = 'content';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

PHPMailer connection issues

I want to send a email using php(using windows operating system).However I keep encountering connection issue and that the attachments file are not found.Are there any solutions?
//-------------sending mail----------------------------
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
//Create an instance; passing true enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'username#gmail.com'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('hotelpage#gmail.com', 'Hotels Page');
$mail->addAddress('MohanDepesh#gmail.com', 'MohanDepesh'); //Add a recipient
$mail->addReplyTo('no-reply#gmail.com', 'no-reply');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Please check following points of you are receiving errors-
1.Check if you are using correct mail and password.
2. There maybe an issue due to "Allow less secure apps" settings.Try to turn on this setting for testing.
Please attach exact error if you are still facing issue.
Edit: As Allow less secure apps is disable now , you can use appPassword for it

PHPMailer doesn't send message or output errors

I just downloaded phpmailer from github and uploaded to shared hosting.
I made file called mailer.php and added this code (example from git-page):
When I start this (open file) Nothing is echoed and no mails are received. If i try to echo anything like any text after this code it won't do it but if I put it before it will echo it out.
I looked up Here but nothing from answers works here...
<?php
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mydomain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; //shared hosting ssl port
//Recipients
$mail->setFrom('email#mydomain.com', 'Site Title');
$mail->addAddress('myGmail#gmail.com', 'Soma name'); // Name is optional
$mail->addReplyTo('email#mydomain.com', 'Info');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

how to set sender mail id for phpmailer

my name is Milind. I trying to use PHPmailer for send mail. I mention set form in phpmailer like this:
$mail->setFrom('from#example.com', 'Mailer');
but when I received mail I get username id where I set in phpmailer
$mail->Username = 'xxx#gmail.com';
What can i do to display setfrom id to recipient
my phpmailer code follow:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmailid#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('xxx#gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Gmail doesn't allow you to send form arbitrary from addresses, though you can create preset "aliases" in your account that it will allow you to use as alternative from addresses. If you try to use an arbitrary address, it will ignore it and send as your account address instead, as you're finding.

How to configure and run PHPmailer in PHP

this query may sounds silly but it has become a headache for me. i am using ampps... I need to implement mail notification in my project. i have downloaded the PHPmailer rar file and extracted to my project folder.
It contains,
*get_oauth_token.php
*src-Exception.php-OAuth.php-PHPMailer.php-POP3.php-SMTP.php
I got only this files in that folder.
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Here i am getting an error message saying,
Warning: require(vendor/autoload.php): failed to open stream: No such
file or directory in C:\Program Files
(x86)\Ampps\www\emailpro\email.php on line 8
By googling I got to know that by composer we can download classes. I tried that also. >composer require phpmailer/phpmailer – ERROR- 'composer' is not recognized as an internal or external command, operable program or batch file.
I am totally confused pls anyone tell me how to implement this PHPmailer to the project.
Please help how to configure from scratch i have googled lot and i didnt got the required answer.
If you don't want to install composer then use this code for php mailer to work. It is working in my projects. You can also remove the part of code which you don't need like attachments. One more thing if you want to hide the debugging code errors and notifications remove or comment this line $mail->SMTPDebug = 2;
For more help visit this link https://github.com/PHPMailer/PHPMailer
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}`enter code here`

Categories