Image doesn't show when I send an email / swiftmailer [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am currently using Swiftmailer to send my emails. Everything works fine it is sending the emails however the emails are not showing the Logo I have put in. Here is the code:
require_once 'vendor/autoload.php';
require_once 'config/constants.php';
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername(EMAIL)
->setPassword(PASSWORD);
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
function sendVerificationEmail($userEmail, $token){
global $mailer;
$body = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Verify Email</title>
</head>
<body>
<div class="not-verified-container">
<img src="logo.svg" class="mazaw-logo-not-verified">
<div class="container-without-logo">
<div class="not-verified-texts">
<h2>Account Verification</h2>
</div>
</div>
</div>
</body>
</html>';
// Create a message
$message = (new Swift_Message('Account Verification'))
->setFrom(EMAIL)
->setTo($userEmail)
->setBody($body, 'text/html');
// Send the message
$result = $mailer->send($message);
}
I would be very very thankful if someone could take a look and see what I have done wrong.
Thank you!

Embedding should work fine
<?php
$message = new Swift_Message('Your subject');
$message->setBody(
'<html>' .
' <body>' .
' <img src="' . $message->embed(Swift_Image::fromPath('image.png')) . '" />' .
' </body>' .
'</html>',
'text/html'
);
?>

You need to include the logo image in the email as an attachment. Not sure how thats done in Swiftmailer but when I googled it there are numerous examples including this one from here in StackOverflow.
Swiftmailer Attachments
$uploadedFileName = CUploadedFile::getInstance($model,'career_resume');
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName);
$message->attach($swiftAttachment);
Also Google how to embed an image in Swiftmailer.

Related

How can i echo input data fields in an file_get_contents html template?

