I am using PHPMailer to send emails via SMTP with TLS encryption. Emails were sending fine and received as expected. But for some reason, the To address of each email is not shown. Am I doing anything wrong here?
$mailer = new PHPMailer;
// header
$mailer->IsHTML(true);
$mailer->CharSet = "text/html; charset=UTF-8;";
$mailer->WordWrap = 80;
// protocol
$mailer->isSMTP(); // Set mailer to use SMTP
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = true; // Enable SMTP authentication
// credential
$mailer->SMTPSecure = 'tls'; // Enable encryption
$mailer->Host = $agent['host'];
$mailer->Port = $agent['port'];
$mailer->Username = $agent['username']; // SMTP username
$mailer->Password = $agent['password']; // SMTP password
// setup different addresses
$mailer->From = $agent['from'];
$mailer->FromName = $agent['from_name'];
$mailer->addAddress($to, $name);
$mailer->addBCC($agent['bcc']);
$mailer->SingleTo = true;
// content
$mailer->Subject = $subject;
$mailer->Body = $html;
$mailer->AltBody = $text;
// finally, send out the mail message
if (!$mailer->Send()) {
throw new Exception('[Mailer] error: ' . $mailer->ErrorInfo);
}
// clear all addresses and attachments for next loop
$mailer->clearAddresses();
Related
I don't know how to send a mail using PHPmailer in a function.
I want to do something similar to this - when I'll want to send a mail, I'll just call a function (like send::mail()). here is my code:
<?php
require '.\vendor\autoload.php';
//PHPMailer Object
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsSendmail();
//From email address and name
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->setFrom('my.bot.mail#gmail.com', 'Bot name');
$mail->Password = "########"; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
//To address and name
$mail->addAddress("mymail#gmail.com"); //Recipient name is optional
//Address to which recipient will reply
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
$mail->Mailer = "sendmail";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
can someone help me put it into a function? when I tried, the "use" command didn't work.
First, you must determine the data.
$mailHost = '';
$mailSMTPAuth = '';
$mailUsername = '';
$mailPassword = '';
$mailSMTPSecure = '';
$mailPort = '';
$mailSenderAddress = '';
$mailSenderName = '';
Then you can generate a function that contains only the recipient, subject and message, and you can send the mail easily by pull the settings into this function:
function sendMail($sendMailAdress, $sendMailName, $sendMailSubject, $sendMailMessage) {
global $mailHost;
global $mailSMTPAuth;
global $mailUsername;
global $mailPassword;
global $mailSMTPSecure;
global $mailPort;
global $mailSenderAddress;
global $mailSenderName;
require_once('class/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mailHost; // Specify main and backup SMTP servers
$mail->SMTPAuth = $mailSMTPAuth; // Enable SMTP authentication
$mail->Username = $mailUsername; // SMTP username or mail
$mail->Password = $mailPassword; // SMTP password
$mail->SMTPSecure = $mailSMTPSecure; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mailPort; // TCP port to connect to
$mail->CharSet = 'utf-8';
$mail->setFrom($mailSenderAddress, $mailSenderName);
$mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sendMailSubject;
$mail->Body = $sendMailMessage;
if(!$mail->send()) {
return false;
} else {
return true;
}
}
The function will work like this:
sendMail('hello#test.com', 'Test User', 'Subject of test mail', 'And your mail message);
I am trying to send a fax via the eFax service. As I understand, this should be as easy as sending a simple email to the specific address.
I use PHPMailer for emails. Ordinary emails are sending fine. Even when I send to an efax address $mail->send returns true, that means that the email was sent seccessfully, but the recipient didn't receive the fax.
Any suggestions?
Thank you.
function sendFax($fax, $msg) {
$efax_number = $fax . "#efaxsend.com";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = EMAIL_HOST; // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
//From email address and name
$mail->From = "orders#orderapp.com";
$mail->FromName = "Test";
//To address and name
$mail->addAddress($efax_number); // SEND EMAIL TO USER
//Send HTML or Plain Text email
$mail->isHTML(false);
$mail->Subject = ' New Order Fax ';
$mail->Body = $msg;
$mail->AltBody = "Sample";
$result = $mail->send();
if (!$result)
{
...
}
else
{
...
}
}
I have a database. The fields are id, email and city. I want to send some email to all emails in my database.
In e.g case: I live in a city, so I want to send an email to all emails matchting my city.
I have wrote my method code like this:
$kota = mysql_real_escape_string(stripcslashes( $_POST['kota']));
$baca_kota = mysql_query("SELECT email FROM conselor WHERE kota='$kota'");
while($read = mysql_fetch_array($baca_kota))
{
$emailcc = $read['email'];
}
require '../phpmailer/PHPMailerAutoload.php';
require '../phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxxxxxxxxxxxxxxxxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email,$name);
$mail->addAddress('stevanlai04#gmail.com'); // Add a recipient
$mail->addReplyTo($email);
$mail->addCC($emailcc);
Do $mail->addCC($emailcc); inside the loop:
while ($read = mysql_fetch_array($baca_kota)) {
$emailcc = $read['email'];
$mail->addCC($emailcc);
}
all you need to call Send() method from your PHPmailer sdk in your
while
$this->mail->IsSMTP();
$this->mail->SMTPAuth = true;
$this->mail->Host = 'mail.example.com';
//$this->mail->SMTPSecure = 'ssl';
$this->mail->Port =25;
$this->mail->Username = 'mail#example.com';
$this->mail->Password = 'your password';
foreach($listemails as $key=>val){
$this->mail->SetFrom($val["email address"], 'topic');
$this->mail->Send();
}
Edit: Also you can create a function that function send email and it returns itself! for example
function sendemail($emailaddres){
// call your phpmailler medhot
/ and return it
$this->mail->Send()
return sendemail($val["email address"])
require_once( './PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com';
$mail->ClearReplyTos();
$mail->addReplyTo($organizer_email, $organizer);// Specify main and backup server
$mail->SetFrom('xyx#xyx.com', 'zyx');
$mail->SMTPAuth = tls; // Enable SMTP authentication
$mail->Username = 'xyz#xyz.com'; // SMTP username
$mail->Password = 'xyzzzzz'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587; //Set the SMTP port number - 587 for authenticated TLS
$mail->addAddress($organizer_email); // Add a recipient
$content = file_get_contents($resume_attach_path);
$content = chunk_split(base64_encode($content));
$eol = "\r\n";
$tags = explode(',', $to);
foreach ($tags as $tag) {
$mail->addCC($tag);
}
$mail->WordWrap = 50;
$mail->Subject = $subject;
$mail->Body = $desc;
$mail->AltBody = $text; // in your case once more the $text string
$mail->Ical = $text; // ical format, in your case $text string
$mail->isHTML(true); // Set email format to HTML
//$mail->addStringAttachment($resume_attach_path,'base64');
if ($mail->send()) {
return true;
}else{
return false;
}
if i use addAttachment i can just see the attachment, cannot see the ical invite ,but if i remove the attachment i can see the ical invite,how can i send both inline ical and external document attachment in phpmailer.
I am showing you a simple working example here
<?php
function sendMail($subject, $body, $name, $email, $attachment = null){
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "**************"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "*************"; // GMAIL username
$mail->Password = "***********"; // GMAIL password
//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom('*********', '************');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
if($attachment){
$mail->AddAttachment($attachment['tmp_name'],
$attachment['name']);
}
try{
$mail->Send();
} catch(Exception $e){
//Something went bad
// print_r($e);
echo "Fail :(";
die;
}
}
?>
In this code $attachment is $_FILES['file']. How to call this function is given below.
sendMail($subject, $body, $name, $email, $_FILES['file']);
To use Ical in mailer see a Stackoverflow Question
According to comment in PHPMailer code:
Only supported in simple alt or alt_inline message types
when you add other attachments, part of adding Ical file based on Ical property is ommited.
I am trying to send emails to phones that have Verizon numbers. Here is my code right now
<?php require('includes/config.php');
function Send( $ToEmail, $MessageHTML, $MessageTEXT) {
require_once ( 'classes/phpmailer/phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "box405.bluehost.com"; // Sets SMTP server
$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "ssl"; //Secure conection
$Mail->Port = 465; // set the SMTP port
$Mail->Username = 'techsupport#test.com'; // SMTP fake account username
$Mail->Password = 'password1'; // SMTP fake account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->FromName = 'Tech support';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->AddAddress( $ToEmail ); // To:
$Mail->isHTML( TRUE );
$Mail->Body = $MessageHTML;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();
if ( $Mail->IsError() ) { // ADDED - This error checking was missing
return FALSE;
}
else {
return TRUE;
}
}
$stmt = $db->prepare("select phone From members where phone not like 'no';");
$stmt->execute();
$result = $stmt->fetchAll();
$ToEmail = $result[0][0];
$ToName = 'techsupport';
$MessageHTML = "test";
$MessageTEXT = 'test';
$Send = Send( $ToEmail, $MessageHTML, $MessageTEXT);
if ( $Send ) {
echo "<h2> Sent OK</h2>";
}
else {
echo "<h2> ERROR</h2>";
}
die;
?>
this works when I try to send emails but when I use it to send texts it says sent but I do not receive anything. I know this is not because of the address because I use the same address when I text myself from gmail and it is not the sql query because I have tested it . I Think the problem is in the smtp or phpmailer for I have not used either of those a lot. Also the code runs though and prints out the SENT OK echo but nothing goes through and no errors.
[EDIT] To answer ironcito's question I am sending the email to
phonenumber#vtext.com
I know this works because I have used the same address through gmail.
I think this might be because you push the content to the mail (body) before you set the content (messagehtml/messagetext), which creates an empty email. Try changing these around, that might be the solution.