Jump to content

Swiftmail Issue intergrating it to PHP Project


Pandee

Recommended Posts

I have Swift/symfony mail installed and sample code setup.

When I require_once to a working PHP page, the page becomes blank in a browser, If i remove it from the require the website works. Lints can't find any errors or syntax issues in the code. Its on gitlab.

What should i do? Below is the page where I send an email out and the environment I am in

Ubuntu

PHP7

SwiftMail 6.0^

 

<?php
require_once '/vendor/autoload.php';
session_start();
// Create the Transport
$transport = (new Swift_SmtpTransport('localhost', 465))
    ->setUsername(noreply@example.com)
    ->setPassword(password);

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

function sendVerificationEmail($email, $token)
{
    global $mailer;
    $body = '<!DOCTYPE html>
    <html lang="en">

    <head>
      <meta charset="UTF-8">
      <title>Test mail</title>

    </head>

    <body>
      <div class="wrapper">
        <p>Thank you for signing up on our site. Please click on the link below to verify your account:.</p>
        <a href="https://tadoo.club/verify-email.php?token=' . $token . '">Verify Email!</a>
      </div>
    </body>

    </html>';

    // Create a message
    $message = (new Swift_Message('Verify your email'))
        ->setFrom(noreply@example.com)
        ->setTo($email)
        ->setBody($body, 'text/html');

    // Send the message
    $result = $mailer->send($message);



?>

 

Link to comment
Share on other sites

6 minutes ago, requinix said:

A blank page means a fatal PHP error. What do your server error logs say?

Sorry about the slow reply, I checked and its this

 

"AH01071: Got error 'PHP message: PHP Parse error: syntax error, unexpected '@', expecting ')' in /var/www/vhosts/tadoo.club/httpdocs/sendEmails.php on line 6'"

Was I not meant to use @ in an user email address?

Link to comment
Share on other sites

Great.

->setUsername(noreply@example.com)

That email address in there is supposed to be a string. That means it needs quotes. See how '/vendor/autoload.php' and 'localhost' and 'Verify your email' are? Those are also strings.

The password in

->setPassword(password)

also needs to be a string, but it could be that you removed your password and replaced it with that just for the post, so for all I know the version you have in front of you is already a string.

Link to comment
Share on other sites

2 minutes ago, requinix said:

Great.


->setUsername(noreply@example.com)

That email address in there is supposed to be a string. That means it needs quotes. See how '/vendor/autoload.php' and 'localhost' and 'Verify your email' are? Those are also strings.

The password in


->setPassword(password)

also needs to be a string, but it could be that you removed your password and replaced it with that just for the post, so for all I know the version you have in front of you is already a string.

Ahh I see.. I thought it was weird when the example had no quotes.  I think this should do the trick.

 

Thank you so much!

Link to comment
Share on other sites

Just now, Pandee said:

Ahh I see.. I thought it was weird when the example had no quotes.  I think this should do the trick.

The example might have been using a constant. Those are pre-defined values like

define('USERNAME', 'noreply@example.com');
// or
const USERNAME = 'noreply@example.com';

In the first one, the constant is created with a function, and the name of the constant needs to be a string because it's passed as a value to the define() function.
The second one shows a special syntax for creating constants, where the name of the constant doesn't have to be a string because that's how the syntax works: name of constant = value of constant.

Note that for both of those syntaxes, the email value is still a string.

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.