I have this send_mail.php file. I'm trying to echo some fields that are inserted in a form and i can't with echo function. What am I doing wrong?
<?php
$date = $_POST['date'];
$full_name = $_POST['full_name'];
$biz_name = $_POST['biz_name'];
$activity = $_POST['activity'];
$afm = $_POST['afm'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
$city = $_POST['website'];
$email = $_POST['email'];
$discount = $_POST['discount'];
$payment_amount = $_POST['payment_amount'];
$seller_name = $_POST['seller_name'];
$to = $email;
$subject = "Welcome to our site!";
$htmlContent = file_get_contents("email_template.html");
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type: text/html; charset=UTF-8" . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Mail sending successful.';
else:
$errorMsg = 'Oops! Something went wrong.';
endif;
?>
And my email_template.html looks like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to our site</title>
<head/>
<body>
<h1> Welcome <?php echo $full_name; ?> </h1>
...
</body>
</html>
I'm new in web development so be gentle! :P
file_get_contents() doesn't parse the file as php. So one option would be to do something like this:
ob_start();
include('email_template.html');
$htmlContent = ob_get_contents();
ob_end_clean();
When you use include() it assumes you are running php code. Then we use an output buffer to capture the contents.
This way all variables are still available, otherwise you need to setup a whole template engine, in that case I would use an existing one like Twig.
Edit template and mark variables by symbols or special names than replace real vars:
email_template.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to our site</title>
<head/>
<body>
<h1> Welcome --full_name-- </h1>
...
</body>
</html>
php :
<?php
$htmlContent = file_get_contents("email_template.html");
$htmlContent = str_replace("--full_name--",$_POST['full_name'],$htmlContent);
I´ve just learned how to answer like simple text, my first answer was totally "coded".
Well, i suggest you learn codeigniter, it´s not the best for sure, but it´s the easiest on my view, and nowadays the most famous is laravel.
Nothing is easy for beginners anyway, but before starting studing any framework, look for understand php and php oo first.
See´ya! :)
How are both files connected? Are you using ajax?
Look, if you´re using ajax, just return $full_name, and print it with jquery or javascript. (I believe you didn't this, but if you do...)
Otherwise, if you just posted and redirected to send_mail.php, just use echo on this same page, just like:
// Send email
if (mail($to,$subject,$htmlContent,$headers)):
echo = 'Mail sending successful.';
else :
echo = 'Oops! Something went wrong.';
endif ;
A third way I can suggest to you, is using $_SESSION.
On send_mail.php start a session with:
session_start();
//and declare
$_SESSION["full_name "]= $fullname;
Then you'll have this variable enabled for any page, while this user is on your site, and you just have to echo this way
echo $_SESSION['full_name'];
When you feel a little improved, I suggest you learn a php framework, that helps a lot. Good luck!

Should all files have the .php extension or should PHP output a string? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I want to show the server status of an IP address on every page, but to check the status I need a PHP script. This script is what I found on the Internet:
<?php
$server = 'google.be:80';
$split = explode(':', $server);
$ip = $split[0];
$port = (empty($split[1])) ? '80' : $split[1];
$server = $ip . ':' . $port;
$fp = #fsockopen($ip, $port, $errno, $errstr, 1);
if($fp) {
echo $server . ' is online';
fclose($fp);
}
else {
echo $server . ' is offline';
}
?>
I want the echoes to be formatted like my CSS content is formatted, so I could just replace the echoes with:
?>
<p>Server is offline<p>
<?php
and
?>
<p>Server is online<p>
<?php
But then I would have to make every HTML file a PHP file. Would you recommend that or is there a different way to handle this?
On my server all the files are a PHP since I need to include PHP functions such as echo username and such, and I believe it doesn't hurt to convert .html to .php. Another thing is that the following page provides information on styling PHP echoes with CSS.
How can I style a PHP echo text?
I think it would be better have all PHP files.
You could use jQuery AJAX to send the PHP data to your HTML page. You could json_encode the response and receive that data as a JSON object and get the data out of it.
EDIT: In a production enviroment and for efficiency, it would be best if you convert the HTML files to PHP files, it will be worth the labour. However this little snippet below could be used for other functionality if modified or built upon so it's a learning experience for you to see basic jQuery AJAX calls.
The following code is a working example of calling your PHP file and getting back the result. Seeing a basic example of using jQuery and AJAX will help you get a firm grounding of how to use it.
check_server.php
<?php
$server='google.be:80';
$split=explode(':',$server);
$ip=$split[0];
$port=(empty($split[1]))?'80':$split[1];
$server=$ip.':'.$port;
$fp = fsockopen($ip, $port, $errno, $errstr, 1);
$result = new stdClass();
if($fp){
$result->result = 'success';
fclose($fp);
}
else{
$result->result = 'offline';
}
echo json_encode($result);
?>
index.html
<html>
<head>
<title>Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url : "check_server.php",
type : "POST",
dataType: "json",
success : function(results){
if (results.result === 'success')
{
$('#status').append('Server online.');
}
else
{
$('#status').append('Server offline.');
}
},
error : function()
{
$('#status').append('An error has occurred.');
}
});
</script>
</head>
<body>
<div id="status"></div>
</body>
</html>
It is not possible to implement PHP in an HTML file. To create HTML in a .php file is the best solution to solve this.
You can use HTML in a PHP file and you do not have to use PHP in the file if you name it a PHP file.

PHPMailer html not showing images

