We are Web Technology Experts Team who provide you Important information on Web Development, Interview Questions and Answers, live project problem and their solution and online free tutorials.
Step 1:
Use Following code to send an email from Gmail Account.
// Import PHPMailer classes into the global namespace
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
//Email Sender
$mail->setFrom('from@example.com', 'Mailer');
//Recipients
$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 (IF there is attachment then only include)
$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 (true)
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body';
$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;
}
If you get similar to following Error Please log in via your web browser and then try again. 5.7.14 Please log in via your web browser and then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fr13sm3613053pdb.81
If Yes, whether you have "turned on 2-Step Verification" OR Google need Verification that you want to use your gmail as SMTP credential. You can fix this in couple of minutes using below link: https://support.google.com/mail/answer/78754
If any issue, please comment below in comment box:
During the website development, many times we need to check the Registration Process, Forget Password and Newsletter Functionalists etc, to Perform all these things we need number of number address. Its very hard to do registration in gmail.com OR yahoo.com OR hotmail.com everytime. You can get email address and at Free of cost within a second. Following are list of websites who provide email account and at free of cost.
SMTP Credentials like SMTP USRNAME, SMTP PASSWORD, SMTP HOST, SMTP PORT
Script Code
$toEmail='toemailaddress@domain.com'; //Receiver email address
$subject='Email with attachment';//Email subject
$htmlEmail='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eget nibh consectetur, mattis mauris vitae, aliquam orci. Nunc pretium tristique metus eget pharetra. Ut condimentum purus in ex varius, quis bibendum ex venenatis. Praesent leo lacus, feugiat vel neque ac, finibus ultricies velit. Nunc venenatis sem eu ullamcorper interdum. Fusce a pulvinar risus, quis viverra nulla. Donec tempus ex congue mauris elementum, eget fermentum orci dapibus. Proin erat lacus, lacinia eu volutpat quis, sagittis suscipit erat. Maecenas nulla enim, tempor ac urna id, tincidunt consectetur metus. Maecenas tincidunt, tortor quis sodales rutrum, purus mi accumsan libero, id maximus purus leo id ante. In posuere erat ipsum, sed mattis est placerat eget. Phasellus consectetur diam augue, sed luctus libero consequat quis. Praesent vel mauris nec ligula malesuada faucibus. Suspendisse ultrices nunc velit, in ultricies nibh tempus id. Integer quis nisi ipsum.'
. 'This is html email.';
//Email Configuration
$config = array(
'auth' => 'login',
'ssl' => 'tls',
'port' => '587',
'username' => 'MAIL_USERNAME',
'password' => 'MAIL_PASSWORD'
);
$tr = new Zend_Mail_Transport_Smtp('MAIL_HOST', $config);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail('UTF-8');
$mail->setFrom(MAIL_SENDER_EMAIL, MAIL_SENDER_NAME);
$mail->addTo($toEmail);
$mail->setSubject($subject);
$mail->setBodyHtml($htmlEmail);
//Attachment Email
//Get the contents of csv file
$csvContent= file_get_contents('http://example.com/projects/export-csv.csv');
$at = new Zend_Mime_Part($csvContent);
$at->type = 'text/csv';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->filename = 'csv-file.csv';
//Attach the attachment in Email
$mail->addAttachment($at);
//Send Email
$mail->send();
Question: How to send image as attachment in Email?
Following are attachment code.
$username = 'mygmail@gmail.com'; //Gmail address
$password = 'gma12#@c'; //Gmail password
error_reporting(0);
$bodyId=35; //This is message Id
//Read Message from indebox
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
//Read Message from Trash
// $hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Trash';
//Read Message from Sent
//$hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail';
$con = imap_open($hostname, $username, $password);
$messageNumber = $bodyId;
$structure = imap_fetchstructure($con, $messageNumber);
//pr($structure);die;
$message='';
if(!empty($structure->parts)){
$flattenedParts = flattenParts($structure->parts);
foreach($flattenedParts as $partNumber => $part) {
switch($part->type) {
case 0:
// the HTML or plain text part of the email
$message = getPart($con, $messageNumber, $partNumber, $part->encoding);
// now do something with the message, e.g. render it
break;
case 1:
// multi-part headers, can ignore
break;
case 2:
// attached message headers, can ignore
break;
case 3: // application
case 4: // audio
case 5: // image
case 6: // video
case 7: // other
//pr($part);
//echo "
$$$$$";
$filename = getFilenameFromPart($part);
if($filename) {
// it's an attachment
$attachment = getPart($con, $messageNumber, $partNumber, $part->encoding);
if (empty($filename))
$filename = $filename;
if (empty($filename))
$filename = time() . ".dat";
$folder = "attachment";
if (!is_dir($folder)) {
mkdir($folder);
}
$fp = fopen("./" . $folder . "/" . $messageNumber . "-" . $filename, "w+");
fwrite($fp, $filename);
fclose($fp);
// now do something with the attachment, e.g. save it somewhere
$message.='