mail with attachment with stanard mail function [duplicate] - php

This question already has answers here:
Send attachments with PHP Mail()?
(16 answers)
Closed 5 years ago.
I need to send an email with an attachment with the standard php mail function. I can't use classes like phpmailer. PHP verison 5.3.
This is working so far.
mail($to,$subject,$content,$headers);
Can you help me out?

Use below code for send mail with attachment
$email = new PHPMailer();
$email->From = 'you#example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress#example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();

Related

PhpMailer gets sent to spam in gmail and hotmail [duplicate]

This question already has answers here:
php email - how to avoid mail ending up in spam box
(6 answers)
Closed 1 year ago.
I am using PHPMailer - https://github.com/PHPMailer/PHPMailer to send an email with attachment. The mails gets sent to gmail/hotmail but they get send to "Junk", how do I avoid this? What could be the trustable domain from which I can easily send to User's inbox?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'vendor/autoload.php';
require_once "./vendor/phpmailer/phpmailer/src/PHPMailer.php";
require_once "./vendor/phpmailer/phpmailer/src/Exception.php";
require_once "./vendor/phpmailer/phpmailer/src/POP3.php";
require_once "./vendor/phpmailer/phpmailer/src/SMTP.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer();
$email->From = 'Steven123#gmail.com';
$email->FromName = 'Steven';
$email->Subject = 'PDF attachment';
$email->Body = 'Hi';
$email->AddAddress( 'NAME_OF_THE_USER#gmail.com' );
$email->AddAttachment( "./PDF.pdf" , "PDF.pdf", 'base64', 'application/octet-stream' );
$email->Send();
?>
Thank you for all the info, I got it working and tested it to send mails to gmail and hotmail. I used SMPT server of our own company and did something like this
$email = new PHPMailer();
$email->Host = 'SMPT SERVER ADDRESS'; //Set the SMTP server to send through
$email->SMTPAuth = true; //Enable SMTP authentication
$email->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$email->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$email->From = 'Steven123#gmail.com';
$email->FromName = 'Steven';
$email->Subject = 'PDF attachment';
$email->Body = 'Hi';
$email->AddAddress( 'NAME_OF_THE_USER#gmail.com' );
$email->AddAttachment( "./PDF.pdf" , "PDF.pdf", 'base64', 'application/octet-stream' );
$email->Send();

PHP mailer redirect issue

I came across an issue using PHPmailer that I just don't understand. I have an assist function redirect for the header function that works 100% in the position shown. It fails to work in the positions shown in the comments where I would like it to work. Everything else works perfectly regardless of where the redirect function is placed. Any ideas? This is also my first post so apologies in advance if there is already a solution which I couldn't find.
<?php
if(isset($_POST['replyall'])) {
redirect("index.php?leadreply"); // Why does this have to be here to work???? //
if(isset($_POST['chk1'])) {
$email = new PHPMailer();
$email->From = $_POST['author'];
$email->FromName = 'JGM Decorating';
$email->Subject = 'Reply to your contact request';
$email->Body = $_POST['comments'];
$email->AddAddress( $_POST['destination'] );
$file_to_attach = '../crm/gtcjgm.pdf';
$email->AddAttachment( $file_to_attach , 'Terms and Conditions.pdf' );
return $email->Send();
// I would like to have the redirect here but it doesn't work??//
} else {
$email = new PHPMailer();
$email->From = $_POST['author'];
$email->FromName = 'JGM Decorating';
$email->Subject = 'Reply to your contact request';
$email->Body = $_POST['comments'];
$email->AddAddress( $_POST['destination'] );
$file_to_attach = '';
$email->AddAttachment( $file_to_attach , 'Terms and Conditions.pdf' );
return $email->Send();
// I would like to have a different redirect here but it doesn't work??//
}
}
?>
This code:
return $email->Send();
Explanation: This will be returning the function and it will stop the process at this stage itself and after that whatever line you place it will not work. (E.g) Echo statement also will not work.
If you want to do both you have to do the mail sending process using Ajax and after that alone you have to redirect as per you need .
Since the return will stop the execution at that instant itself. So in order to do so you can use it with the hep of AJAX and then redirect it.

Is there a way to add a button to attach file to email?

in my case, I have to add the email button on the file list but before it I must be signed first so that the file can be viewed, this email button when clicked the file will automatically be in the form of attachment when sending mail. In that cases is it possible without having to download the file first and then browse the file as attachment ?
Basic version (using php):
$email = new PHPMailer();
$email->From = 'you#example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress#example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();
Reference : Send attachments with PHP Mail()?
Using laravel 5.0:
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
$message->attach($pathToFile);
});
Refer : Laravel mail

Using PHPMailer send attachment file in PHP

I currently using PHPMailer to sending mail with attachment file in localhost and also in my server.
The problem I have is the attachment file does not receive in email when execute in server. But in other side, the attachment file receive perfectly when I execute on localhost.
Here is my simple code:
$email = new PHPMailer();
$email->From = 'william#msmedia.com.sg';
$email->FromName = 'John';
$email->Subject = 'Message Subject';
$email->Body = 'test';
$email->AddAddress( 'william#msmedia.com.sg' );
$file_to_attach = $_FILES["cv"]["tmp_name"];
$email->AddAttachment( $file_to_attach , $_FILES["cv"]["name"]);
return $email->Send();
Can anyone able to correct my mistake..?

PHPMailer not sending mail

I'm trying to send an order confirmation and also notify the seller about a user purchase. However, PHPMailer only sends the first email. Here's quick and dirty:
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Subject';
$email->Body = $bodytext;
$email->AddAddress($_REQUEST['sahkoposti']);
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
<?php
//send message to seller
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Tilaus vastaanotettu';
$email->Body = $bodytext;
$email->AddAddress("mail#mail.com");
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
Is it even possible to send multiple emails from one script?
It is possible, however you're using return in the first statement, which will stop execution of the function. Remove the first return (just use $email->Send();) and it should work.
The second email does not get executed because you are returning right after sending the first email, you should change:
return $email->Send();
for this:
$email->Send();

Categories