I'm using PHPMailer to send the registration confirm email to my users with a full html file. The email shows perfectly except for the pictures are not loaded.
Here I attach my php code:
$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8';
$mail->setFrom($this->Settings->SUPPORT_EMAIL_ADDRESS);
$mail->AddAddress($email);
$mail->Subject = dblang('registration_confirm_email_subject');
if (file_exists(APPPATH . '/views/email/registration/confirm.txt.' . $this->lang->lang() . '.php')) {
$mail->AltBody = $this->load->view('_email/registration/confirm.txt.' . $this->lang->lang() . '.php',
$emailData, true);
}
$mail->isHTML(true);
$mail->Body = $this->load->view('_email/registration/confirm.html.' . $this->lang->lang() . '.php',
$emailData, true);
if($mail->send()) {
swal_success('registration_email_send_title', 'registration_email_send_text');
redirect(site_url('home'));
}
Here I put a part of the html code. I've tried with relative and absolute urlfor the pictures path:
<img src="../../../../../../bets/assets/email/welovesports.png" alt="We Love Sports" border="0" width="290" height="92"
style="display: block;border: none;outline: none;text-decoration: none;-ms-interpolation-mode: bicubic;">
Since you've tried absolute urls with no luck.
looking on your code reference ../../../../../../bets/assets/email/
I suggest copy that image to very root of public_html -> /public_html/welovesports.png
and then reference it as <img src="http://your-domain.test/welovesports.png">
because the no. of directories your code suggests, can make it difficult to reference the correct path
OR Alternatively
you can use http://imgur.com/upload and host your image with them.
and on the gallery page of that image copy its direct link to use it
(it will be like http://i.imgur.com/RDz0KBs.png)
eg: http://i.imgur.com/RDz0KBs.png

PHP Cant Pass Variable include File? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Lets go to the Point,
I hava a form like this
exhibition.php
<?php
include("email_class.php");
if(isset($_POST[SAVE])){
$email = $_POST[EMAIL];
$company = $_POST[COMPANY];
$gender = $_POST[GENDER];
$buyer = $_POST[BUYER];
$discussion = $_POST[DISCUSSION];
$class = new email_class();
$class->notifikasi($discussion);
}//end if
?>
And the code include file like this
email_class.php
<?php
class email_class{
function notifikasi($discussion){
if($discussion == "DISCUSSION"){
$to = $email;
$subjek = "Thanks for visiting us at Gulfood exhibition, Dubai";
$message = "<html>
<head>
<title>Exibithion Email</title>
</head>
<body>
bla blaa";
$message.= "Dear <b> ".$gender." ".$buyer."</b><br><br>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: **** Group <export#***>' . "\r\n";
echo $message;
}//end if
}//end function
}//end of class
?>
Ok, echo HTML mail is Succees. anything is running well, only the variabel from exhibition.php cant pass to the email_class.php.
Can anyone Fix my Problem?
Pass the values as arguments to notifikasi.
<?php
include("email_class.php");
if(isset($_POST[SAVE])){
$class = new email_class();
$class->notifikasi($_POST[EMAIL], $_POST[COMPANY], $_POST[GENDER], $_POST[BUYER], $_POST[DISCUSSION])
}//end if
?>
class
class email_class{
function notifikasi($email, $company, $gender, $buyer, $discussion){
if($discussion == "DISCUSSION"){
/** ... **/

How to sendmail via php with dynamic url

I have a big problem! I need to track what the users are doing on my site. As a way to resolve it I crated a sendmail function in order to send me an email every time a user clicks on a button. The code is this:
<div class="buy">
<a onclick="target='_blank'" href="<?php echo $this->product['from'];?>">
<?php
// The message
$message = "A new buy";
$link = "<?php echo $this->product['from'];?>";
// Send
mail('xxx#mail.com', '#buy PRODUCT', $message, $link);
?>
<img src="http://xxx.com/data/images/xxx.jpg" alt="Comprar" />
</a>
</div>
The message I receive is
"A new buy
**<?php echo $this->product['from'];?>**"
And it should look like:
"A new buy
http://www.xxxx.com"
Anyone can help me with this problem?
Ok, then try this:
$message = "A new buy ".PHP_EOL.PHP_EOL;//add two new lines for plaintext message
$message .= $this->product['from']; //add link to the end of message
// Send
mail('xxx#mail.com', '#buy PRODUCT', $message); //no need for fourth parameter
Read further:
http://php.net/manual/en/function.mail.php
And for easy e-mailing use phpmailer:
http://code.google.com/a/apache-extras.org/p/phpmailer/
Instead of:
$link = "<?php echo $this->product['from'];?>";
use
$link = $this->product['from'];

Categories