i am using phpmailler for sending mail,mail works successfully but without attachments. i want to send mail with attachments.
i have tried this code.
thanks in advance.
$s2="select * from tbl_new_user where login_name='".$rw['clientname']."'";
$q2=mysql_query($s2) or die($s2);
$row=mysql_fetch_array($q2);
$s22="select * from tbl_job_schedule where clientname='".$rw['clientname']."' and jobdate='".$_SESSION['strmonth']."-".$_REQUEST['dt']."-".$_SESSION['yy']."'";
$q22=mysql_query($s22) or die($s22);
$row2=mysql_fetch_array($q22);
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpout.secureserver.net'; // Specify main and backup server
$mail->Port = '80';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = ''; // Enable encryption, 'ssl' also accepted
$mail->SMTPDebug = 1;
$mail->From = 'abc#abc.com';
$mail->FromName = 'abc#abc.com';
$mail->AddAddress($row['client_email'], ''); // Add a recipient
$mail->AddAddress($row['client_email2']); // Name is optional
$mail->AddAddress($row['client_email3']);
$mail->AddAddress($row['client_email4']);
$mail->AddAddress($row['client_email5']);
$mail->AddAddress($row['client_email6']);
$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
if($row2['file1']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file1'].''); // Add attachments
}
if($row2['file2']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file2'].''); // Add attachments
}
if($row2['file3']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file3'].''); // Add attachments
}
if($row2['file4']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file4'].''); // Add attachments
}
if($row2['file5']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file5'].''); // Add attachments
}
if($row2['file6']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file6'].''); // Add attachments
}
if($row2['file7']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file7'].''); // Add attachments
}
if($row2['file8']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file8'].''); // Add attachments
}
if($row2['file9']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file9'].''); // Add attachments
}
if($row2['file10']!='')
{
$mail->AddAttachment('kurtacompany/techreporting/upload/'.$row2['file10'].''); // Add attachments
}
//$mail->AddAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Reporting';
$mail->Body = '<p>This is an automated email report for the work done today.
Below are the comments showing on what we have worked,if you have any questions please go to the reporting URL provided and update your comment or can send a separate email to me directly on my email ID provided.</p>
<b>Work Comments : "'.$row2['client_cmnt'].'"</b>';
$mail->AltBody = '';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
echo "<script>window.close()</script>";
}
Make sure the path your are using for your attachments is valid.
i.e. does a file exist at kurtacompany/techreporting/upload/'.$row2['file3'] ?
It might be as simple as you missing a / from the beginning to indicate it should start searching from the root directory. If in doubt, try an absolute link to confirm:
http://www.mywebsite.com/kurtacompany/techreporting/upload/'.$row2['file3']
Related
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';
}
I want to send to a mail with a click on the HTML. I used ajax to call a PHP file. I included the PHP mailer code to send an email. And I also include a print json_encode("Success") at the bottom. I couldn't send the success message from php back to JS. I have used Print and many other output commands in PHP its not working.
$.ajax({
url:"api/collect_money.php",
type:"POST",
data:ajax_data,
async:false,
success:function(response)
{
console.log(response,"hello");
}
});
This is my PHP:
function sendmail($bills_count_data)
{
//print_r($bills_count_data[0]['end_date']);
//Load Composer's autoloader
require 'php/vendor/phpmailer/phpmailer/src/Exception.php';
require 'php/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'php/vendor/phpmailer/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 = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '****#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');
// Add a recipient
$mail->addAddress('****#gmail.com');
//$mail->addAddress('****#thewashhouseinc.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 = 'Collect Money Notification';
$mail->Body = "Collection has been made on ".$bills_count_data[0]['end_date']." The list of bills: one's ".$bills_count_data[0]['one']." two's ".$bills_count_data[0]['two']."";
$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;
}
}
sendmail($bills_count_data);
}
print ("success");
I don't see any response on the console. when I execute this code
This is likely to be your problem:
$mail->SMTPDebug = 2;
That outputs all kinds of stuff that's not remotely like JSON, and will cause your parsing of the response to fail. If you look at the raw response in your browser's dev tools, you should see it all. Disable debug output with:
$mail->SMTPDebug = false;
I'm trying to run PHPMailer only if my form is submitted. I've tried it a few different ways but no success.
I'm also not sure what part of this code actually sends the email. When I test it using
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
it works, so I know it runs correctly. Any ideas why this code doesn't work?
<?php
require_once 'C:\wamp\www\phpmailer\PHPMailer-master\PHPMailerAutoload.php';
$mail = new PHPMailer;
//$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 = 'test#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('tester#gmail.com', 'Marco'); // 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 = 'NEW APPLICATION';
$mail->Body = "--PERSONAL INFORMATION--
First Name: $fname Middle Name: $mname Last Name: $lname
Address:$address City:$city State:$state Zip:$zip ";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if (isset($_POST['Submit']))
{
$mail->send();
}
?>
If the test for isset($_POST['Submit']) is failing it either means that the Submit param is not provided, or it's present with a null value. This is a hazard of using isset rather than a more accurate check like array_key_exists('Submit', $_POST).
Also why check for the presence of that param so late? Check it earlier so you can skip all that other PHPMailer code that's not going to be used.
as written on the subject, PHPMailer send the messages with my server email, not the email of the actual sender.
for example:
from: sendername myservername#server.com
the sender email doesn't show up on the email from section
here is my code
$result = mysql_query($insert_query, $connection) or die(mysql_error());
if($result)
{
require_once('../se482/class.phpmailer.php');
//$mail->SMTPDebug = 3; // Enable verbose debug output
//$mail->IsSMTP(true);
$mail = new PHPMailer; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'mypassowrd'; // SMTP password
$mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';
$mail->addAddress('myemail#gmail.com'); // Add a recipient
$mail->setFrom($email, $name);
//$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->isHTML(true); // Set email format to HTML
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
$mail->SmtpClose();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
You have to include class.smtp.php as well to use smtp. Also uncomment //$mail->isSMTP(true);
As mentioned in documentation to avoid this you can include only autoloader class that is PHPMailerAutoload.php
updated code
$result = mysql_query($insert_query, $connection) or die(mysql_error());
if($result)
{
//require_once('../se482/class.phpmailer.php');
// require_once('../se482/class.smtp.php');
//or
require_once('../se482/PHPMailerAutoload.php');
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(true);
$mail = new PHPMailer; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#gmail.com'; // SMTP username
$mail->Password = 'mypassowrd'; // SMTP password
$mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Encoding = '8bit';
$mail->addAddress('myemail#gmail.com'); // Add a recipient
$mail->setFrom($email, $name);
//$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->isHTML(true); // Set email format to HTML
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->From = $email;
$mail->Subject = 'Here is the subject';
$mail->Body = $comment;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
$mail->SmtpClose();
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Update
You have also add your app credential in get_auth_token.php as mentioned in documentation for use gmail. Here is link https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
$mail->from is set to $email which isn't defined anywhere.
I have a webapp to send mails and using PHPMailer.
I have generated a contact list which stores the email addresses in database.
Now in my mail function(web page), I do not have any field to enter email address manually, rather I would like to fetch the email id's from table and send mail to all at one click.
Can someone please help me with this, not knowing what to do.
public function actionSendMail(){
$model = YourModelForMails::model()->findall();
foreach($model as $m){
$this->mailfunction($m['emailcolumn']);
}
}
public function mailfunction($email){
sendyourmail to: $email;
}
SoI will not code for you the app but will give really useful hint to make you workout your app.
Use Yii CActiveRecord based Model to fetch User Data (Normally containing Email and Other Details)
Put PHPMailer in vendors Folder and Import using alias path vendor (Hint Yii::import)
Use PHPMailer and Do the Sending. Here I attach PHPMailer's own example
Put Effort to Make them work together. if you cannot, it means you need to put your work aside and go learning!
Code:
<?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 server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // 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('josh#example.net', 'Josh Adams'); // 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;
exit;
}
echo 'Message has been sent';