How to use phpmailer from sh service framework - php

I am really stuck in email concept. I used sh service framework for past 3 days i am searching all the way to find how email concept works in sh service especially php mailer.
Thanks and help needed

Use this command :
composer require phpmailer/phpmailer
Follow below procedure:
require 'vendor/autoload.php';
$app->get('/contactus',function($app){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com;smtp2.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User');
$mail->addAddress('ellen#example.com');
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
} // Set mailer to
});
it is working fine.
Thanks

Related

Mail body is going null while sending mail through smtp

I am trying to send a mail-in php using SMTP. If I send the mail in the simple text then it's working perfectly but when I add HTML code like b,a tag in it then the mail is going but my body of the mail is empty.
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'pushkarnabandhu.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = SMTP_EMAIL; // SMTP username
$mail->Password = SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom(SMTP_EMAIL, 'Virtual Music');
$mail->addAddress($_POST['email'],$_POST['name']); // 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');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email confirmation';
$mail->Body = "<b><a href='".ROOT."confirm/$$insert[verification_string]'></a></b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

Is there a way to send email using PHPMailer without Composer and in XAMPP

I'm hosting my PHP documents on XAMPP (Apache, not sure why) and I don't want to use composer but still want to use PHPMailer. No matter where I place classes and index.php it always gives me an error
require(): Failed opening required 'PHPMailerAutoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\PHPMailer-master\index.php on line 2
I've put my index.php in C:\xampp\htdocs\PHPMailer-master (directly downloaded from GitHub) and it still didn't work.
I've used code from this article: How to send an email using PHP?
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
require_once(APPPATH.'third_party/phpmailer/src/PHPMailer.php');
require_once(APPPATH.'third_party/phpmailer/src/SMTP.php');
$mail = new \PHPMailer(true);
//Server settings
$mail->isSMTP();
//$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->CharSet = "utf-8"; // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'ssl';
$mail->Port = 25;//587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Recipients
$mail->setFrom('noreply#mail.com', 'From name');
$mail->addAddress($mailData['to_email']); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Subject";
$mail->Body ="Html contetn";
$mail->send();
return true;
Try to download phpmailer library from trusted source or you can copy the src folder from the phpmailer library downloaded via composer

Setting up PHPmailer, not receiving any emails?

I've been trying to set up my php emailer today but for some reason it simply isn't sending my emails. I have my website hosted with hostgator and was told to set up the smtp via gator3115.hostgator.com with port 465 when using hostgator which is what i've done but when I check the recipient email there still isn't anything on there. Anyone know what's going on here?
<?php
ob_clean();session_start();
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
if (isset($_POST['submit'])){
require 'PHP/mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'gator3115.hostgator.com'; // 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 = 465; // TCP port to connect to
$mail->setFrom('matt#sulmaxmarketing.com', 'Mailer');
$mail->addAddress('sulmaxcp#gmail.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');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>

How to setup php on webhost without php.ini file

I'm close to finishing my first real website, this website uses php mail function to send mail from my website. My script works because I'm running mercury which routes the mail through gmail smtp. Now my question is if someone can tell me how to connect to this smtp server from php because my webhost doesn't allow me acces to the php.ini file.
Thanks in advance
This PHP class will do it for you. Most real webapps don't rely on PHP's mail(), they use this class instead (or swiftmailer, but his one's much easier).
PHPMailer
Sample code (from the linked page)
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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
$mail->From = 'from#example.com';
$mail->FromName = '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');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

How to rectify SMTP connection failure error?

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.comcast.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mass.mari06#gmail.com'; // SMTP username
$mail->Password = '8489328117'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->Port = $SmtpPort;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('mass.mari06#gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('deeparavisankar93#gmail.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
****ERROR:**** Message could not be sent.Mailer Error: SMTP connect() failed.
So what can I do? Please help me.. this code I downloaded from GitHub.
SMTP is mail sending server.
You should use the one you are trying to send mail "from" is assigned. I assumed that you try to send from mass.mari06#gmail.com so configuration should look like following:
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mass.mari06#gmail.com';

Categories